Skip to content

Commit 50ad45e

Browse files
authored
feat: upstream spec sync + all chat completions apis (#469)
* updates to vector store api and types * types::vectorstores * examples updates for types::vectorstores * types::videos * examples for types::videos * types::containers * examples for types::containers * updates to chat completions types * updates * fix example * fix examples compilation * types::chat * fix examples for types::chat * remaining chat completion apis * update chat-store example to run all the new apis * video example
1 parent 419f465 commit 50ad45e

File tree

49 files changed

+870
-450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+870
-450
lines changed

async-openai/src/chat.rs

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
use serde::Serialize;
2+
13
use crate::{
24
config::Config,
35
error::OpenAIError,
4-
types::{
6+
types::chat::{
7+
ChatCompletionDeleted, ChatCompletionList, ChatCompletionMessageList,
58
ChatCompletionResponseStream, CreateChatCompletionRequest, CreateChatCompletionResponse,
9+
UpdateChatCompletionRequest,
610
},
711
Client,
812
};
913

1014
/// Given a list of messages comprising a conversation, the model will return a response.
1115
///
12-
/// Related guide: [Chat completions](https://platform.openai.com//docs/guides/text-generation)
16+
/// Related guide: [Chat Completions](https://platform.openai.com/docs/guides/text-generation)
1317
pub struct Chat<'c, C: Config> {
1418
client: &'c Client<C>,
1519
}
@@ -19,21 +23,13 @@ impl<'c, C: Config> Chat<'c, C> {
1923
Self { client }
2024
}
2125

22-
/// Creates a model response for the given chat conversation. Learn more in
23-
/// the
24-
///
25-
/// [text generation](https://platform.openai.com/docs/guides/text-generation),
26-
/// [vision](https://platform.openai.com/docs/guides/vision),
27-
///
28-
/// and [audio](https://platform.openai.com/docs/guides/audio) guides.
26+
/// Creates a model response for the given chat conversation.
2927
///
28+
/// Returns a [chat completion](https://platform.openai.com/docs/api-reference/chat/object) object, or a streamed sequence of [chat completion chunk](https://platform.openai.com/docs/api-reference/chat/streaming) objects if the request is streamed.
3029
///
31-
/// Parameter support can differ depending on the model used to generate the
32-
/// response, particularly for newer reasoning models. Parameters that are
33-
/// only supported for reasoning models are noted below. For the current state
34-
/// of unsupported parameters in reasoning models,
30+
/// Learn more in the [text generation](https://platform.openai.com/docs/guides/text-generation), [vision](https://platform.openai.com/docs/guides/vision), and [audio](https://platform.openai.com/docs/guides/audio) guides.
3531
///
36-
/// [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning).
32+
/// Parameter support can differ depending on the model used to generate the response, particularly for newer reasoning models. Parameters that are only supported for reasoning models are noted below. For the current state of unsupported parameters in reasoning models, [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning).
3733
///
3834
/// byot: You must ensure "stream: false" in serialized `request`
3935
#[crate::byot(
@@ -55,9 +51,11 @@ impl<'c, C: Config> Chat<'c, C> {
5551
self.client.post("/chat/completions", request).await
5652
}
5753

58-
/// Creates a completion for the chat message
54+
/// Creates a completion for the chat message.
5955
///
60-
/// partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message.
56+
/// If set to true, the model response data will be streamed to the client as it is generated using [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format).
57+
///
58+
/// See the [Streaming section](https://platform.openai.com/docs/api-reference/chat/streaming) for more information, along with the [streaming responses](https://platform.openai.com/docs/guides/streaming-responses) guide for more information on how to handle the streaming events.
6159
///
6260
/// [ChatCompletionResponseStream] is a parsed SSE stream until a \[DONE\] is received from server.
6361
///
@@ -85,4 +83,73 @@ impl<'c, C: Config> Chat<'c, C> {
8583
}
8684
Ok(self.client.post_stream("/chat/completions", request).await)
8785
}
86+
87+
/// List stored Chat Completions. Only Chat Completions that have been stored
88+
/// with the `store` parameter set to `true` will be returned.
89+
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
90+
pub async fn list<Q>(&self, query: &Q) -> Result<ChatCompletionList, OpenAIError>
91+
where
92+
Q: Serialize + ?Sized,
93+
{
94+
self.client
95+
.get_with_query("/chat/completions", &query)
96+
.await
97+
}
98+
99+
/// Get a stored chat completion. Only Chat Completions that have been created
100+
/// with the `store` parameter set to `true` will be returned.
101+
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
102+
pub async fn retrieve(
103+
&self,
104+
completion_id: &str,
105+
) -> Result<CreateChatCompletionResponse, OpenAIError> {
106+
self.client
107+
.get(&format!("/chat/completions/{completion_id}"))
108+
.await
109+
}
110+
111+
/// Modify a stored chat completion. Only Chat Completions that have been
112+
/// created with the `store` parameter set to `true` can be modified. Currently,
113+
/// the only supported modification is to update the `metadata` field.
114+
#[crate::byot(
115+
T0 = std::fmt::Display,
116+
T1 = serde::Serialize,
117+
R = serde::de::DeserializeOwned
118+
)]
119+
pub async fn update(
120+
&self,
121+
completion_id: &str,
122+
request: UpdateChatCompletionRequest,
123+
) -> Result<CreateChatCompletionResponse, OpenAIError> {
124+
self.client
125+
.post(&format!("/chat/completions/{completion_id}"), request)
126+
.await
127+
}
128+
129+
/// Delete a stored chat completion. Only Chat Completions that have been
130+
/// created with the `store` parameter set to `true` can be deleted.
131+
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
132+
pub async fn delete(&self, completion_id: &str) -> Result<ChatCompletionDeleted, OpenAIError> {
133+
self.client
134+
.delete(&format!("/chat/completions/{completion_id}"))
135+
.await
136+
}
137+
138+
/// Get a list of messages for the specified chat completion.
139+
#[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
140+
pub async fn messages<Q>(
141+
&self,
142+
completion_id: &str,
143+
query: &Q,
144+
) -> Result<ChatCompletionMessageList, OpenAIError>
145+
where
146+
Q: Serialize + ?Sized,
147+
{
148+
self.client
149+
.get_with_query(
150+
&format!("/chat/completions/{completion_id}/messages"),
151+
&query,
152+
)
153+
.await
154+
}
88155
}

async-openai/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl Config for AzureConfig {
241241
#[cfg(test)]
242242
mod test {
243243
use super::*;
244-
use crate::types::{
244+
use crate::types::chat::{
245245
ChatCompletionRequestMessage, ChatCompletionRequestUserMessage, CreateChatCompletionRequest,
246246
};
247247
use crate::Client;

async-openai/src/container_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::Serialize;
44
use crate::{
55
config::Config,
66
error::OpenAIError,
7-
types::{
7+
types::containers::{
88
ContainerFileListResource, ContainerFileResource, CreateContainerFileRequest,
99
DeleteContainerFileResponse,
1010
},

async-openai/src/containers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
config::Config,
55
container_files::ContainerFiles,
66
error::OpenAIError,
7-
types::{
7+
types::containers::{
88
ContainerListResource, ContainerResource, CreateContainerRequest, DeleteContainerResponse,
99
},
1010
Client,

async-openai/src/types/assistant.rs

Lines changed: 1 addition & 1 deletion
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, ResponseFormat};
8+
use crate::types::chat::{FunctionName, FunctionObject, ResponseFormat};
99

1010
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
1111
pub struct AssistantToolCodeInterpreterResources {

async-openai/src/types/assistant_impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use super::{
2-
AssistantToolCodeInterpreterResources, AssistantToolFileSearchResources,
1+
use crate::types::{
2+
chat::FunctionObject, AssistantToolCodeInterpreterResources, AssistantToolFileSearchResources,
33
AssistantToolResources, AssistantTools, AssistantToolsFileSearch, AssistantToolsFunction,
4-
CreateAssistantToolFileSearchResources, CreateAssistantToolResources, FunctionObject,
4+
CreateAssistantToolFileSearchResources, CreateAssistantToolResources,
55
};
66

77
impl From<AssistantToolsFileSearch> for AssistantTools {

0 commit comments

Comments
 (0)