@@ -121,6 +121,82 @@ Use the `objects.deleteOne()` method to delete an Object by specifying the Objec
121121await cosmic .objects .deleteOne (' 5ff75368c2dfa81a91695cec' );
122122```
123123
124+ ## AI Capabilities
125+
126+ Cosmic provides AI-powered text and image generation capabilities through the SDK.
127+
128+ ### Generate Text [[ see docs] ( https://www.cosmicjs.com/docs/api/ai#generate-text )]
129+
130+ Use the ` ai.generateText() ` method to generate text content using AI models. You must provide either a ` prompt ` or ` messages ` parameter.
131+
132+ #### Using a simple prompt:
133+
134+ ``` jsx
135+ const textResponse = await cosmic .ai .generateText ({
136+ prompt: ' Write a product description for a coffee mug' ,
137+ max_tokens: 500 , // optional
138+ });
139+
140+ console .log (textResponse .text );
141+ console .log (textResponse .usage ); // { input_tokens: 10, output_tokens: 150 }
142+ ```
143+
144+ #### Using messages for chat-based models:
145+
146+ ``` jsx
147+ const chatResponse = await cosmic .ai .generateText ({
148+ messages: [
149+ { role: ' user' , content: ' Tell me about coffee mugs' },
150+ {
151+ role: ' assistant' ,
152+ content: ' Coffee mugs are vessels designed to hold hot beverages...' ,
153+ },
154+ { role: ' user' , content: ' What materials are they typically made from?' },
155+ ],
156+ max_tokens: 500 , // optional
157+ });
158+
159+ console .log (chatResponse .text );
160+ console .log (chatResponse .usage );
161+ ```
162+
163+ ### Analyze Images and Files
164+
165+ The AI model can analyze images and files when generating text responses. This feature works with both the ` prompt ` and ` messages ` approaches.
166+
167+ ``` jsx
168+ const textWithImageResponse = await cosmic .ai .generateText ({
169+ prompt: ' Describe this coffee mug and suggest improvements to its design' ,
170+ media_url: ' https://imgix.cosmicjs.com/your-image-url.jpg' ,
171+ max_tokens: 500 ,
172+ });
173+
174+ console .log (textWithImageResponse .text );
175+ console .log (textWithImageResponse .usage );
176+ ```
177+
178+ ### Generate Image [[ see docs] ( https://www.cosmicjs.com/docs/api/ai#generate-image )]
179+
180+ Use the ` ai.generateImage() ` method to create AI-generated images based on text prompts.
181+
182+ ``` jsx
183+ const imageResponse = await cosmic .ai .generateImage ({
184+ prompt: ' A serene mountain landscape at sunset' ,
185+ // Optional parameters
186+ metadata: { tags: [' landscape' , ' mountains' , ' sunset' ] },
187+ folder: ' ai-generated-images' ,
188+ alt_text: ' A beautiful mountain landscape with a colorful sunset' ,
189+ });
190+
191+ // Access the generated image properties
192+ console .log (imageResponse .media .url ); // Direct URL to the generated image
193+ console .log (imageResponse .media .imgix_url ); // Imgix-enhanced URL for additional transformations
194+ console .log (imageResponse .media .width ); // Image width
195+ console .log (imageResponse .media .height ); // Image height
196+ console .log (imageResponse .media .alt_text ); // Alt text for the image
197+ console .log (imageResponse .revised_prompt ); // Potentially revised prompt used by the AI
198+ ```
199+
124200## Learn more
125201
126202Go to the [ Cosmic docs] ( https://www.cosmicjs.com/docs ) to learn more capabilities.
0 commit comments