Struct XAI

Source
pub struct XAI {
Show 18 fields pub api_key: String, pub model: String, pub max_tokens: Option<u32>, pub temperature: Option<f32>, pub system: Option<String>, pub timeout_seconds: Option<u64>, pub stream: Option<bool>, pub top_p: Option<f32>, pub top_k: Option<u32>, pub embedding_encoding_format: Option<String>, pub embedding_dimensions: Option<u32>, pub json_schema: Option<StructuredOutputFormat>, pub xai_search_mode: Option<String>, pub xai_search_source_type: Option<String>, pub xai_search_excluded_websites: Option<Vec<String>>, pub xai_search_max_results: Option<u32>, pub xai_search_from_date: Option<String>, pub xai_search_to_date: Option<String>, /* private fields */
}
Expand description

Client for interacting with X.AI’s API.

This struct provides methods for making chat and completion requests to X.AI’s language models. It handles authentication, request configuration, and response parsing.

Fields§

§api_key: String

API key for authentication with X.AI services

§model: String

Model identifier to use for requests (e.g. “grok-2-latest”)

§max_tokens: Option<u32>

Maximum number of tokens to generate in responses

§temperature: Option<f32>

Temperature parameter for controlling response randomness (0.0 to 1.0)

§system: Option<String>

Optional system prompt to provide context

§timeout_seconds: Option<u64>

Request timeout duration in seconds

§stream: Option<bool>

Whether to enable streaming responses

§top_p: Option<f32>

Top-p sampling parameter for controlling response diversity

§top_k: Option<u32>

Top-k sampling parameter for controlling response diversity

§embedding_encoding_format: Option<String>

Embedding encoding format

§embedding_dimensions: Option<u32>

Embedding dimensions

§json_schema: Option<StructuredOutputFormat>

JSON schema for structured output

§xai_search_mode: Option<String>

XAI search parameters

§xai_search_source_type: Option<String>

XAI search sources

§xai_search_excluded_websites: Option<Vec<String>>

XAI search excluded websites

§xai_search_max_results: Option<u32>

XAI search max results

§xai_search_from_date: Option<String>

XAI search from date

§xai_search_to_date: Option<String>

XAI search to date

Implementations§

Source§

impl XAI

Source

pub fn new( api_key: impl Into<String>, model: Option<String>, max_tokens: Option<u32>, temperature: Option<f32>, timeout_seconds: Option<u64>, system: Option<String>, stream: Option<bool>, top_p: Option<f32>, top_k: Option<u32>, embedding_encoding_format: Option<String>, embedding_dimensions: Option<u32>, json_schema: Option<StructuredOutputFormat>, xai_search_mode: Option<String>, xai_search_source_type: Option<String>, xai_search_excluded_websites: Option<Vec<String>>, xai_search_max_results: Option<u32>, xai_search_from_date: Option<String>, xai_search_to_date: Option<String>, ) -> Self

Creates a new X.AI client with the specified configuration.

§Arguments
  • api_key - Authentication key for X.AI API access
  • model - Model identifier (defaults to “grok-2-latest” if None)
  • max_tokens - Maximum number of tokens to generate in responses
  • temperature - Sampling temperature for controlling randomness
  • timeout_seconds - Request timeout duration in seconds
  • system - System prompt for providing context
  • stream - Whether to enable streaming responses
  • top_p - Top-p sampling parameter
  • top_k - Top-k sampling parameter
  • json_schema - JSON schema for structured output
  • search_parameters - Search parameters for search functionality
§Returns

A configured X.AI client instance ready to make API requests.

Trait Implementations§

Source§

impl ChatProvider for XAI

Source§

fn chat<'life0, 'life1, 'async_trait>( &'life0 self, messages: &'life1 [ChatMessage], ) -> Pin<Box<dyn Future<Output = Result<Box<dyn ChatResponse>, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Sends a chat request to the X.AI API and returns the response.

§Arguments
  • messages - Array of chat messages representing the conversation
§Returns

The generated response text, or an error if the request fails.

Source§

fn chat_with_tools<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, messages: &'life1 [ChatMessage], _tools: Option<&'life2 [Tool]>, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn ChatResponse>, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Sends a chat request to X.AI’s API with tools.

§Arguments
  • messages - The conversation history as a slice of chat messages
  • tools - Optional slice of tools to use in the chat
§Returns

The provider’s response text or an error

Source§

fn chat_stream<'life0, 'life1, 'async_trait>( &'life0 self, messages: &'life1 [ChatMessage], ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<String, LLMError>> + Send>>, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Sends a streaming chat request to X.AI’s API.

§Arguments
  • messages - Slice of chat messages representing the conversation
§Returns

A stream of text tokens or an error

Source§

fn memory_contents<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<Vec<ChatMessage>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get current memory contents if provider supports memory
Source§

fn summarize_history<'life0, 'life1, 'async_trait>( &'life0 self, msgs: &'life1 [ChatMessage], ) -> Pin<Box<dyn Future<Output = Result<String, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Summarizes a conversation history into a concise 2-3 sentence summary Read more
Source§

impl CompletionProvider for XAI

Source§

fn complete<'life0, 'life1, 'async_trait>( &'life0 self, _req: &'life1 CompletionRequest, ) -> Pin<Box<dyn Future<Output = Result<CompletionResponse, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Sends a completion request to X.AI’s API.

This functionality is currently not implemented.

§Arguments
  • _req - The completion request parameters
§Returns

A placeholder response indicating the functionality is not implemented.

Source§

impl EmbeddingProvider for XAI

Source§

fn embed<'life0, 'async_trait>( &'life0 self, text: Vec<String>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl LLMProvider for XAI

Source§

fn tools(&self) -> Option<&[Tool]>

Source§

impl SpeechToTextProvider for XAI

Source§

fn transcribe<'life0, 'async_trait>( &'life0 self, _audio: Vec<u8>, ) -> Pin<Box<dyn Future<Output = Result<String, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Transcribe the given audio bytes into text Read more
Source§

fn transcribe_file<'life0, 'life1, 'async_trait>( &'life0 self, file_path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

impl TextToSpeechProvider for XAI

Source§

fn speech<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, LLMError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Convert the given text into speech audio Read more

Auto Trait Implementations§

§

impl Freeze for XAI

§

impl !RefUnwindSafe for XAI

§

impl Send for XAI

§

impl Sync for XAI

§

impl Unpin for XAI

§

impl !UnwindSafe for XAI

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> MaybeSendSync for T