Skip to content

Commit

Permalink
feat: add runs apis
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Nov 20, 2024
1 parent b72c61d commit 03c7f45
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
1 change: 1 addition & 0 deletions rs_openai/src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pub mod images;
pub mod messages;
pub mod models;
pub mod moderations;
pub mod runs;
pub mod threads;
pub mod uploads;
57 changes: 57 additions & 0 deletions rs_openai/src/apis/runs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Create runs that assistants can interact with.
//!
//! Related guide: [Assistants](https://platform.openai.com/docs/assistants/overview)
use crate::client::OpenAI;
use crate::interfaces::runs;
use crate::shared::response_wrapper::OpenAIResponse;

pub struct Runs<'a> {
openai: &'a OpenAI,
}

impl<'a> Runs<'a> {
pub fn new(openai: &'a OpenAI) -> Self {
Self { openai }
}

/// Create a run.
///
/// # Path parameters
///
/// - `thread_id` - The ID of the thread to run.
pub async fn create(
&self,
thread_id: &str,
req: &runs::CreateRunRequest,
) -> OpenAIResponse<runs::RunResponse> {
self.openai
.post(&format!("/threads/{thread_id}/runs"), req)
.await
}

/// Retrieves a thread.
pub async fn retrieve(
&self,
thread_id: &str, // The thread object matching the specified ID.
) -> OpenAIResponse<runs::RunResponse> {
self.openai.get(&format!("/runs/{thread_id}"), &()).await
}

/// Modifies a thread.
pub async fn modify(
&self,
thread_id: &str, // The ID of the thread to modify. Only the `metadata` can be modified.
req: &runs::ModifyRunRequest,
) -> OpenAIResponse<runs::RunResponse> {
self.openai.post(&format!("/runs/{thread_id}"), req).await
}

/// Delete a thread.
pub async fn delete(
&self,
thread_id: &str, // The ID of the thread to modify. Only the `metadata` can be modified.
) -> OpenAIResponse<runs::DeleteRunResponse> {
self.openai.delete(&format!("/runs/{thread_id}"), &()).await
}
}
1 change: 1 addition & 0 deletions rs_openai/src/interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pub mod images;
pub mod messages;
pub mod models;
pub mod moderations;
pub mod runs;
pub mod threads;
pub mod uploads;
162 changes: 162 additions & 0 deletions rs_openai/src/interfaces/runs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use super::assistants::ToolResources;
use crate::shared::response_wrapper::OpenAIError;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Builder, Clone, Debug, Default, Serialize)]
#[builder(name = "CreateRunRequestBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateRunRequest {
/// A list of additional fields to include in the response. Currently the only supported value is `step_details.tool_calls[*].file_search.results[*].content` to fetch the file search result content.
/// See the [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search/customizing-file-search-settings) for more information.
#[serde(skip_serializing_if = "Option::is_none")]
pub includes: Option<Vec<String>>,
}

#[derive(Builder, Clone, Debug, Serialize)]
pub struct Message {
/// The role of the entity that is creating the message. Allowed values include:
/// - `user`: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
/// - `assistant`: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.
pub role: String,
pub content: Content,
/// A list of files attached to the message, and the tools they should be added to.
pub attachments: Option<Vec<Attachment>>,
/// Set of 16 key-value pairs that can be attached to an object.
/// This can be useful for storing additional information about the object in a structured format.
/// Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Serialize, Clone, strum::Display)]
pub enum Content {
/// The text contents of the message.
TextContent(String),
/// An array of content parts with a defined type, each can be of type `text` or images can be passed with `image_url` or `image_file`.
/// Image types are only supported on [Vision-compatible models](https://platform.openai.com/docs/models/overview).
ArrayOfContentParts(Vec<ArrayOfContentParts>),
}

#[derive(Debug, Serialize, Clone, strum::Display)]
pub enum ArrayOfContentParts {
/// References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message.
ImageFileType(ImageFileType),
/// References an image URL in the content of a message.
ImageUrlType(ImageUrlType),
/// The text content that is part of a message.
TextType(TextType),
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct ImageFileType {
/// Always `image_file`.
pub r#type: String,
pub image_file: ImageFile,
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct ImageFile {
/// The [File](https://platform.openai.com/docs/api-reference/files) ID of the image in the message content. Set `purpose="vision"` when uploading the File if you need to later display the file content.
pub file_id: String,

/// Specifies the detail level of the image if specified by the user. `low` uses fewer tokens, you can opt in to high resolution using `high`.
pub detail: Option<String>, // Defaults to auto
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct ImageUrlType {
/// The type of the content part.
pub r#type: String,
pub image_url: ImageUrl,
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct ImageUrl {
/// The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp.
pub url: String,
/// Specifies the detail level of the image if specified by the user. `low` uses fewer tokens, you can opt in to high resolution using `high`. Defaults to auto
pub detail: Option<String>, // Defaults to auto
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct TextType {
/// Always `text`.
pub r#type: String,
/// Text content to be sent to the model
pub text: String,
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct Attachment {
/// The ID of the file to attach to the message.
pub file_id: Option<String>,
/// The tools to add this file to.
pub tools: Option<Vec<Tool>>,
}

#[derive(Debug, Serialize, Clone, strum::Display)]
pub enum Tool {
CodeInterpreterTool(CodeInterpreterTool),
FileSearchTool(FileSearchTool),
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct CodeInterpreterTool {
/// The type of tool being defined: `code_interpreter`
pub r#type: String,
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
pub struct FileSearchTool {
/// The type of tool being defined: `file_search`
pub r#type: String,
}

/// Represents a thread that contains [messages](https://platform.openai.com/docs/api-reference/messages).
#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct RunResponse {
/// The identifier, which can be referenced in API endpoints.
pub id: String,
/// The object type, which is always `thread`.
pub object: String,
/// The Unix timestamp (in seconds) for when the thread was created.
pub created_at: u64,
/// Set of 16 key-value pairs that can be attached to an object.
/// This can be useful for storing additional information about the object in a structured format.
/// Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.
pub metadata: HashMap<String, serde_json::Value>,
/// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool.
/// For example, the code_interpreter tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs.
pub tool_resources: Option<ToolResources>,
}

#[derive(Builder, Clone, Debug, Default, Serialize)]
#[builder(name = "ModifyRunRequestBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct ModifyRunRequest {
/// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool.
/// For example, the code_interpreter tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs.
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_resources: Option<ToolResources>,

/// Set of 16 key-value pairs that can be attached to an object.
/// This can be useful for storing additional information about the object in a structured format.
/// Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct DeleteRunResponse {
pub id: String,
pub object: String,
pub delete: bool,
}

0 comments on commit 03c7f45

Please sign in to comment.