@@ -27,6 +27,7 @@ This is a Swift community-driven repository for interfacing with the [OpenAI](ht
2727 - [ Chats] ( #chats )
2828 - [ Chats Streaming] ( #chatcompletions-with-stream )
2929 - [ Chats without Streaming] ( #chatcompletions-without-stream )
30+ - [ ChatCompletions with Image Input] ( #chatcompletions-with-image-input )
3031 - [ Images] ( #images )
3132 - [ Create Image] ( #create-image )
3233 - [ Embeddings] ( #embeddings )
@@ -206,6 +207,34 @@ do {
206207 print (" Error: \( error ) " )
207208}
208209```
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+
209238## Images
210239### [ Create Image] ( https://platform.openai.com/docs/api-reference/images/create )
211240Given a prompt and/or an input image, the model will generate a new image.
0 commit comments