### Example Search: u32 -> bool Source: https://docs.rs/genai/0.5.2/genai/chat/printer/fn.print_chat_stream.html?search= An example search query demonstrating a type transformation from `u32` to `bool`. ```rust u32 -> bool ``` -------------------------------- ### Example Search: std::vec Source: https://docs.rs/genai/0.5.2/genai/chat/printer/fn.print_chat_stream.html?search= An example search query for `std::vec`. ```rust std::vec ``` -------------------------------- ### Example Search: Option, (T -> U) -> Option Source: https://docs.rs/genai/0.5.2/genai/chat/printer/fn.print_chat_stream.html?search= An example search query illustrating a functional transformation on an `Option` type. ```rust Option, (T -> U) -> Option ``` -------------------------------- ### Tool Schema Example Source: https://docs.rs/genai/0.5.2/genai/chat/struct.Tool.html An example of a JSON Schema for tool parameters, used to define the expected input for a tool. ```json { "type": "object", "properties": { "city": { "type": "string", "description": "The city name" }, "country": { "type": "string", "description": "The most likely country of this city name" }, "unit": { "type": "string", "enum": ["C", "F"], "description": "The temperature unit for the country. C for Celsius, and F for Fahrenheit" } }, "required": ["city", "country", "unit"], } ``` -------------------------------- ### ClientBuilder Initialization Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search=std%3A%3Avec Demonstrates the default initialization of the `ClientBuilder` and how to start building a `Client`. ```rust use crate::chat::ChatOptions; use crate::resolver::{ AuthResolver, IntoAuthResolverFn, IntoModelMapperFn, IntoServiceTargetResolverFn, ModelMapper, ServiceTargetResolver, }; use crate::webc::WebClient; use crate::{Client, ClientConfig, WebConfig}; use std::sync::Arc; /// Builder for `Client`. /// /// Create via: /// - `ClientBuilder::default()` /// - `Client::builder()` #[derive(Debug, Default)] pub struct ClientBuilder { web_client: Option, config: Option, } /// Builder methods impl ClientBuilder { /// Use a custom `reqwest::Client`. pub fn with_reqwest(mut self, reqwest_client: reqwest::Client) -> Self { self.web_client = Some(WebClient::from_reqwest_client(reqwest_client)); self } /// Set a `ClientConfig`. pub fn with_config(mut self, config: ClientConfig) -> Self { self.config = Some(config); self } /// Set `WebConfig` used to build the internal `reqwest::Client` (creates `ClientConfig` if absent). pub fn with_web_config(mut self, req_options: WebConfig) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); client_config.web_config = Some(req_options); self } } /// Builder ClientConfig passthrough convenient setters. /// The goal of these functions is to set nested values such as Client Config and ChatOptions for the client. impl ClientBuilder { /// Set `ChatOptions` on `ClientConfig` (creates it if absent). pub fn with_chat_options(mut self, options: ChatOptions) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); client_config.chat_options = Some(options); self } /// Set `AuthResolver` on `ClientConfig` (creates it if absent). pub fn with_auth_resolver(mut self, auth_resolver: AuthResolver) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); self.config.as_mut().unwrap().auth_resolver = Some(auth_resolver); self } /// Set `AuthResolver` from a resolver function (creates `ClientConfig` if absent). pub fn with_auth_resolver_fn(mut self, auth_resolver_fn: impl IntoAuthResolverFn) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); let auth_resolver = AuthResolver::from_resolver_fn(auth_resolver_fn); self.config.as_mut().unwrap().auth_resolver = Some(auth_resolver); self } /// Set `ServiceTargetResolver` on `ClientConfig` (creates it if absent). pub fn with_service_target_resolver(mut self, target_resolver: ServiceTargetResolver) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); self.config.as_mut().unwrap().service_target_resolver = Some(target_resolver); self } /// Set `ServiceTargetResolver` from a resolver function (creates `ClientConfig` if absent). pub fn with_service_target_resolver_fn(mut self, target_resolver_fn: impl IntoServiceTargetResolverFn) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); let target_resolver = ServiceTargetResolver::from_resolver_fn(target_resolver_fn); self.config.as_mut().unwrap().service_target_resolver = Some(target_resolver); self } /// Set `ModelMapper` on `ClientConfig` (creates it if absent). pub fn with_model_mapper(mut self, model_mapper: ModelMapper) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); self.config.as_mut().unwrap().model_mapper = Some(model_mapper); self } /// Set `ModelMapper` from a mapper function (creates `ClientConfig` if absent). pub fn with_model_mapper_fn(mut self, model_mapper_fn: impl IntoModelMapperFn) -> Self { let client_config = self.config.get_or_insert_with(ClientConfig::default); let model_mapper = ModelMapper::from_mapper_fn(model_mapper_fn); self.config.as_mut().unwrap().model_mapper = Some(model_mapper); self } } impl ClientBuilder { /// Build a `Client`. pub fn build(self) -> Client { let config = self.config.unwrap_or_default(); // Create WebClient based on configuration let web_client = if let Some(web_client) = self.web_client { // Use explicitly provided WebClient web_client } else if let Some(req_config) = config.web_config() { // Create WebClient with reqwest configuration let mut builder = reqwest::Client::builder(); builder = req_config.apply_to_builder(builder); let reqwest_client = builder.build().expect("Failed to build reqwest client"); WebClient::from_reqwest_client(reqwest_client) } else { // Use default WebClient WebClient::default() }; let inner = super::ClientInner { web_client, config }; Client { inner: Arc::new(inner) } } } ``` -------------------------------- ### Using Zai Adapter with Dual Endpoints Source: https://docs.rs/genai/0.5.2/src/genai/adapter/adapters/zai/mod.rs.html?search=std%3A%3Avec Demonstrates how to use the Zai adapter with different API endpoints. The first two examples show how to access the regular API, with the second explicitly using the 'zai::' namespace. The third example shows how to access the coding plan API using the 'zai-coding::' namespace. ```rust use genai::resolver::{Endpoint, ServiceTargetResolver}; use genai::{Client, AdapterKind, ModelIden}; let client = Client::builder().with_service_target_resolver(target_resolver).build(); // Use regular API let response = client.exec_chat("glm-4.6", chat_request, None).await?; ``` ```rust // Same, regular API let response = client.exec_chat("zai::glm-4.6", chat_request, None).await?; ``` ```rust // Use coding plan let response = client.exec_chat("zai-coding::glm-4.6", chat_request, None).await?; ``` -------------------------------- ### Iterate Over Characters and Their Byte Positions Source: https://docs.rs/genai/0.5.2/genai/struct.ModelName.html Use `char_indices` to get an iterator over the Unicode characters and their starting byte positions in a string slice. Useful for processing strings with multi-byte characters. ```rust let word = "goodbye"; let count = word.char_indices().count(); assert_eq!(7, count); let mut char_indices = word.char_indices(); assert_eq!(Some((0, 'g')), char_indices.next()); assert_eq!(Some((1, 'o')), char_indices.next()); assert_eq!(Some((2, 'o')), char_indices.next()); assert_eq!(Some((3, 'd')), char_indices.next()); assert_eq!(Some((4, 'b')), char_indices.next()); assert_eq!(Some((5, 'y')), char_indices.next()); assert_eq!(Some((6, 'e')), char_indices.next()); assert_eq!(None, char_indices.next()); ``` ```rust let yes = "y̆es"; let mut char_indices = yes.char_indices(); assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆') assert_eq!(Some((1, '\u{0306}')), char_indices.next()); // note the 3 here - the previous character took up two bytes assert_eq!(Some((3, 'e')), char_indices.next()); assert_eq!(Some((4, 's')), char_indices.next()); assert_eq!(None, char_indices.next()); ``` -------------------------------- ### Constructing a Client Source: https://docs.rs/genai/0.5.2/genai/struct.Client.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows how to construct a Client instance using ClientBuilder. This is the recommended way to initialize the client. ```rust let client = ClientBuilder::default().build(); ``` -------------------------------- ### Client Builder Initialization Source: https://docs.rs/genai/0.5.2/genai/struct.Client.html?search=u32+-%3E+bool Shows how to construct a Client instance using the default builder pattern. ```rust ClientBuilder::default().build() ``` -------------------------------- ### ClientBuilder Initialization via Client Source: https://docs.rs/genai/0.5.2/genai/struct.ClientBuilder.html Shows how to obtain a ClientBuilder from an existing Client instance. ```rust let builder = genai::Client::builder(); ``` -------------------------------- ### Initialize ClientBuilder Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates the default initialization of the ClientBuilder. ```rust use crate::client::ClientBuilder; let builder = ClientBuilder::default(); ``` -------------------------------- ### WebClient GET Request Source: https://docs.rs/genai/0.5.2/src/genai/webc/web_client.rs.html?search= Performs an asynchronous GET request to a specified URL with custom headers. Handles response processing and error checking. ```rust pub async fn do_get(&self, url: &str, headers: &[(String, String)]) -> Result { let mut reqwest_builder = self.reqwest_client.request(Method::GET, url); for (k, v) in headers.iter() { reqwest_builder = reqwest_builder.header(k, v); } let reqwest_res = reqwest_builder.send().await?; let response = WebResponse::from_reqwest_response(reqwest_res).await?; Ok(response) } ``` -------------------------------- ### Build Client with Combined Configurations Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates building a Client with multiple configurations applied, including custom reqwest client and web config. ```rust use crate::client::ClientBuilder; use crate::WebConfig; let reqwest_client = reqwest::Client::new(); let web_config = WebConfig::default(); let client = ClientBuilder::default() .with_reqwest(reqwest_client) .with_web_config(web_config) .build(); ``` -------------------------------- ### Get references to Result contents Source: https://docs.rs/genai/0.5.2/genai/resolver/type.Result.html?search=u32+-%3E+bool Use `as_ref` to get references to the contained values of a `Result` without consuming it. `as_mut` provides mutable references. ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` -------------------------------- ### Build Client with Default WebClient Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search= Illustrates building a client that uses the default `WebClient` when no custom web client or configuration is provided. ```rust use genai::Client; let client = Client::builder().build(); // This client will use WebClient::default() ``` -------------------------------- ### Get Chat Capture Raw Body Option Source: https://docs.rs/genai/0.5.2/src/genai/chat/chat_options.rs.html Retrieves the capture raw body setting for chat. It attempts to get the value from the chat object, falling back to the client object if unavailable. ```rust pub fn capture_raw_body(&self) -> Option { self.chat .and_then(|chat| chat.capture_raw_body) .or_else(|| self.client.and_then(|client| client.capture_raw_body)) } ``` -------------------------------- ### EmbedRequest Getters Source: https://docs.rs/genai/0.5.2/src/genai/embed/embed_request.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Offers methods to retrieve input data from an `EmbedRequest`, including getting a single input as an Option, all inputs as a vector of string slices, checking if it's a batch request, and getting the count of inputs. ```rust impl EmbedRequest { /// Get the input as a single string if it's a single input, None otherwise. pub fn single_input(&self) -> Option<&str> { match &self.input { EmbedInput::Single(text) => Some(text), EmbedInput::Batch(_) => None, } } /// Get the input as a vector of strings. /// For single input, returns a vector with one element. pub fn inputs(&self) -> Vec<&str> { match &self.input { EmbedInput::Single(text) => vec![text], EmbedInput::Batch(texts) => texts.iter().map(|s| s.as_str()).collect(), } } /// Check if this is a batch request. pub fn is_batch(&self) -> bool { matches!(self.input, EmbedInput::Batch(_)) } /// Get the number of inputs. pub fn input_count(&self) -> usize { match &self.input { EmbedInput::Single(_) => 1, EmbedInput::Batch(texts) => texts.len(), } } } ``` -------------------------------- ### ClientBuilder Default Initialization Source: https://docs.rs/genai/0.5.2/genai/struct.ClientBuilder.html Demonstrates how to create a default ClientBuilder instance. ```rust let builder = genai::ClientBuilder::default(); ``` -------------------------------- ### EmbedOptionsSet::new Source: https://docs.rs/genai/0.5.2/genai/embed/struct.EmbedOptionsSet.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a new instance of EmbedOptionsSet with default settings. ```APIDOC ## EmbedOptionsSet::new ### Description Creates a new EmbedOptionsSet. ### Method ``` pub fn new() -> Self ``` ### Returns A new `EmbedOptionsSet` instance. ``` -------------------------------- ### Get Service URL by Model Adapter Kind Source: https://docs.rs/genai/0.5.2/src/genai/adapter/dispatcher.rs.html Routes the request to the appropriate adapter to get the service URL based on the model's adapter kind. This function is used to determine the correct endpoint for a given model. ```rust pub fn get_service_url(model: &ModelIden, service_type: ServiceType, endpoint: Endpoint) -> Result { match model.adapter_kind { AdapterKind::OpenAI => OpenAIAdapter::get_service_url(model, service_type, endpoint), AdapterKind::OpenAIResp => OpenAIRespAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Gemini => GeminiAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Anthropic => AnthropicAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Fireworks => FireworksAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Together => TogetherAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Groq => GroqAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Mimo => MimoAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Nebius => NebiusAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Xai => XaiAdapter::get_service_url(model, service_type, endpoint), AdapterKind::DeepSeek => DeepSeekAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Zai => ZaiAdapter::get_service_url(model, service_type, endpoint), AdapterKind::BigModel => BigModelAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Cohere => CohereAdapter::get_service_url(model, service_type, endpoint), AdapterKind::Ollama => OllamaAdapter::get_service_url(model, service_type, endpoint), } } ``` -------------------------------- ### Chaining Optional Values with and_then and or_else Source: https://docs.rs/genai/0.5.2/src/genai/chat/chat_options.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This pattern is used to access nested optional values. It first attempts to get a value from `self.chat`. If `self.chat` is `None` or the nested field is `None`, it then attempts to get the value from `self.client` as a fallback. ```rust pub fn capture_content(&self) -> Option { self.chat .and_then(|chat| chat.capture_content) .or_else(|| self.client.and_then(|client| client.capture_content)) } ``` ```rust pub fn capture_reasoning_content(&self) -> Option { self.chat .and_then(|chat| chat.capture_reasoning_content) .or_else(|| self.client.and_then(|client| client.capture_reasoning_content)) } ``` ```rust pub fn capture_tool_calls(&self) -> Option { self.chat .and_then(|chat| chat.capture_tool_calls) .or_else(|| self.client.and_then(|client| client.capture_tool_calls)) } ``` ```rust pub fn capture_raw_body(&self) -> Option { self.chat .and_then(|chat| chat.capture_raw_body) .or_else(|| self.client.and_then(|client| client.capture_raw_body)) } ``` ```rust pub fn response_format(&self) -> Option<&ChatResponseFormat> { self.chat .and_then(|chat| chat.response_format.as_ref()) .or_else(|| self.client.and_then(|client| client.response_format.as_ref())) } ``` ```rust pub fn normalize_reasoning_content(&self) -> Option { self.chat .and_then(|chat| chat.normalize_reasoning_content) .or_else(|| self.client.and_then(|client| client.normalize_reasoning_content)) } ``` ```rust pub fn reasoning_effort(&self) -> Option<&ReasoningEffort> { self.chat .and_then(|chat| chat.reasoning_effort.as_ref()) .or_else(|| self.client.and_then(|client| client.reasoning_effort.as_ref())) } ``` ```rust pub fn verbosity(&self) -> Option<&Verbosity> { self.chat .and_then(|chat| chat.verbosity.as_ref()) .or_else(|| self.client.and_then(|client| client.verbosity.as_ref())) } ``` ```rust pub fn seed(&self) -> Option { self.chat .and_then(|chat| chat.seed) .or_else(|| self.client.and_then(|client| client.seed)) } ``` ```rust pub fn service_tier(&self) -> Option<&ServiceTier> { self.chat .and_then(|chat| chat.service_tier.as_ref()) .or_else(|| self.client.and_then(|client| client.service_tier.as_ref())) } ``` ```rust pub fn extra_headers(&self) -> Option<&Headers> { self.chat .and_then(|chat| chat.extra_headers.as_ref()) .or_else(|| self.client.and_then(|client| client.extra_headers.as_ref())) } ``` -------------------------------- ### Client Construction Source: https://docs.rs/genai/0.5.2/src/genai/client/client_types.rs.html?search=std%3A%3Avec Demonstrates how to construct a `Client` instance for sending AI requests. You can use the default constructor or a builder pattern for more configuration options. ```APIDOC ## Client Construction ### Description Provides methods for creating and configuring a `Client` to interact with AI services. ### Methods - `Client::default()`: Creates a `Client` with default configuration. Equivalent to `Client::builder().build()`. - `Client::builder()`: Returns a `ClientBuilder` for configuring and constructing a `Client`. Equivalent to `ClientBuilder::default()`. ### Usage Examples ```rust // Using default constructor let client = genai::Client::default(); // Using builder pattern let client = genai::Client::builder().build(); ``` ### Related Types - [`ClientBuilder`]: Used to configure and build a `Client` instance. - [`ClientConfig`]: Represents the configuration settings for the client. ``` -------------------------------- ### Tool Constructor and Methods Source: https://docs.rs/genai/0.5.2/genai/chat/struct.Tool.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Provides methods for creating and configuring Tool instances, including setting the name, description, schema, and configuration. ```APIDOC ## impl Tool ### Constructor #### pub fn new(name: impl Into) -> Self Create a new tool with the given name. ### Methods #### pub fn size(&self) -> usize Returns an approximate in-memory size of this `Tool`, in bytes, computed as the sum of the UTF-8 lengths of: * `name` * `description` (if any) * JSON-serialized `schema` (if any) * JSON-serialized `config` (if any) #### pub fn with_description(self, description: impl Into) -> Self Set the tool description. Returns self for chaining. #### pub fn with_schema(self, parameters: Value) -> Self Set the JSON Schema for the tool parameters. Returns self for chaining. #### pub fn with_config(self, config: Value) -> Self Set provider-specific configuration (if any). Returns self for chaining. ``` -------------------------------- ### Get Namespace Source: https://docs.rs/genai/0.5.2/genai/struct.ModelName.html Retrieves the namespace from the ModelName, if it exists. ```rust pub fn namespace(&self) -> Option<&str> ``` -------------------------------- ### Building the Client Source: https://docs.rs/genai/0.5.2/genai/struct.ClientBuilder.html Finalizes the configuration and constructs a Client instance using the configured ClientBuilder. ```rust let client = genai::ClientBuilder::default().build(); ``` -------------------------------- ### Get Namespace and Name Source: https://docs.rs/genai/0.5.2/genai/struct.ModelName.html Returns the namespace and the name from the ModelName. ```rust pub fn namespace_and_name(&self) -> (Option<&str>, &str) ``` -------------------------------- ### Build Client with Reqwest Configuration Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html Builds a `Client` instance, creating the `WebClient` from `WebConfig` if no `WebClient` was explicitly set. ```rust use crate::{ClientBuilder, WebConfig}; let mut builder = ClientBuilder::default(); let web_config = WebConfig::default(); builder = builder.with_web_config(web_config); // The build method will now use the web_config to create the reqwest client. let client = builder.build(); ``` -------------------------------- ### Get Embedding Dimensions Source: https://docs.rs/genai/0.5.2/genai/embed/struct.Embedding.html Retrieves the dimensionality of the embedding vector. ```rust pub fn dimensions(&self) -> usize ``` -------------------------------- ### Build Client with Default Configuration Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Builds a Client using default configurations for WebClient and ClientConfig. ```rust use crate::client::ClientBuilder; let client = ClientBuilder::default().build(); ``` -------------------------------- ### Get Embedding Vector Source: https://docs.rs/genai/0.5.2/genai/embed/struct.Embedding.html Retrieves a reference to the embedding vector. ```rust pub fn vector(&self) -> &Vec ``` -------------------------------- ### Build Client with Reqwest Configuration Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search= Shows building a client where the `WebClient` is created using `reqwest::Client` configuration specified via `WebConfig`. ```rust use genai::{Client, WebConfig}; let web_config = WebConfig::default(); let client = Client::builder() .with_web_config(web_config) .build(); // This client's WebClient is built from the provided WebConfig ``` -------------------------------- ### impl Any for T Source: https://docs.rs/genai/0.5.2/genai/chat/enum.ServiceTier.html?search=std%3A%3Avec Provides the `type_id` method to get the `TypeId` of an object. ```APIDOC ## fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. ``` -------------------------------- ### Get Dimensions Source: https://docs.rs/genai/0.5.2/src/genai/embed/embed_options.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the optional embedding dimensions specified for the request. ```rust pub fn dimensions(&self) -> Option { self.dimensions } ``` -------------------------------- ### type_id Function Source: https://docs.rs/genai/0.5.2/genai/chat/enum.ServiceTier.html?search=std%3A%3Avec Gets the `TypeId` of `self`. This is part of the `Any` trait implementation. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### ClientBuilder::build Source: https://docs.rs/genai/0.5.2/genai/struct.ClientBuilder.html?search=std%3A%3Avec Builds and returns a `Client` instance. ```APIDOC ## ClientBuilder::build ### Description Builds and returns a `Client` instance based on the current configuration of the `ClientBuilder`. ### Method Builder method ### Parameters None ### Response - **Client** - The constructed `Client` instance. ### Response Example ```rust // Assuming all necessary configurations have been set on the builder // let client = genai::ClientBuilder::default() // .with_chat_options(genai::ChatOptions::default()) // .build(); ``` ``` -------------------------------- ### try_get_int_ne Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed n-byte integer from the BoxError in native-endian byte order. ```APIDOC ## fn try_get_int_ne(&mut self, nbytes: usize) -> Result ### Description Gets a signed n-byte integer from `self` in native-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters - **nbytes** (usize) - Required - The number of bytes to read for the signed integer. ### Response - **Ok(i64)**: If a signed integer of the specified byte length is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### try_get_int_le Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed n-byte integer from the BoxError in little-endian byte order. ```APIDOC ## fn try_get_int_le(&mut self, nbytes: usize) -> Result ### Description Gets a signed n-byte integer from `self` in little-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters - **nbytes** (usize) - Required - The number of bytes to read for the signed integer. ### Response - **Ok(i64)**: If a signed integer of the specified byte length is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Box::from_raw with Global Allocator Example Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Shows how to manually create a Box from a raw pointer obtained via the global allocator. This is an advanced use case requiring careful memory management and adherence to safety invariants. ```rust use std::alloc::{alloc, Layout}; unsafe { let ptr = alloc(Layout::new::()) as *mut i32; // In general .write is required to avoid attempting to destruct // the (uninitialized) previous contents of `ptr`, though for this // simple example `*ptr = 5` would have worked as well. ptr.write(5); let x = Box::from_raw(ptr); } ``` -------------------------------- ### try_get_int Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed n-byte integer from the BoxError in big-endian byte order. ```APIDOC ## fn try_get_int(&mut self, nbytes: usize) -> Result ### Description Gets a signed n-byte integer from `self` in big-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters - **nbytes** (usize) - Required - The number of bytes to read for the signed integer. ### Response - **Ok(i64)**: If a signed integer of the specified byte length is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Tool Constructor Source: https://docs.rs/genai/0.5.2/src/genai/chat/tool/tool_base.rs.html?search= Creates a new `Tool` instance with a given name. Other fields like description, schema, and config are initialized to None. ```rust impl Tool { /// Create a new tool with the given name. pub fn new(name: impl Into) -> Self { Self { name: name.into(), description: None, schema: None, config: None, } } } ``` -------------------------------- ### try_get_uint_ne Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned n-byte integer from the BoxError in native-endian byte order. ```APIDOC ## fn try_get_uint_ne(&mut self, nbytes: usize) -> Result ### Description Gets an unsigned n-byte integer from `self` in native-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters - **nbytes** (usize) - Required - The number of bytes to read for the unsigned integer. ### Response - **Ok(u64)**: If an unsigned integer of the specified byte length is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### try_get_uint_le Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned n-byte integer from the BoxError in little-endian byte order. ```APIDOC ## fn try_get_uint_le(&mut self, nbytes: usize) -> Result ### Description Gets an unsigned n-byte integer from `self` in little-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters - **nbytes** (usize) - Required - The number of bytes to read for the unsigned integer. ### Response - **Ok(u64)**: If an unsigned integer of the specified byte length is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Using ZAI API Dual Endpoints Source: https://docs.rs/genai/0.5.2/src/genai/adapter/adapters/zai/mod.rs.html Demonstrates how to use the ZAI API with both the regular credit-based endpoint and the coding plan endpoint. The `zai::` namespace selects the regular API, while `zai-coding::` selects the coding plan API. Ensure the `Client` is built with the appropriate `ServiceTargetResolver`. ```rust use genai::resolver::{Endpoint, ServiceTargetResolver}; use genai::{Client, AdapterKind, ModelIden}; let client = Client::builder().with_service_target_resolver(target_resolver).build(); // Use regular API let response = client.exec_chat("glm-4.6", chat_request, None).await?; // Same, regular API let response = client.exec_chat("zai::glm-4.6", chat_request, None).await?; // Use coding plan let response = client.exec_chat("zai-coding::glm-4.6", chat_request, None).await?; ``` -------------------------------- ### Tool::new Source: https://docs.rs/genai/0.5.2/src/genai/chat/tool/tool_base.rs.html Creates a new `Tool` instance with a given name. Other fields like description, schema, and config are initialized to their default values (None). ```APIDOC ## Tool::new ### Description Create a new tool with the given name. ### Parameters - **name** (impl Into) - Required - The name for the new tool. ### Returns - `Self` - A new `Tool` instance. ``` -------------------------------- ### try_get_uint Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned n-byte integer from the BoxError in big-endian byte order. ```APIDOC ## fn try_get_uint(&mut self, nbytes: usize) -> Result ### Description Gets an unsigned n-byte integer from `self` in big-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters - **nbytes** (usize) - Required - The number of bytes to read for the unsigned integer. ### Response - **Ok(u64)**: If an unsigned integer of the specified byte length is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### try_get_i128_ne Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed 128-bit integer from the BoxError in native-endian byte order. ```APIDOC ## fn try_get_i128_ne(&mut self) -> Result ### Description Gets a signed 128-bit integer from `self` in native-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(i128)**: If a signed 128-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Build Client Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html?search= Build a `Client` instance using the configured `ClientBuilder`. ```APIDOC ## Build Client ### `build` **Description**: Build a `Client`. **Usage**: Creates a `Client` instance. It prioritizes an explicitly provided `WebClient`, then attempts to create one using `WebConfig` from `ClientConfig`, and falls back to a default `WebClient` if neither is available. ``` -------------------------------- ### try_get_i128_le Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed 128-bit integer from the BoxError in little-endian byte order. ```APIDOC ## fn try_get_i128_le(&mut self) -> Result ### Description Gets a signed 128-bit integer from `self` in little-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(i128)**: If a signed 128-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Binary Constructors Source: https://docs.rs/genai/0.5.2/src/genai/chat/binary.rs.html?search=std%3A%3Avec Provides methods to create `Binary` instances. Use `new` for direct construction, `from_base64` for encoded content, `from_url` for web links, and `from_file` for local files. ```rust impl Binary { /// Construct a new Binary value. pub fn new(content_type: impl Into, source: BinarySource, name: Option) -> Self { Self { name, content_type: content_type.into(), source, } } /// Create a binary from a base64 payload. /// /// - content_type: MIME type (e.g., "image/png", "application/pdf"). /// - content: base64-encoded bytes. /// - name: optional display name or filename. pub fn from_base64(content_type: impl Into, content: impl Into>, name: Option) -> Binary { Binary { name, content_type: content_type.into(), source: BinarySource::Base64(content.into()), } } /// Create a binary referencing a URL. /// /// Note: Only some providers accept URL-based inputs. pub fn from_url(content_type: impl Into, url: impl Into, name: Option) -> Binary { Binary { name, content_type: content_type.into(), source: BinarySource::Url(url.into()), } } /// Create a binary from a file path. /// /// Reads the file, determines the MIME type from the file extension, /// and base64-encodes the content. /// /// - file_path: Path to the file to read. /// /// Returns an error if the file cannot be read. pub fn from_file(file_path: impl AsRef) -> Result { let file_path = file_path.as_ref(); // Read the file content let content = std::fs::read(file_path) .map_err(|e| crate::Error::Internal(format!("Failed to read file '{}': {}", file_path.display(), e)))?; // Determine MIME type from extension let content_type = mime_guess::from_path(file_path).first_or_octet_stream().to_string(); // Base64 encode let b64_content = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &content); // Extract file name let name = file_path.file_name().and_then(|n| n.to_str()).map(String::from); Ok(Binary { name, content_type, source: BinarySource::Base64(b64_content.into()), }) } } ``` -------------------------------- ### try_get_i128 Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed 128-bit integer from the BoxError in big-endian byte order. ```APIDOC ## fn try_get_i128(&mut self) -> Result ### Description Gets a signed 128-bit integer from `self` in big-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(i128)**: If a signed 128-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### GroqAdapter Methods Source: https://docs.rs/genai/0.5.2/src/genai/adapter/adapters/groq/adapter_impl.rs.html?search=std%3A%3Avec This section details the methods implemented by the GroqAdapter, showcasing its functionality and limitations. ```APIDOC ## GroqAdapter ### Description Implements the `Adapter` trait for the Groq API, which is compatible with the OpenAI API. ### Methods #### `default_endpoint()` Returns the default API endpoint for Groq. - **Returns**: `Endpoint` - The default Groq API base URL. #### `default_auth()` Retrieves the default authentication data, typically from an environment variable. - **Returns**: `AuthData` - Authentication data, usually API key from `GROQ_API_KEY` environment variable. #### `all_model_names(_kind: AdapterKind)` Retrieves a list of all supported model names for the Groq adapter. - **Parameters**: - `_kind` (AdapterKind) - The type of adapter, not used in this implementation. - **Returns**: `Result>` - A vector of model name strings. #### `get_service_url(model: &ModelIden, service_type: ServiceType, endpoint: Endpoint)` Constructs the service URL for a given model and service type using the provided endpoint. This method delegates to `OpenAIAdapter::util_get_service_url`. - **Parameters**: - `model` (&ModelIden) - The identifier for the model. - `service_type` (ServiceType) - The type of service (e.g., chat completion). - `endpoint` (Endpoint) - The base API endpoint. - **Returns**: `Result` - The formatted service URL. #### `to_web_request_data(...)` Transforms chat request data into a format suitable for web requests. This method delegates to `OpenAIAdapter::util_to_web_request_data`. - **Parameters**: - `target` (ServiceTarget) - `service_type` (ServiceType) - `chat_req` (ChatRequest) - `chat_options` (ChatOptionsSet<'_, '_>) - **Returns**: `Result` - The web request data. #### `to_chat_response(...)` Converts a web response into a chat response object. This method delegates to `OpenAIAdapter::to_chat_response`. - **Parameters**: - `model_iden` (ModelIden) - `web_response` (WebResponse) - `options_set` (ChatOptionsSet<'_, '_>) - **Returns**: `Result` - The chat response object. #### `to_chat_stream(...)` Converts a `RequestBuilder` into a chat stream response. This method delegates to `OpenAIAdapter::to_chat_stream`. - **Parameters**: - `model_iden` (ModelIden) - `reqwest_builder` (RequestBuilder) - `options_set` (ChatOptionsSet<'_, '_>) - **Returns**: `Result` - The chat stream response object. #### `to_embed_request_data(...)` This method is not supported for the Groq adapter. - **Returns**: `Err(crate::Error::AdapterNotSupported)` - Indicates that embeddings are not supported. #### `to_embed_response(...)` This method is not supported for the Groq adapter. - **Returns**: `Err(crate::Error::AdapterNotSupported)` - Indicates that embeddings are not supported. ``` -------------------------------- ### try_get_u128_ne Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned 128-bit integer from the BoxError in native-endian byte order. ```APIDOC ## fn try_get_u128_ne(&mut self) -> Result ### Description Gets an unsigned 128-bit integer from `self` in native-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(u128)**: If an unsigned 128-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Set Web Configuration Source: https://docs.rs/genai/0.5.2/src/genai/client/builder.rs.html Configures the internal `reqwest::Client` using `WebConfig`. If `ClientConfig` is absent, it will be created. ```rust use crate::{ClientBuilder, WebConfig}; let web_config = WebConfig::default(); let builder = ClientBuilder::default().with_web_config(web_config); ``` -------------------------------- ### try_get_u128_le Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned 128-bit integer from the BoxError in little-endian byte order. ```APIDOC ## fn try_get_u128_le(&mut self) -> Result ### Description Gets an unsigned 128-bit integer from `self` in little-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(u128)**: If an unsigned 128-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### try_get_u128 Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned 128-bit integer from the BoxError in big-endian byte order. ```APIDOC ## fn try_get_u128(&mut self) -> Result ### Description Gets an unsigned 128-bit integer from `self` in big-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(u128)**: If an unsigned 128-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### ClientBuilder::build Source: https://docs.rs/genai/0.5.2/genai/struct.ClientBuilder.html Builds and returns a `Client` instance based on the current configuration. ```APIDOC ## ClientBuilder::build ### Description Build a `Client` instance using the configurations set on the `ClientBuilder`. ### Method Builder method ### Parameters None ### Response - **Client** - The constructed `Client` instance. ``` -------------------------------- ### Box::from_raw Example Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Demonstrates how to reconstruct a Box from a raw pointer. This is typically used when a Box has been converted to a raw pointer using Box::into_raw and needs to be managed again by Box for automatic memory management. ```rust let x = Box::new(5); let ptr = Box::into_raw(x); let x = unsafe { Box::from_raw(ptr) }; ``` -------------------------------- ### try_get_i64_ne Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed 64-bit integer from the BoxError in native-endian byte order. ```APIDOC ## fn try_get_i64_ne(&mut self) -> Result ### Description Gets a signed 64-bit integer from `self` in native-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(i64)**: If a signed 64-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Handle Unsupported Binary Sources Source: https://docs.rs/genai/0.5.2/src/genai/adapter/adapters/openai/adapter_impl.rs.html?search=std%3A%3Avec Logs a warning for unsupported binary sources, specifically files provided via URL, as OpenAI's API does not directly support this method. ```rust else if matches!(&binary.source, BinarySource::Url(_)) { // TODO: Need to return error warn!("OpenAI doesn't support file from URL, need to handle it gracefully"); } ``` -------------------------------- ### try_get_i64_le Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed 64-bit integer from the BoxError in little-endian byte order. ```APIDOC ## fn try_get_i64_le(&mut self) -> Result ### Description Gets a signed 64-bit integer from `self` in little-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(i64)**: If a signed 64-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### try_get_i64 Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets a signed 64-bit integer from the BoxError in big-endian byte order. ```APIDOC ## fn try_get_i64(&mut self) -> Result ### Description Gets a signed 64-bit integer from `self` in big-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(i64)**: If a signed 64-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Chat Options Configuration Source: https://docs.rs/genai/0.5.2/src/genai/chat/chat_options.rs.html Demonstrates how to configure various options for chat requests using the builder pattern. ```APIDOC ## Chat Options Configuration This section details the methods available for configuring chat options. ### `with_reasoning_effort` Sets the reasoning effort hint for the chat model. - **Method**: `with_reasoning_effort` - **Parameters**: - `value` (ReasoningEffort) - The desired reasoning effort level. ### `with_verbosity` Sets the verbosity hint for the chat model. - **Method**: `with_verbosity` - **Parameters**: - `value` (Verbosity) - The desired verbosity level. ### `with_seed` Sets a deterministic seed for reproducible results. - **Method**: `with_seed` - **Parameters**: - `value` (u64) - The seed value. ### `with_service_tier` Sets the service tier preference (OpenAI-specific). - **Method**: `with_service_tier` - **Parameters**: - `value` (ServiceTier) - The desired service tier. ### `with_extra_headers` Adds extra HTTP headers to the request. - **Method**: `with_extra_headers` - **Parameters**: - `headers` (impl Into) - The headers to add. ### `with_json_mode` (Deprecated) Deprecated: use `with_response_format(ChatResponseFormat::JsonMode)`. When using JSON mode, you should still instruct the model to produce JSON in your prompt for broad provider compatibility (e.g., mention "json" in system/user messages). - **Method**: `with_json_mode` - **Parameters**: - `value` (bool) - Whether to enable JSON mode. ``` -------------------------------- ### try_get_u64_ne Source: https://docs.rs/genai/0.5.2/genai/type.BoxError.html?search= Gets an unsigned 64-bit integer from the BoxError in native-endian byte order. ```APIDOC ## fn try_get_u64_ne(&mut self) -> Result ### Description Gets an unsigned 64-bit integer from `self` in native-endian byte order. ### Method (Implicitly a method call on a mutable reference) ### Parameters None ### Response - **Ok(u64)**: If an unsigned 64-bit integer is successfully extracted. - **Err(TryGetError)**: If the extraction fails. ``` -------------------------------- ### Client Builder Method Source: https://docs.rs/genai/0.5.2/genai/struct.Client.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Provides a convenient way to get a `ClientBuilder` instance, equivalent to `ClientBuilder::default()`. ```rust let builder = Client::builder(); ```