Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Update documentation on streaming
  • Loading branch information
Krivoblotsky committed May 15, 2023
1 parent db71d79 commit c82c55e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This repository contains Swift community-maintained implementation over [OpenAI]
- [Usage](#usage)
- [Initialization](#initialization)
- [Completions](#completions)
- [Completions Streaming](#streaming)
- [Chats](#chats)
- [Images](#images)
- [Audio](#audio)
Expand Down Expand Up @@ -146,6 +147,43 @@ let result = try await openAI.completions(query: query)
- index : 0
```

#### Completions Streaming

Completions streaming is available by using `completionsStream` function. Token will be sent one-by one.

**Closures**
```swift
openAI.completionsStream(query: query) { partialResult in
switch partialResult {
case .success(let result):
print(result.choices)
case .failure(let error):
//Handle chunk error here
}
} completion: { error in
//Handle streaming error here
}
```

**Combine**

```swift
openAI
.completionsStream(query: .init(model: .textDavinci_003, prompt: "What is 42?"))
.sink { completion in
//Handle completion result here
} receiveValue: { result in
//Handle chunk here
}.store(in: &cancellables)
```

**Structured concurrency**
```swift
for try await result in openAI.completionsStream(query: .init(model: .textDavinci_003, prompt: "what is 42")) {
//Handle result here
}
```

Review [Completions Documentation](https://platform.openai.com/docs/api-reference/completions) for more info.

### Chats
Expand Down

0 comments on commit c82c55e

Please sign in to comment.