pub struct Conversations { /* private fields */ }Expand description
Client for interacting with the OpenAI Conversations API.
This struct provides methods to create, retrieve, update, delete conversations,
and manage conversation items. Use Conversations::new() to create a new instance.
§Example
use openai_tools::conversations::request::Conversations;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
// Create a conversation with metadata
let mut metadata = HashMap::new();
metadata.insert("user_id".to_string(), "user123".to_string());
let conv = conversations.create(Some(metadata), None).await?;
println!("Created: {}", conv.id);
// Retrieve the conversation
let retrieved = conversations.retrieve(&conv.id).await?;
println!("Retrieved: {:?}", retrieved.metadata);
Ok(())
}Implementations§
Source§impl Conversations
impl Conversations
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new Conversations client for OpenAI API.
Initializes the client by loading the OpenAI API key from
the environment variable OPENAI_API_KEY. Supports .env file loading
via dotenvy.
§Returns
Ok(Conversations)- A new Conversations client ready for useErr(OpenAIToolError)- If the API key is not found in the environment
§Example
use openai_tools::conversations::request::Conversations;
let conversations = Conversations::new().expect("API key should be set");Sourcepub fn with_auth(auth: AuthProvider) -> Self
pub fn with_auth(auth: AuthProvider) -> Self
Creates a new Conversations client with a custom authentication provider
Sourcepub fn detect_provider() -> Result<Self>
pub fn detect_provider() -> Result<Self>
Creates a new Conversations client by auto-detecting the provider
Sourcepub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self
pub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self
Creates a new Conversations client with URL-based provider detection
Sourcepub fn from_url<S: Into<String>>(url: S) -> Result<Self>
pub fn from_url<S: Into<String>>(url: S) -> Result<Self>
Creates a new Conversations client from URL using environment variables
Sourcepub fn auth(&self) -> &AuthProvider
pub fn auth(&self) -> &AuthProvider
Returns the authentication provider
Sourcepub async fn create(
&self,
metadata: Option<HashMap<String, String>>,
items: Option<Vec<InputItem>>,
) -> Result<Conversation>
pub async fn create( &self, metadata: Option<HashMap<String, String>>, items: Option<Vec<InputItem>>, ) -> Result<Conversation>
Creates a new conversation.
You can optionally provide metadata and initial items to include in the conversation.
§Arguments
metadata- Optional key-value pairs for storing additional informationitems- Optional initial items to add to the conversation (up to 20 items)
§Returns
Ok(Conversation)- The created conversation objectErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::conversations::request::Conversations;
use openai_tools::conversations::response::InputItem;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
// Create with metadata and initial message
let mut metadata = HashMap::new();
metadata.insert("topic".to_string(), "greeting".to_string());
let items = vec![InputItem::user_message("Hello!")];
let conv = conversations.create(Some(metadata), Some(items)).await?;
println!("Created conversation: {}", conv.id);
Ok(())
}Sourcepub async fn retrieve(&self, conversation_id: &str) -> Result<Conversation>
pub async fn retrieve(&self, conversation_id: &str) -> Result<Conversation>
Retrieves a specific conversation.
§Arguments
conversation_id- The ID of the conversation to retrieve
§Returns
Ok(Conversation)- The conversation objectErr(OpenAIToolError)- If the conversation is not found or the request fails
§Example
use openai_tools::conversations::request::Conversations;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
let conv = conversations.retrieve("conv_abc123").await?;
println!("Conversation: {}", conv.id);
println!("Created at: {}", conv.created_at);
Ok(())
}Sourcepub async fn update(
&self,
conversation_id: &str,
metadata: HashMap<String, String>,
) -> Result<Conversation>
pub async fn update( &self, conversation_id: &str, metadata: HashMap<String, String>, ) -> Result<Conversation>
Updates a conversation’s metadata.
§Arguments
conversation_id- The ID of the conversation to updatemetadata- The new metadata to set
§Returns
Ok(Conversation)- The updated conversation objectErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::conversations::request::Conversations;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
let mut metadata = HashMap::new();
metadata.insert("topic".to_string(), "updated-topic".to_string());
let conv = conversations.update("conv_abc123", metadata).await?;
println!("Updated: {:?}", conv.metadata);
Ok(())
}Sourcepub async fn delete(
&self,
conversation_id: &str,
) -> Result<DeleteConversationResponse>
pub async fn delete( &self, conversation_id: &str, ) -> Result<DeleteConversationResponse>
Deletes a conversation.
§Arguments
conversation_id- The ID of the conversation to delete
§Returns
Ok(DeleteConversationResponse)- Confirmation of deletionErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::conversations::request::Conversations;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
let result = conversations.delete("conv_abc123").await?;
if result.deleted {
println!("Conversation {} was deleted", result.id);
}
Ok(())
}Sourcepub async fn create_items(
&self,
conversation_id: &str,
items: Vec<InputItem>,
) -> Result<ConversationItemListResponse>
pub async fn create_items( &self, conversation_id: &str, items: Vec<InputItem>, ) -> Result<ConversationItemListResponse>
Creates items in a conversation.
You can add up to 20 items at a time.
§Arguments
conversation_id- The ID of the conversationitems- The items to add to the conversation
§Returns
Ok(ConversationItemListResponse)- The created itemsErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::conversations::request::Conversations;
use openai_tools::conversations::response::InputItem;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
let items = vec![
InputItem::user_message("What is the weather like?"),
InputItem::assistant_message("I'd be happy to help with weather information!"),
];
let result = conversations.create_items("conv_abc123", items).await?;
println!("Added {} items", result.data.len());
Ok(())
}Sourcepub async fn list_items(
&self,
conversation_id: &str,
limit: Option<u32>,
after: Option<&str>,
order: Option<&str>,
include: Option<Vec<ConversationInclude>>,
) -> Result<ConversationItemListResponse>
pub async fn list_items( &self, conversation_id: &str, limit: Option<u32>, after: Option<&str>, order: Option<&str>, include: Option<Vec<ConversationInclude>>, ) -> Result<ConversationItemListResponse>
Lists items in a conversation.
§Arguments
conversation_id- The ID of the conversationlimit- Maximum number of items to return (1-100, default 20)after- Cursor for pagination (item ID to start after)order- Sort order (“asc” or “desc”, default “desc”)include- Additional data to include in the response
§Returns
Ok(ConversationItemListResponse)- The list of itemsErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::conversations::request::{Conversations, ConversationInclude};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
// List items with pagination
let items = conversations.list_items(
"conv_abc123",
Some(20),
None,
Some("desc"),
None,
).await?;
for item in &items.data {
println!("Item: {} ({})", item.id, item.item_type);
}
Ok(())
}Sourcepub async fn list(
&self,
limit: Option<u32>,
after: Option<&str>,
) -> Result<ConversationListResponse>
pub async fn list( &self, limit: Option<u32>, after: Option<&str>, ) -> Result<ConversationListResponse>
Lists all conversations (if available).
Note: This endpoint may not be available in all API versions.
§Arguments
limit- Maximum number of conversations to return (1-100, default 20)after- Cursor for pagination (conversation ID to start after)
§Returns
Ok(ConversationListResponse)- The list of conversationsErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::conversations::request::Conversations;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conversations = Conversations::new()?;
let response = conversations.list(Some(10), None).await?;
for conv in &response.data {
println!("Conversation: {} (created: {})", conv.id, conv.created_at);
}
Ok(())
}Auto Trait Implementations§
impl Freeze for Conversations
impl RefUnwindSafe for Conversations
impl Send for Conversations
impl Sync for Conversations
impl Unpin for Conversations
impl UnwindSafe for Conversations
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more