Struct Client

Source
pub struct Client {
    pub api_key: String,
    pub endpoint: String,
    pub max_retries: u32,
    pub timeout: u32,
    /* private fields */
}

Fields§

§api_key: String§endpoint: String§max_retries: u32§timeout: u32

Implementations§

Source§

impl Client

Source

pub fn new( api_key: Option<String>, endpoint: Option<String>, max_retries: Option<u32>, timeout: Option<u32>, ) -> Result<Self, ClientError>

Constructs a new Client.

§Arguments
  • api_key - An optional API key. If not provided, the method will try to use the MISTRAL_API_KEY environment variable.
  • endpoint - An optional custom API endpoint. Defaults to the official API endpoint if not provided.
  • max_retries - Optional maximum number of retries for failed requests. Defaults to 5.
  • timeout - Optional timeout in seconds for requests. Defaults to 120.
§Examples
use mistralai_client::v1::client::Client;

let client = Client::new(Some("your_api_key_here".to_string()), None, Some(3), Some(60));
assert!(client.is_ok());
§Errors

This method fails whenever neither the api_key is provided nor the MISTRAL_API_KEY environment variable is set.

Source

pub fn chat( &self, model: Model, messages: Vec<ChatMessage>, options: Option<ChatParams>, ) -> Result<ChatResponse, ApiError>

Synchronously sends a chat completion request and returns the response.

§Arguments
  • model - The [Model] to use for the chat completion.
  • messages - A vector of [ChatMessage] to send as part of the chat.
  • options - Optional [ChatParams] to customize the request.
§Returns

Returns a Result containing the ChatResponse if the request is successful, or an [ApiError] if there is an error.

§Examples
use mistralai_client::v1::{
    chat::{ChatMessage, ChatMessageRole},
    client::Client,
    constants::Model,
};

let client = Client::new(None, None, None, None).unwrap();
let messages = vec![ChatMessage {
    role: ChatMessageRole::User,
    content: "Hello, world!".to_string(),
    tool_calls: None,
}];
let response = client.chat(Model::OpenMistral7b, messages, None).unwrap();
println!("{:?}: {}", response.choices[0].message.role, response.choices[0].message.content);
Source

pub async fn chat_async( &self, model: Model, messages: Vec<ChatMessage>, options: Option<ChatParams>, ) -> Result<ChatResponse, ApiError>

Asynchronously sends a chat completion request and returns the response.

§Arguments
  • model - The [Model] to use for the chat completion.
  • messages - A vector of [ChatMessage] to send as part of the chat.
  • options - Optional [ChatParams] to customize the request.
§Returns

Returns a Result containing a Stream of ChatStreamChunk if the request is successful, or an [ApiError] if there is an error.

§Examples
use mistralai_client::v1::{
    chat::{ChatMessage, ChatMessageRole},
    client::Client,
    constants::Model,
};

#[tokio::main]
async fn main() {
    let client = Client::new(None, None, None, None).unwrap();
    let messages = vec![ChatMessage {
        role: ChatMessageRole::User,
        content: "Hello, world!".to_string(),
        tool_calls: None,
    }];
    let response = client.chat_async(Model::OpenMistral7b, messages, None).await.unwrap();
    println!("{:?}: {}", response.choices[0].message.role, response.choices[0].message.content);
}
Source

pub async fn chat_stream( &self, model: Model, messages: Vec<ChatMessage>, options: Option<ChatParams>, ) -> Result<impl Stream<Item = Result<Vec<ChatStreamChunk>, ApiError>>, ApiError>

Asynchronously sends a chat completion request and returns a stream of message chunks.

§Arguments
  • model - The [Model] to use for the chat completion.
  • messages - A vector of [ChatMessage] to send as part of the chat.
  • options - Optional [ChatParams] to customize the request.
§Returns

Returns a Result containing a Stream of ChatStreamChunk if the request is successful, or an [ApiError] if there is an error.

§Examples
use futures::stream::StreamExt;
use mistralai_client::v1::{
    chat::{ChatMessage, ChatMessageRole},
    client::Client,
    constants::Model,
};
use std::io::{self, Write};

#[tokio::main]
async fn main() {
    let client = Client::new(None, None, None, None).unwrap();
    let messages = vec![ChatMessage {
        role: ChatMessageRole::User,
        content: "Hello, world!".to_string(),
        tool_calls: None,
    }];

    let stream_result = client
        .chat_stream(Model::OpenMistral7b,messages, None)
        .await
        .unwrap();
    stream_result
        .for_each(|chunk_result| async {
            match chunk_result {
                Ok(chunks) => chunks.iter().for_each(|chunk| {
                    print!("{}", chunk.choices[0].delta.content);
                    io::stdout().flush().unwrap();
                    // => "Once upon a time, [...]"
                }),
                Err(error) => {
                    eprintln!("Error processing chunk: {:?}", error)
                }
            }
        })
        .await;
    print!("\n") // To persist the last chunk output.
}
Source

pub fn embeddings( &self, model: EmbedModel, input: Vec<String>, options: Option<EmbeddingRequestOptions>, ) -> Result<EmbeddingResponse, ApiError>

Source

pub async fn embeddings_async( &self, model: EmbedModel, input: Vec<String>, options: Option<EmbeddingRequestOptions>, ) -> Result<EmbeddingResponse, ApiError>

Source

pub fn get_last_function_call_result(&self) -> Option<Box<dyn Any + Send>>

Source

pub fn list_models(&self) -> Result<ModelListResponse, ApiError>

Source

pub async fn list_models_async(&self) -> Result<ModelListResponse, ApiError>

Source

pub fn register_function(&mut self, name: String, function: Box<dyn Function>)

Trait Implementations§

Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,