Skip to content

Commit

Permalink
Documentation of usage (#16)
Browse files Browse the repository at this point in the history
* Updating documentation

* Udpate
  • Loading branch information
jamesrochabrun committed Mar 22, 2024
1 parent 754c277 commit 5900d83
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
64 changes: 57 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ An open-source Swift package designed for effortless interaction with OpenAI's p
- [Getting an API Key](#getting-an-api-key)
- [Installation](#installation)
- [Usage](#usage)
- [Azure OpenAI](#azure-openAI)
- [Collaboration](#collaboration)

## Description

`SwiftOpenAI` is an open-source Swift package that streamlines interactions with **all** OpenAI's API endpoints.
`SwiftOpenAI` is an open-source Swift package that streamlines interactions with **all** OpenAI's API endpoints, now with added support for Azure and Assistant stream APIs.

### OpenAI ENDPOINTS

Expand All @@ -46,10 +47,9 @@ An open-source Swift package designed for effortless interaction with OpenAI's p
- [Runs](#runs)
- [Run Step object](#run-step-object)
- [Run Step details](#run-step-details)
- [Streaming](#streaming)
- [Assistants Streaming](#assistants-streaming)
- [Message Delta Object](#message-delta-object)
- [Run Step Delta Object](#run-step-delta-object)
### [Azure OpenAI](#azure-openAI)

## Getting an API Key

Expand Down Expand Up @@ -2460,9 +2460,15 @@ public struct RunStepDetails: Codable {
}
```
### Streaming
### Assistants Streaming
Assistants API [streaming.](https://platform.openai.com/docs/assistants/overview?context=with-streaming))
Assistants API [streaming.](https://platform.openai.com/docs/api-reference/assistants-streaming)
Stream the result of executing a Run or resuming a Run after submitting tool outputs.
You can stream events from the [Create Thread and Run](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun), [Create Run](https://platform.openai.com/docs/api-reference/runs/createRun), and [Submit Tool Outputs](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) endpoints by passing "stream": true. The response will be a Server-Sent events stream.
OpenAI Python tutorial(https://platform.openai.com/docs/assistants/overview?context=with-streaming))
### Message Delta Object
Expand Down Expand Up @@ -2527,7 +2533,52 @@ public struct RunStepDeltaObject: Decodable {
}
```
### Azure OpenAI
⚠️ To utilize the new stream APIs, first create an assistant and initiate a thread.
Usage
[Create Thread and Run](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun)
The `createRunAndStreamMessage` streams [events](https://platform.openai.com/docs/api-reference/assistants-streaming/events), you can decide which one you need for your implememntation. e.g: This is how you can access mesage delta amd run step delta objects.
```swift
let assistantID = "asst_abc123""
let threadID = "thread_abc123"
let messageParameter = MessageParameter(role: .user, content: "Tell me the square root of 1235")
let message = try await service.createMessage(threadID: threadID, parameters: messageParameter)
let runParameters = RunParameter(assistantID: assistantID)
let stream = try await service.createRunAndStreamMessage(threadID: threadID, parameters: runParameters)

for try await result in stream {
switch result {
case .threadMessageDelta(let messageDelta):
let content = messageDelta.delta.content.first
switch content {
case .imageFile, nil:
break
case .text(let textContent):
print(textContent.text.value) // this will print the streamed response for a message.
}

case .threadRunStepDelta(let runStepDelta):
if let toolCall = runStepDelta.delta.stepDetails.toolCalls?.first?.toolCall {
switch toolCall {
case .codeInterpreterToolCall(let toolCall):
print(toolCall.input ?? "") // this will print the streamed response for code interpreter tool call.
case .retrieveToolCall(let toolCall):
print("Retrieve tool call")
case .functionToolCall(let toolCall):
print("Function tool call")
case nil:
break
}
}
}
}
```

You can go to the [Examples folder](https://github.com/jamesrochabrun/SwiftOpenAI/tree/main/Examples/SwiftOpenAIExample/SwiftOpenAIExample) in this package, navigate to the 'Configure Assistants' tab, create an assistant, and follow the subsequent steps.

## Azure OpenAI

This library provides support for both chat completions and chat stream completions through Azure OpenAI. Currently, `DefaultOpenAIAzureService` supports chat completions, including both streamed and non-streamed options.

Expand Down Expand Up @@ -2565,6 +2616,5 @@ let parameters = ChatCompletionParameters(
let completionObject = try await service.startChat(parameters: parameters)
```

### Collaboration
Open a PR for any proposed change pointing it to `main` branch.
17 changes: 17 additions & 0 deletions Sources/OpenAI/Public/ResponseModels/Delta.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Delta.swift
//
//
// Created by James Rochabrun on 3/22/24.
//

import Foundation

/// Protocol for Assistant Stream Delta.
/// Defines a set of requirements for objects that can be included in an assistant event stream, such as `RunStepDeltaObject` or `MessageDeltaObject`.
public protocol Delta: Decodable {
associatedtype T
var id: String { get }
var object: String { get }
var delta: T { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
// Created by James Rochabrun on 3/17/24.
//


public protocol Delta: Decodable {
associatedtype T
var id: String { get }
var object: String { get }
var delta: T { get }
}


import Foundation

/// Represents a [run step delta](https://platform.openai.com/docs/api-reference/assistants-streaming/run-step-delta-object) i.e. any changed fields on a run step during streaming.
Expand Down

0 comments on commit 5900d83

Please sign in to comment.