@@ -27,6 +27,7 @@ This is a Swift community-driven repository for interfacing with the [OpenAI](ht
27
27
- [ Chats] ( #chats )
28
28
- [ Chats Streaming] ( #chatcompletions-with-stream )
29
29
- [ Chats without Streaming] ( #chatcompletions-without-stream )
30
+ - [ ChatCompletions with Image Input] ( #chatcompletions-with-image-input )
30
31
- [ Images] ( #images )
31
32
- [ Create Image] ( #create-image )
32
33
- [ Embeddings] ( #embeddings )
@@ -206,6 +207,34 @@ do {
206
207
print (" Error: \( error ) " )
207
208
}
208
209
```
210
+
211
+ ### [ ChatCompletions with Image Input] ( https://platform.openai.com/docs/api-reference/chat/create )
212
+ Given a chat conversation, the model will return a chat completion response.
213
+
214
+ ``` swift
215
+ let message = " What appears in the photo?"
216
+ let imageVisionURL = " https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/M31bobo.jpg/640px-M31bobo.jpg"
217
+
218
+ do {
219
+ let myMessage = MessageChatImageInput (text : message,
220
+ imageURL : imageVisionURL,
221
+ role : .user )
222
+
223
+ let optionalParameters: ChatCompletionsOptionalParameters = .init (temperature : 0.5 , stop : [" stopstring" ], stream : false , maxTokens : 1200 )
224
+
225
+ let result = try await openAI.createChatCompletionsWithImageInput (model : .gpt4 (.gpt_4_vision_preview ),
226
+ messages : [myMessage],
227
+ optionalParameters : optionalParameters)
228
+
229
+ print (" Result \( result? .choices .first ? .message ) " )
230
+ self .message = result? .choices .first ? .message .content ?? " No value"
231
+ self .isLoading = false
232
+
233
+ } catch {
234
+ print (" Error: \( error ) " )
235
+ }
236
+ ```
237
+
209
238
## Images
210
239
### [ Create Image] ( https://platform.openai.com/docs/api-reference/images/create )
211
240
Given a prompt and/or an input image, the model will generate a new image.
0 commit comments