pub struct ChatCompletion { /* private fields */ }
Expand description
OpenAI Chat Completions API client
This structure manages interactions with the OpenAI Chat Completions API. It handles API key management, request parameter configuration, and API calls.
§Example
use openai_tools::chat::request::ChatCompletion;
use openai_tools::common::message::Message;
use openai_tools::common::role::Role;
let mut chat = ChatCompletion::new();
let messages = vec![Message::from_string(Role::User, "Hello!")];
let response = chat
.model_id("gpt-4o-mini")
.messages(messages)
.temperature(1.0)
.chat()
.await?;
Implementations§
Source§impl ChatCompletion
impl ChatCompletion
Sourcepub fn frequency_penalty(&mut self, frequency_penalty: f32) -> &mut Self
pub fn frequency_penalty(&mut self, frequency_penalty: f32) -> &mut Self
Sourcepub fn top_logprobs(&mut self, top_logprobs: u8) -> &mut Self
pub fn top_logprobs(&mut self, top_logprobs: u8) -> &mut Self
Sourcepub fn max_completion_tokens(&mut self, max_completion_tokens: u64) -> &mut Self
pub fn max_completion_tokens(&mut self, max_completion_tokens: u64) -> &mut Self
Sourcepub fn modalities<T: AsRef<str>>(&mut self, modalities: Vec<T>) -> &mut Self
pub fn modalities<T: AsRef<str>>(&mut self, modalities: Vec<T>) -> &mut Self
Sourcepub fn presence_penalty(&mut self, presence_penalty: f32) -> &mut Self
pub fn presence_penalty(&mut self, presence_penalty: f32) -> &mut Self
Sets the presence penalty
A parameter that controls the tendency to include new content in the document. Positive values encourage talking about new topics, negative values encourage staying on existing topics.
§Arguments
presence_penalty
- Presence penalty value (range: -2.0 to 2.0)
§Returns
A mutable reference to self for method chaining
Sourcepub fn temperature(&mut self, temperature: f32) -> &mut Self
pub fn temperature(&mut self, temperature: f32) -> &mut Self
Sets the temperature parameter to control response randomness
Higher values (e.g., 1.0) produce more creative and diverse outputs, while lower values (e.g., 0.2) produce more deterministic and consistent outputs.
§Arguments
temperature
- Temperature parameter (range: 0.0 to 2.0)
§Returns
A mutable reference to self for method chaining
Sourcepub fn json_schema(&mut self, json_schema: Schema) -> &mut Self
pub fn json_schema(&mut self, json_schema: Schema) -> &mut Self
Sourcepub fn tools(&mut self, tools: Vec<Tool>) -> &mut Self
pub fn tools(&mut self, tools: Vec<Tool>) -> &mut Self
Sets the tools that can be called by the model
Enables function calling by providing a list of tools that the model can choose to call. When tools are provided, the model may generate tool calls instead of or in addition to regular text responses.
§Arguments
tools
- Vector of tools available for the model to use
§Returns
A mutable reference to self for method chaining
Sourcepub fn get_message_history(&self) -> Vec<Message>
pub fn get_message_history(&self) -> Vec<Message>
Sourcepub async fn chat(&mut self) -> Result<Response>
pub async fn chat(&mut self) -> Result<Response>
Sends the chat completion request to OpenAI API
This method validates the request parameters, constructs the HTTP request, and sends it to the OpenAI Chat Completions endpoint.
§Returns
A Result
containing the API response on success, or an error on failure.
§Errors
Returns an error if:
- API key is not set
- Model ID is not set
- Messages are empty
- Network request fails
- Response parsing fails
§Example
use openai_tools::chat::request::ChatCompletion;
use openai_tools::common::message::Message;
use openai_tools::common::role::Role;
let mut chat = ChatCompletion::new();
let messages = vec![Message::from_string(Role::User, "Hello!")];
let response = chat
.model_id("gpt-4o-mini")
.messages(messages)
.temperature(1.0)
.chat()
.await?;
println!("{}", response.choices[0].message.content.as_ref().unwrap().text.as_ref().unwrap());
Trait Implementations§
Source§impl Clone for ChatCompletion
impl Clone for ChatCompletion
Source§fn clone(&self) -> ChatCompletion
fn clone(&self) -> ChatCompletion
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ChatCompletion
impl Debug for ChatCompletion
Source§impl Default for ChatCompletion
impl Default for ChatCompletion
Source§fn default() -> ChatCompletion
fn default() -> ChatCompletion
Source§impl<'de> Deserialize<'de> for ChatCompletion
impl<'de> Deserialize<'de> for ChatCompletion
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for ChatCompletion
impl RefUnwindSafe for ChatCompletion
impl Send for ChatCompletion
impl Sync for ChatCompletion
impl Unpin for ChatCompletion
impl UnwindSafe for ChatCompletion
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.