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
Copy file name to clipboardExpand all lines: README.md
+76-2Lines changed: 76 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,9 @@ This repository contains Swift community-maintained implementation over [OpenAI]
16
16
-[Usage](#usage)
17
17
-[Initialization](#initialization)
18
18
-[Completions](#completions)
19
+
-[Completions Streaming](#completions-streaming)
19
20
-[Chats](#chats)
21
+
-[Chats Streaming](#chats-streaming)
20
22
-[Images](#images)
21
23
-[Audio](#audio)
22
24
-[Audio Transcriptions](#audio-transcriptions)
@@ -146,6 +148,43 @@ let result = try await openAI.completions(query: query)
146
148
- index : 0
147
149
```
148
150
151
+
#### Completions Streaming
152
+
153
+
Completions streaming is available by using `completionsStream` function. Tokens will be sent one-by-one.
154
+
155
+
**Closures**
156
+
```swift
157
+
openAI.completionsStream(query: query) { partialResult in
158
+
switch partialResult {
159
+
case .success(let result):
160
+
print(result.choices)
161
+
case .failure(let error):
162
+
//Handle chunk error here
163
+
}
164
+
} completion: { error in
165
+
//Handle streaming error here
166
+
}
167
+
```
168
+
169
+
**Combine**
170
+
171
+
```swift
172
+
openAI
173
+
.completionsStream(query: query)
174
+
.sink { completion in
175
+
//Handle completion result here
176
+
} receiveValue: { result in
177
+
//Handle chunk here
178
+
}.store(in: &cancellables)
179
+
```
180
+
181
+
**Structured concurrency**
182
+
```swift
183
+
fortryawait result in openAI.completionsStream(query: query) {
184
+
//Handle result here
185
+
}
186
+
```
187
+
149
188
Review [Completions Documentation](https://platform.openai.com/docs/api-reference/completions) for more info.
150
189
151
190
### Chats
@@ -175,8 +214,6 @@ Using the OpenAI Chat API, you can build your own applications with `gpt-3.5-tur
175
214
publiclet topP: Double?
176
215
/// How many chat completion choices to generate for each input message.
177
216
publiclet n: Int?
178
-
/// If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only `server-sent events` as they become available, with the stream terminated by a data: [DONE] message.
179
-
publiclet stream: Bool?
180
217
/// Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
181
218
publiclet stop: [String]?
182
219
/// The maximum number of tokens to generate in the completion.
@@ -244,6 +281,43 @@ let result = try await openAI.chats(query: query)
244
281
- total_tokens : 49
245
282
```
246
283
284
+
#### Chats Streaming
285
+
286
+
Chats streaming is available by using `chatStream` function. Tokens will be sent one-by-one.
287
+
288
+
**Closures**
289
+
```swift
290
+
openAI.chatsStream(query: query) { partialResult in
291
+
switch partialResult {
292
+
case .success(let result):
293
+
print(result.choices)
294
+
case .failure(let error):
295
+
//Handle chunk error here
296
+
}
297
+
} completion: { error in
298
+
//Handle streaming error here
299
+
}
300
+
```
301
+
302
+
**Combine**
303
+
304
+
```swift
305
+
openAI
306
+
.chatsStream(query: query)
307
+
.sink { completion in
308
+
//Handle completion result here
309
+
} receiveValue: { result in
310
+
//Handle chunk here
311
+
}.store(in: &cancellables)
312
+
```
313
+
314
+
**Structured concurrency**
315
+
```swift
316
+
fortryawait result in openAI.chatsStream(query: query) {
317
+
//Handle result here
318
+
}
319
+
```
320
+
247
321
Review [Chat Documentation](https://platform.openai.com/docs/guides/chat) for more info.
0 commit comments