You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Conversation class provides a high-level interface for managing a conversation with the model. It wraps the `ResponsesAPI` class and handles the details of sending and receiving messages, as well as managing the conversation history.
108
+
109
+
#### Configuring the conversation
110
+
111
+
To create a `Conversation` instance, all you need is an OpenAI API key, and the model you will be talking to:
You can optionally provide a closure to configure the conversation, adding a system prompt or tools for the model to use:
118
+
119
+
```swift
120
+
@Stateprivatevar conversation =Conversation(authToken: OPENAI_API_KEY, using: .gpt4o) { config in
121
+
// configure the model's behaviour
122
+
config.instructions="You are a coding assistant that talks like a pirate"
123
+
124
+
// allow the model to browse the web
125
+
config.tools= [.webSearch()]
126
+
}
127
+
```
128
+
129
+
Your configuration will be reused for all subsequent messages, but you can always change any of the properties (including which model you're talking to!) mid-conversation:
130
+
131
+
```swift
132
+
// update a bunch of properties at once
133
+
conversation.updateConfig { config in
134
+
config.model= .o3Mini
135
+
}
136
+
137
+
// or update them directly
138
+
conversation.truncation= .auto
139
+
```
140
+
141
+
#### Sending messages
142
+
143
+
Your `Conversation` instance contains various helpers to make communicating with the model easier. For example, you can send a simple text message like this:
144
+
145
+
```swift
146
+
tryawait conversation.send(text: "Hey!")
147
+
```
148
+
149
+
There are also helpers for providing the output of a tool call or computer use call:
For more complex use cases, you can construct the `Input` yourself:
157
+
158
+
```swift
159
+
tryawait conversation.send(Input([
160
+
.message(content: Input.Content([
161
+
.image(fileId: "..."),
162
+
.text("Take a look at this image and tell me what you see"),
163
+
])),
164
+
]))
165
+
```
166
+
167
+
#### Reading messages
168
+
169
+
You can access the messages in the conversation through the messages property. Note that this won't include function calls and its responses, only the messages between the user and the model. To access the full conversation history, use the `entries` property. For example:
170
+
171
+
```swift
172
+
ScrollView {
173
+
ScrollViewReader { scrollView in
174
+
VStack(spacing: 12) {
175
+
ForEach(conversation.messages, id: \.self) { message in
@@ -56,9 +199,9 @@ Optionally, you can provide an Organization ID and/or project ID:
56
199
57
200
```swift
58
201
let client =ResponsesAPI(
59
-
authToken: YOUR_OPENAI_API_KEY,
60
-
organizationId: YOUR_ORGANIZATION_ID,
61
-
projectId: YOUR_PROJECT_ID
202
+
authToken: YOUR_OPENAI_API_KEY,
203
+
organizationId: YOUR_ORGANIZATION_ID,
204
+
projectId: YOUR_PROJECT_ID
62
205
)
63
206
```
64
207
@@ -77,9 +220,9 @@ To create a new response, call the `create` method with a `Request` instance:
77
220
78
221
```swift
79
222
let response =tryawait client.create(Request(
80
-
model: "gpt-4o",
81
-
input: .text("Are semicolons optional in JavaScript?"),
82
-
instructions: "You are a coding assistant that talks like a pirate"
223
+
model: "gpt-4o",
224
+
input: .text("Are semicolons optional in JavaScript?"),
225
+
instructions: "You are a coding assistant that talks like a pirate"
83
226
))
84
227
```
85
228
@@ -91,18 +234,40 @@ To stream back the response as it is generated, use the `stream` method:
91
234
92
235
```swift
93
236
let stream =tryawait client.stream(Request(
94
-
model: "gpt-4o",
95
-
input: .text("Are semicolons optional in JavaScript?"),
96
-
instructions: "You are a coding assistant that talks like a pirate"
237
+
model: "gpt-4o",
238
+
input: .text("Are semicolons optional in JavaScript?"),
239
+
instructions: "You are a coding assistant that talks like a pirate"
97
240
))
98
241
99
242
fortryawait event in stream {
100
-
switch event {
101
-
// ...
102
-
}
243
+
switch event {
244
+
// ...
245
+
}
103
246
}
104
247
```
105
248
249
+
#### Uploading files
250
+
251
+
While uploading files is not part of the Responses API, you'll need it for sending content to the model (like images, PDFs, etc.). You can upload a file to the model like so:
252
+
253
+
```swift
254
+
let file =tryawait client.upload(file: .file(name: "image.png", contents: imageData, contentType: "image/png"))
255
+
256
+
// then, use it on a message
257
+
tryawait conversation.send(Input([
258
+
.message(content: Input.Content([
259
+
.image(fileId: file.id),
260
+
.text("Take a look at this image and tell me what you see"),
261
+
])),
262
+
]))
263
+
```
264
+
265
+
You can also load files directly from the user's filesystem or the web:
266
+
267
+
```swift
268
+
let file =tryawait client.upload(file: .url(URL(string: "https://example.com/file.pdf")!, contentType: "application/pdf"))
269
+
```
270
+
106
271
#### Other Stuff
107
272
108
273
You can retrieve a previously-created response by calling the `get` method with the response ID:
0 commit comments