Skip to content

Commit 7287fbc

Browse files
committed
remaining chat completion apis
1 parent d828257 commit 7287fbc

3 files changed

Lines changed: 149 additions & 1 deletion

File tree

async-openai/src/chat.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use serde::Serialize;
2+
13
use crate::{
24
config::Config,
35
error::OpenAIError,
46
types::chat::{
7+
ChatCompletionDeleted, ChatCompletionList, ChatCompletionMessageList,
58
ChatCompletionResponseStream, CreateChatCompletionRequest, CreateChatCompletionResponse,
9+
UpdateChatCompletionRequest,
610
},
711
Client,
812
};
@@ -79,4 +83,73 @@ impl<'c, C: Config> Chat<'c, C> {
7983
}
8084
Ok(self.client.post_stream("/chat/completions", request).await)
8185
}
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+
}
82155
}

async-openai/src/types/chat/chat.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,3 +1316,78 @@ pub struct CreateChatCompletionStreamResponse {
13161316
/// When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request.
13171317
pub usage: Option<CompletionUsage>,
13181318
}
1319+
1320+
/// An object representing a list of Chat Completions.
1321+
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
1322+
pub struct ChatCompletionList {
1323+
/// The type of this object. It is always set to "list".
1324+
pub object: String,
1325+
/// An array of chat completion objects.
1326+
pub data: Vec<CreateChatCompletionResponse>,
1327+
/// The identifier of the first chat completion in the data array.
1328+
pub first_id: String,
1329+
/// The identifier of the last chat completion in the data array.
1330+
pub last_id: String,
1331+
/// Indicates whether there are more Chat Completions available.
1332+
pub has_more: bool,
1333+
}
1334+
1335+
/// Response when deleting a chat completion.
1336+
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
1337+
pub struct ChatCompletionDeleted {
1338+
/// The type of object being deleted.
1339+
pub object: String,
1340+
/// The ID of the chat completion that was deleted.
1341+
pub id: String,
1342+
/// Whether the chat completion was deleted.
1343+
pub deleted: bool,
1344+
}
1345+
1346+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
1347+
#[serde(tag = "type")]
1348+
#[serde(rename_all = "snake_case")]
1349+
1350+
pub enum ContentPart {
1351+
Text(ChatCompletionRequestMessageContentPartText),
1352+
ImageUrl(ChatCompletionRequestMessageContentPartImage),
1353+
}
1354+
1355+
/// A chat completion message with additional fields for listing.
1356+
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
1357+
pub struct ChatCompletionMessageListItem {
1358+
/// The identifier of the chat message.
1359+
pub id: String,
1360+
/// If a content parts array was provided, this is an array of `text` and `image_url` parts. Otherwise, null.
1361+
#[serde(skip_serializing_if = "Option::is_none")]
1362+
pub content_parts: Option<Vec<ContentPart>>,
1363+
1364+
#[serde(flatten)]
1365+
pub message: ChatCompletionResponseMessage,
1366+
}
1367+
1368+
/// An object representing a list of chat completion messages.
1369+
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
1370+
pub struct ChatCompletionMessageList {
1371+
/// The type of this object. It is always set to "list".
1372+
pub object: String,
1373+
/// An array of chat completion message objects.
1374+
pub data: Vec<ChatCompletionMessageListItem>,
1375+
/// The identifier of the first chat message in the data array.
1376+
pub first_id: String,
1377+
/// The identifier of the last chat message in the data array.
1378+
pub last_id: String,
1379+
/// Indicates whether there are more chat messages available.
1380+
pub has_more: bool,
1381+
}
1382+
1383+
/// Request to update a chat completion.
1384+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)]
1385+
#[builder(name = "UpdateChatCompletionRequestArgs")]
1386+
#[builder(pattern = "mutable")]
1387+
#[builder(setter(into, strip_option), default)]
1388+
#[builder(derive(Debug))]
1389+
#[builder(build_fn(error = "OpenAIError"))]
1390+
pub struct UpdateChatCompletionRequest {
1391+
/// Set of 16 key-value pairs that can be attached to an object.
1392+
pub metadata: Metadata,
1393+
}

async-openai/src/types/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, path::PathBuf};
1+
use std::path::PathBuf;
22

33
use bytes::Bytes;
44
use serde::{Deserialize, Serialize};

0 commit comments

Comments
 (0)