Skip to content

Commit f4286c0

Browse files
committed
feat: Add responses API
Adds support for the OpenAI responses API
1 parent aeb6d1f commit f4286c0

File tree

8 files changed

+1175
-19
lines changed

8 files changed

+1175
-19
lines changed

async-openai/src/client.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
moderation::Moderations,
1515
traits::AsyncTryFrom,
1616
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
17-
Models, Projects, Threads, Uploads, Users, VectorStores,
17+
Models, Projects, Responses, Threads, Uploads, Users, VectorStores,
1818
};
1919

2020
#[derive(Debug, Clone, Default)]
@@ -162,6 +162,11 @@ impl<C: Config> Client<C> {
162162
Projects::new(self)
163163
}
164164

165+
/// To call [Responses] group related APIs using this client.
166+
pub fn responses(&self) -> Responses<C> {
167+
Responses::new(self)
168+
}
169+
165170
pub fn config(&self) -> &C {
166171
&self.config
167172
}
@@ -341,7 +346,12 @@ impl<C: Config> Client<C> {
341346
let message: String = String::from_utf8_lossy(&bytes).into_owned();
342347
tracing::warn!("Server error: {status} - {message}");
343348
return Err(backoff::Error::Transient {
344-
err: OpenAIError::ApiError(ApiError { message, r#type: None, param: None, code: None }),
349+
err: OpenAIError::ApiError(ApiError {
350+
message,
351+
r#type: None,
352+
param: None,
353+
code: None,
354+
}),
345355
retry_after: None,
346356
});
347357
}

async-openai/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ mod project_api_keys;
146146
mod project_service_accounts;
147147
mod project_users;
148148
mod projects;
149+
mod responses;
149150
mod runs;
150151
mod steps;
151152
mod threads;
@@ -177,6 +178,7 @@ pub use project_api_keys::ProjectAPIKeys;
177178
pub use project_service_accounts::ProjectServiceAccounts;
178179
pub use project_users::ProjectUsers;
179180
pub use projects::Projects;
181+
pub use responses::Responses;
180182
pub use runs::Runs;
181183
pub use steps::Steps;
182184
pub use threads::Threads;

async-openai/src/responses.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::{
2+
config::Config,
3+
error::OpenAIError,
4+
types::{CreateResponseRequest, CreateResponseResponse},
5+
Client,
6+
};
7+
8+
/// Given text input or a list of context items, the model will generate a response.
9+
///
10+
/// Related guide: [Responses API](https://platform.openai.com/docs/guides/responses)
11+
pub struct Responses<'c, C: Config> {
12+
client: &'c Client<C>,
13+
}
14+
15+
impl<'c, C: Config> Responses<'c, C> {
16+
/// Constructs a new Responses client.
17+
pub fn new(client: &'c Client<C>) -> Self {
18+
Self { client }
19+
}
20+
21+
/// Creates a model response for the given input.
22+
#[crate::byot(
23+
T0 = serde::Serialize,
24+
R = serde::de::DeserializeOwned
25+
)]
26+
pub async fn create(
27+
&self,
28+
request: CreateResponseRequest,
29+
) -> Result<CreateResponseResponse, OpenAIError> {
30+
self.client.post("/responses", request).await
31+
}
32+
}

async-openai/src/types/impls.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,7 @@ use crate::{
1313

1414
use bytes::Bytes;
1515

16-
use super::{
17-
AddUploadPartRequest, AudioInput, AudioResponseFormat, ChatCompletionFunctionCall,
18-
ChatCompletionFunctions, ChatCompletionNamedToolChoice, ChatCompletionRequestAssistantMessage,
19-
ChatCompletionRequestAssistantMessageContent, ChatCompletionRequestDeveloperMessage,
20-
ChatCompletionRequestDeveloperMessageContent, ChatCompletionRequestFunctionMessage,
21-
ChatCompletionRequestMessage, ChatCompletionRequestMessageContentPartAudio,
22-
ChatCompletionRequestMessageContentPartImage, ChatCompletionRequestMessageContentPartText,
23-
ChatCompletionRequestSystemMessage, ChatCompletionRequestSystemMessageContent,
24-
ChatCompletionRequestToolMessage, ChatCompletionRequestToolMessageContent,
25-
ChatCompletionRequestUserMessage, ChatCompletionRequestUserMessageContent,
26-
ChatCompletionRequestUserMessageContentPart, ChatCompletionToolChoiceOption, CreateFileRequest,
27-
CreateImageEditRequest, CreateImageVariationRequest, CreateMessageRequestContent,
28-
CreateSpeechResponse, CreateTranscriptionRequest, CreateTranslationRequest, DallE2ImageSize,
29-
EmbeddingInput, FileInput, FilePurpose, FunctionName, Image, ImageInput, ImageModel,
30-
ImageResponseFormat, ImageSize, ImageUrl, ImagesResponse, ModerationInput, Prompt, Role, Stop,
31-
TimestampGranularity,
32-
};
16+
use super::{AddUploadPartRequest, AudioInput, AudioResponseFormat, ChatCompletionFunctionCall, ChatCompletionFunctions, ChatCompletionNamedToolChoice, ChatCompletionRequestAssistantMessage, ChatCompletionRequestAssistantMessageContent, ChatCompletionRequestDeveloperMessage, ChatCompletionRequestDeveloperMessageContent, ChatCompletionRequestFunctionMessage, ChatCompletionRequestMessage, ChatCompletionRequestMessageContentPartAudio, ChatCompletionRequestMessageContentPartImage, ChatCompletionRequestMessageContentPartText, ChatCompletionRequestSystemMessage, ChatCompletionRequestSystemMessageContent, ChatCompletionRequestToolMessage, ChatCompletionRequestToolMessageContent, ChatCompletionRequestUserMessage, ChatCompletionRequestUserMessageContent, ChatCompletionRequestUserMessageContentPart, ChatCompletionToolChoiceOption, CreateFileRequest, CreateImageEditRequest, CreateImageVariationRequest, CreateMessageRequestContent, CreateSpeechResponse, CreateTranscriptionRequest, CreateTranslationRequest, DallE2ImageSize, EmbeddingInput, FileInput, FilePurpose, FunctionName, Image, ImageInput, ImageModel, ImageResponseFormat, ImageSize, ImageUrl, ImagesResponse, InputContent, ModerationInput, Prompt, ResponseInput, ResponsesRole, Role, Stop, TimestampGranularity};
3317

3418
/// for `impl_from!(T, Enum)`, implements
3519
/// - `From<T>`
@@ -987,3 +971,46 @@ impl AsyncTryFrom<AddUploadPartRequest> for reqwest::multipart::Form {
987971
}
988972

989973
// end: types to multipart form
974+
975+
impl Default for ResponseInput {
976+
fn default() -> Self {
977+
Self::Text("".to_string())
978+
}
979+
}
980+
981+
impl Default for InputContent {
982+
fn default() -> Self {
983+
Self::TextInput("".to_string())
984+
}
985+
}
986+
987+
988+
impl From<String> for ResponseInput {
989+
fn from(value: String) -> Self {
990+
ResponseInput::Text(value)
991+
}
992+
}
993+
994+
impl From<&str> for ResponseInput {
995+
fn from(value: &str) -> Self {
996+
ResponseInput::Text(value.to_owned())
997+
}
998+
}
999+
1000+
impl Default for ResponsesRole {
1001+
fn default() -> Self {
1002+
Self::User
1003+
}
1004+
}
1005+
1006+
impl From<String> for InputContent {
1007+
fn from(value: String) -> Self {
1008+
Self::TextInput(value)
1009+
}
1010+
}
1011+
1012+
impl From<&str> for InputContent {
1013+
fn from(value: &str) -> Self {
1014+
Self::TextInput(value.to_owned())
1015+
}
1016+
}

async-openai/src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod projects;
2424
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
2525
#[cfg(feature = "realtime")]
2626
pub mod realtime;
27+
mod responses;
2728
mod run;
2829
mod step;
2930
mod thread;
@@ -51,6 +52,7 @@ pub use project_api_key::*;
5152
pub use project_service_account::*;
5253
pub use project_users::*;
5354
pub use projects::*;
55+
pub use responses::*;
5456
pub use run::*;
5557
pub use step::*;
5658
pub use thread::*;

0 commit comments

Comments
 (0)