Skip to content

Commit 577c27f

Browse files
authored
Sync spec + structured outputs example (#257)
* CreateChatCompletionRequest updated * CreateChatCompletionResponse updated * ChatCompletionStreamResponseDelta updated * CreateChatCompletionStreamResponse updated * CreateFineTuningJobRequest updated * ResponseFormat and ImageResponseFormat * update examples with ImageResponseFormat * AssistantToolsFileSearch updated with FileSearchRankingOptions * updated MessageContent and MessageDeltaContent to include refusal variant * update examples with message refusal variant * updated RunStepDetailsToolCallsFileSearchObject * updated VectoreStoreFileObject last_error enum variant * updated step-object link * updated ChatCompletionRequestMessage * udpated FunctionObject to include strict * update example for FunctionObject strict param * helper From traits for chat message types * Add structured-outputs example * update readme * updated readme * add comment
1 parent 650281d commit 577c27f

File tree

21 files changed

+506
-96
lines changed

21 files changed

+506
-96
lines changed

async-openai/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
- [x] Images
3535
- [x] Models
3636
- [x] Moderations
37+
- [ ] Organizations | Administration
3738
- [ ] Uploads
38-
- SSE streaming on all available APIs
39+
- SSE streaming on available APIs
3940
- Requests (except SSE streaming) including form submissions are retried with exponential backoff when [rate limited](https://platform.openai.com/docs/guides/rate-limits).
4041
- Ergonomic builder pattern for all request objects.
41-
- Microsoft Azure OpenAI Service (only APIs matching OpenAI spec)
42+
- Microsoft Azure OpenAI Service (only for APIs matching OpenAI spec)
4243

4344
## Usage
4445

@@ -61,7 +62,7 @@ $Env:OPENAI_API_KEY='sk-...'
6162

6263
```rust
6364
use async_openai::{
64-
types::{CreateImageRequestArgs, ImageSize, ResponseFormat},
65+
types::{CreateImageRequestArgs, ImageSize, ImageResponseFormat},
6566
Client,
6667
};
6768
use std::error::Error;
@@ -74,7 +75,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
7475
let request = CreateImageRequestArgs::default()
7576
.prompt("cats on sofa and carpet in living room")
7677
.n(2)
77-
.response_format(ResponseFormat::Url)
78+
.response_format(ImageResponseFormat::Url)
7879
.size(ImageSize::S256x256)
7980
.user("async-openai")
8081
.build()?;
@@ -110,14 +111,16 @@ All forms of contributions, such as new features requests, bug fixes, issues, do
110111
A good starting point would be to look at existing [open issues](https://github.com/64bit/async-openai/issues).
111112

112113
To maintain quality of the project, a minimum of the following is a must for code contribution:
114+
113115
- **Names & Documentation**: All struct names, field names and doc comments are from OpenAPI spec. Nested objects in spec without names leaves room for making appropriate name.
114-
- **Tested**: For changes supporting test(s) and/or example is required. Existing examples, doc tests, unit tests, and integration tests should be made to work with the changes if applicable.
116+
- **Tested**: For changes supporting test(s) and/or example is required. Existing examples, doc tests, unit tests, and integration tests should be made to work with the changes if applicable.
115117
- **Scope**: Keep scope limited to APIs available in official documents such as [API Reference](https://platform.openai.com/docs/api-reference) or [OpenAPI spec](https://github.com/openai/openai-openapi/). Other LLMs or AI Providers offer OpenAI-compatible APIs, yet they may not always have full parity. In such cases, the OpenAI spec takes precedence.
116118
- **Consistency**: Keep code style consistent across all the "APIs" that library exposes; it creates a great developer experience.
117119

118120
This project adheres to [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct)
119121

120122
## Complimentary Crates
123+
121124
- [openai-func-enums](https://github.com/frankfralick/openai-func-enums) provides procedural macros that make it easier to use this library with OpenAI API's tool calling feature. It also provides derive macros you can add to existing [clap](https://github.com/clap-rs/clap) application subcommands for natural language use of command line tools. It also supports openai's [parallel tool calls](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) and allows you to choose between running multiple tool calls concurrently or own their own OS threads.
122125
- [async-openai-wasm](https://github.com/ifsheldon/async-openai-wasm) provides WASM support.
123126

async-openai/src/types/assistant.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55

66
use crate::error::OpenAIError;
77

8-
use super::{FunctionName, FunctionObject};
8+
use super::{FunctionName, FunctionObject, ResponseFormat};
99

1010
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
1111
pub struct AssistantToolCodeInterpreterResources {
@@ -112,6 +112,8 @@ pub struct AssistantObject {
112112

113113
/// Specifies the format that the model must output. Compatible with [GPT-4o](https://platform.openai.com/docs/models/gpt-4o), [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
114114
///
115+
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which guarantees the model will match your supplied JSON schema. Learn more in the [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
116+
///
115117
/// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON.
116118
///
117119
/// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length.
@@ -120,25 +122,8 @@ pub enum AssistantsApiResponseFormatOption {
120122
#[default]
121123
#[serde(rename = "auto")]
122124
Auto,
123-
#[serde(rename = "none")]
124-
None,
125125
#[serde(untagged)]
126-
Format(AssistantsApiResponseFormat),
127-
}
128-
129-
/// An object describing the expected output of the model. If `json_object` only `function` type `tools` are allowed to be passed to the Run. If `text` the model can return text or any value needed.
130-
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
131-
pub struct AssistantsApiResponseFormat {
132-
/// Must be one of `text` or `json_object`.
133-
pub r#type: AssistantsApiResponseFormatType,
134-
}
135-
136-
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
137-
#[serde(rename_all = "snake_case")]
138-
pub enum AssistantsApiResponseFormatType {
139-
#[default]
140-
Text,
141-
JsonObject,
126+
Format(ResponseFormat),
142127
}
143128

144129
/// Retrieval tool
@@ -153,8 +138,28 @@ pub struct AssistantToolsFileSearch {
153138
pub struct AssistantToolsFileSearchOverrides {
154139
/// The maximum number of results the file search tool should output. The default is 20 for gpt-4* models and 5 for gpt-3.5-turbo. This number should be between 1 and 50 inclusive.
155140
///
156-
//// Note that the file search tool may output fewer than `max_num_results` results. See the [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search/number-of-chunks-returned) for more information.
157-
pub max_num_results: u8,
141+
//// Note that the file search tool may output fewer than `max_num_results` results. See the [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search/customizing-file-search-settings) for more information.
142+
pub max_num_results: Option<u8>,
143+
pub ranking_options: Option<FileSearchRankingOptions>,
144+
}
145+
146+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
147+
pub enum FileSearchRanker {
148+
#[serde(rename = "auto")]
149+
Auto,
150+
#[serde(rename = "default_2024_08_21")]
151+
Default2024_08_21,
152+
}
153+
154+
/// The ranking options for the file search.
155+
///
156+
/// See the [file search tool documentation](/docs/assistants/tools/file-search/customizing-file-search-settings) for more information.
157+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
158+
pub struct FileSearchRankingOptions {
159+
/// The ranker to use for the file search. If not specified will use the `auto` ranker.
160+
pub ranker: Option<FileSearchRanker>,
161+
/// The score threshold for the file search. All values must be a floating point number between 0 and 1.
162+
pub score_threshold: Option<f32>,
158163
}
159164

160165
/// Function tool

async-openai/src/types/assistant_stream.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,25 @@ pub enum AssistantStreamEvent {
6666
/// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) expires.
6767
#[serde(rename = "thread.run.expired")]
6868
ThreadRunExpired(RunObject),
69-
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) is created.
69+
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) is created.
7070
#[serde(rename = "thread.run.step.created")]
7171
ThreadRunStepCreated(RunStepObject),
72-
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) moves to an `in_progress` state.
72+
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) moves to an `in_progress` state.
7373
#[serde(rename = "thread.run.step.in_progress")]
7474
ThreadRunStepInProgress(RunStepObject),
75-
/// Occurs when parts of a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) are being streamed.
75+
/// Occurs when parts of a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) are being streamed.
7676
#[serde(rename = "thread.run.step.delta")]
7777
ThreadRunStepDelta(RunStepDeltaObject),
78-
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) is completed.
78+
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) is completed.
7979
#[serde(rename = "thread.run.step.completed")]
8080
ThreadRunStepCompleted(RunStepObject),
81-
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) fails.
81+
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) fails.
8282
#[serde(rename = "thread.run.step.failed")]
8383
ThreadRunStepFailed(RunStepObject),
84-
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) is cancelled.
84+
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) is cancelled.
8585
#[serde(rename = "thread.run.step.cancelled")]
8686
ThreadRunStepCancelled(RunStepObject),
87-
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/runs/step-object) expires.
87+
/// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) expires.
8888
#[serde(rename = "thread.run.step.expired")]
8989
ThreadRunStepExpired(RunStepObject),
9090
/// Occurs when a [message](https://platform.openai.com/docs/api-reference/messages/object) is created.

0 commit comments

Comments
 (0)