### trim_start_matches example Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Demonstrates removing a slice of characters from the start of a string. ```rust let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12"); ``` -------------------------------- ### Box::try_new_uninit example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Nightly-only experimental allocator_api example. Tries to construct a Box with uninitialized contents, then writes and initializes it. Returns Result with AllocError on failure. ```rust #![feature(allocator_api)] let mut five = Box::::try_new_uninit()?; // Deferred initialization: five.write(5); let five = unsafe { five.assume_init() }; assert_eq!(*five, 5); ``` -------------------------------- ### Box::try_new example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Nightly-only example requiring the allocator_api feature. Tries to allocate a new Box and returns Result; with ? the error is propagated if allocation fails. No allocation occurs for zero-sized T. ```rust #![feature(allocator_api)] let five = Box::try_new(5)?; ``` -------------------------------- ### Box::try_new_zeroed example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Nightly-only allocator_api example. Tries to construct a Box with uninitialized contents filled with 0 bytes, then initializes it. Returns Result with AllocError on failure. ```rust #![feature(allocator_api)] let zero = Box::::try_new_zeroed()?; let zero = unsafe { zero.assume_init() }; assert_eq!(*zero, 0); ``` -------------------------------- ### and_then example function Source: https://docs.rs/genai/latest/genai/type.Result.html Example for and_then: defines a function that squares a number and converts to string, returning an error on overflow. ```rust fn sq_then_to_string(x: u32) -> Result { x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") } ``` -------------------------------- ### Method chaining example Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Demonstrates trimming prefix and suffix characters from a string. ```rust assert_eq!("".trim_prefix('<').trim_suffix('>'), "https://example.com/"); ``` -------------------------------- ### trim_start Example Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Demonstrates trimming leading whitespace from a string containing Hebrew characters. ```rust let s = " עברית "; assert!(Some('ע') == s.trim_start().chars().next()); ``` -------------------------------- ### split_inclusive example Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Demonstrates splitting a string while keeping the matched pattern as a terminator. ```rust let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb." .split_inclusive('\n').collect(); assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]); ``` ```rust let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n" .split_inclusive('\n').collect(); assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]); ``` -------------------------------- ### eq_ignore_case_unnormalized examples Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Demonstrates Unicode-aware caseless matching using the nightly-only feature. ```rust #![feature(casefold)] assert!("Ferris".eq_ignore_case_unnormalized("FERRIS")); assert!("Ferrös".eq_ignore_case_unnormalized("FERRÖS")); assert!("ẞ".eq_ignore_case_unnormalized("ss")); ``` -------------------------------- ### Box::new example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Creates a Box containing the value 5. No allocation occurs if T is zero-sized. ```rust let five = Box::new(5); ``` -------------------------------- ### OpenCodeGo adapter test module setup Source: https://docs.rs/genai/latest/src/genai/adapter/adapters/opencode_go/adapter_impl.rs.html Test module imports and helper functions for creating test targets and requests. ```rust #[cfg(test)] mod tests { use super::*; use crate::ServiceTarget; use crate::adapter::{Adapter, ServiceType}; use crate::chat::{ChatOptions, ChatOptionsSet, ChatRequest}; use crate::embed::{EmbedOptionsSet, EmbedRequest}; use crate::resolver::AuthData; fn test_target(model_name: &str) -> ServiceTarget { ServiceTarget { endpoint: OpenCodeGoAdapter::default_endpoint(), auth: AuthData::from_single("test-key"), model: ModelIden::new(AdapterKind::OpenCodeGo, model_name), } } fn make_request(model_name: &str, service_type: ServiceType) -> WebRequestData { OpenCodeGoAdapter::to_web_request_data( test_target(model_name), service_type, ChatRequest::from_user("hello"), ChatOptionsSet::default(), ) .expect("to_web_request_data should succeed") } } ``` -------------------------------- ### trim_start directionality example Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Trims leading whitespace and then gets the first character. The example shows that after trimming, the first character is 'E'. This demonstrates the directionality of trim_start. ```rust let s = " English "; assert!(Some('E') == s.trim_start().chars().next()); ``` -------------------------------- ### match_indices with string patterns Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Collects matches along with their starting indices using match_indices. Overlapping matches are not included; only the first match is returned. The third example shows that overlapping matches are skipped. ```rust let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect(); assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); let v: Vec<_> = "1abcabc2".match_indices("abc").collect(); assert_eq!(v, [(1, "abc"), (4, "abc")]); let v: Vec<_> = "ababa".match_indices("aba").collect(); assert_eq!(v, [(0, "aba")]); // only the first `aba` ``` -------------------------------- ### Client construction logic Source: https://docs.rs/genai/latest/src/genai/client/builder.rs.html This code snippet demonstrates how the Client is built, selecting between an existing WebClient, a configured reqwest client, or a default WebClient instance. ```rust 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 with performance optimizations let default_config = super::web_config::WebConfig::default(); let mut builder = reqwest::Client::builder(); builder = default_config.apply_to_builder(builder); let reqwest_client = builder.build().expect("Failed to build reqwest client"); WebClient::from_reqwest_client(reqwest_client) }; let inner = super::ClientInner { web_client, config }; Client { inner: Arc::new(inner) } } } ``` -------------------------------- ### Build method to construct Client Source: https://docs.rs/genai/latest/src/genai/client/builder.rs.html Consumes the builder and returns a Client. Uses the stored config or a default ClientConfig if none was set. The method is incomplete in the source, ending with a comment about creating WebClient based on configuration. ```rust impl ClientBuilder { /// Build a `Client`. pub fn build(self) -> Client { let config = self.config.unwrap_or_default(); // Create WebClient based on configuration ``` -------------------------------- ### rmatch_indices with string patterns Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Collects matches in reverse order with their starting indices using rmatch_indices. Overlapping matches are not included; only the last match is returned. The third example shows that overlapping matches are skipped. ```rust let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect(); assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]); let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect(); assert_eq!(v, [(4, "abc"), (1, "abc")]); let v: Vec<_> = "ababa".rmatch_indices("aba").collect(); assert_eq!(v, [(2, "aba")]); // only the last `aba` ``` -------------------------------- ### iter_mut: Mutate contained Ok value via iterator Source: https://docs.rs/genai/latest/genai/type.Result.html Shows `iter_mut` to get a mutable iterator over the `Ok` value, allowing modification. The example mutates the contained value when `Ok` and verifies that `Err` yields no items. ```rust let mut x: Result = Ok(7); match x.iter_mut().next() { Some(v) => *v = 40, None => {}, } assert_eq!(x, Ok(40)); let mut x: Result = Err("nothing!"); assert_eq!(x.iter_mut().next(), None); ``` -------------------------------- ### split: iterate substrings separated by a pattern Source: https://docs.rs/genai/latest/genai/struct.ModelName.html Demonstrates `str::split` with various patterns: `char`, `&str`, slices of chars, closures, and the empty string. Shows that contiguous separators produce empty strings, separators at the start or end are neighbored by empty strings, and the empty string separator splits between every character. The last example warns about surprising whitespace behavior. ```rust let v: Vec<&str> = "Mary had a little lamb".split(' ').collect(); assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]); let v: Vec<&str> = "".split('X').collect(); assert_eq!(v, [""]); let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect(); assert_eq!(v, ["lion", "", "tiger", "leopard"]); let v: Vec<&str> = "lion::tiger::leopard".split("::").collect(); assert_eq!(v, ["lion", "tiger", "leopard"]); let v: Vec<&str> = "AABBCC".split("DD").collect(); assert_eq!(v, ["AABBCC"]); let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect(); assert_eq!(v, ["abc", "def", "ghi"]); let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect(); assert_eq!(v, ["lion", "tiger", "leopard"]); ``` ```rust let v: Vec<&str> = "2020-11-03 23:59".split(&['-', ' ', ':', '@'][..]).collect(); assert_eq!(v, ["2020", "11", "03", "23", "59"]); ``` ```rust let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect(); assert_eq!(v, ["abc", "def", "ghi"]); ``` ```rust let x = "||||a||b|c".to_string(); let d: Vec<_> = x.split('|').collect(); assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]); ``` ```rust let x = "(///)".to_string(); let d: Vec<_> = x.split('/').collect(); assert_eq!(d, &["(", "", "", ")"]); ``` ```rust let d: Vec<_> = "010".split("0").collect(); assert_eq!(d, &["", "1", ""]); ``` ```rust let f: Vec<_> = "rust".split("").collect(); assert_eq!(f, &["", "r", "u", "s", "t", ""]); ``` ```rust let x = " a b c".to_string(); let d: Vec<_> = x.split(' ').collect(); assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]); ``` -------------------------------- ### Tool Schema Example Source: https://docs.rs/genai/latest/genai/chat/struct.Tool.html An example of a JSON schema for tool parameters using the json! macro. ```json 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"], }) ``` -------------------------------- ### Manually create a Box from a raw pointer using from_raw_in Source: https://docs.rs/genai/latest/genai/type.BoxError.html Demonstrates manual allocation and Box construction using the system allocator. Requires allocator_api and slice_ptr_get features. ```rust #![feature(allocator_api, slice_ptr_get)] use std::alloc::{Allocator, Layout, System}; unsafe { let ptr = System.allocate(Layout::new::())?.as_mut_ptr() 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_in(ptr, System); } ``` -------------------------------- ### Gemini API Request Example Source: https://docs.rs/genai/latest/src/genai/adapter/adapters/gemini/adapter_impl.rs.html A cURL example showing how to send a content generation request to the Gemini API. ```bash # curl \ # -H 'Content-Type: application/json' \ # -d '{"contents":[{"parts":[{"text":"Explain how AI works"}]}]}' \ # -X POST 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=YOUR_API_KEY' ``` -------------------------------- ### Box::new_uninit example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Constructs a new Box with uninitialized contents and then writes a value and initializes it with assume_init. The write method is called before assume_init. ```rust let mut five = Box::::new_uninit(); // Deferred initialization: five.write(5); let five = unsafe { five.assume_init() }; assert_eq!(*five, 5) ``` -------------------------------- ### Tool constructors Source: https://docs.rs/genai/latest/src/genai/chat/tool/tool_base.rs.html Methods to initialize a new Tool or a web search tool. ```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, strict: None, config: None, } } /// Create a new web search tool. pub fn new_web_search() -> Self { Self::new(ToolName::WebSearch) } } ``` -------------------------------- ### Parse payload and emit Start event Source: https://docs.rs/genai/latest/src/genai/adapter/adapters/bedrock/streamer.rs.html Parses the frame payload into a Value, emitting a Start event if not already emitted. ```rust let mut payload: Value = if frame.payload.is_empty() { Value::Null } else { serde_json::from_slice(&frame.payload).map_err(|serde_error| Error::StreamParse { model_iden: self.options.model_iden.clone(), serde_error, })? }; let mut events: Vec = Vec::new(); if !self.emitted_start { self.emitted_start = true; events.push(InterStreamEvent::Start); } ``` -------------------------------- ### Box::leak simple usage example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Leaks a Box and obtains a mutable reference with a chosen lifetime. The example uses 'static and modifies the value. ```rust let x = Box::new(41); let static_ref: &'static mut usize = Box::leak(x); *static_ref += 1; assert_eq!(*static_ref, 42); ``` -------------------------------- ### Binary::from_file Source: https://docs.rs/genai/latest/src/genai/chat/binary.rs.html Creates a Binary from a file path, reading the file, determining MIME type from extension, and base64-encoding the content. ```APIDOC ## Binary::from_file ### Description Creates a `Binary` from a file path. Reads the file, determines the MIME type from the file extension, and base64-encodes the content. ### Method `pub fn from_file(file_path: impl AsRef) -> Result` ### Parameters - **file_path** (`impl AsRef`) - Required - Path to the file to read. ### Returns - `Result` - The constructed `Binary` or an error if the file cannot be read. ``` -------------------------------- ### WebConfig builder methods for timeouts, headers, and proxies Source: https://docs.rs/genai/latest/src/genai/client/web_config.rs.html Provides builder-style methods to set timeouts, default headers, and proxies. The proxy methods return Result and use reqwest::Proxy::http, https, or all. ```rust impl WebConfig { /// Sets the per-request timeout. pub fn with_timeout(mut self, timeout: Duration) -> Self { self.timeout = Some(timeout); self } /// Sets the connect timeout. pub fn with_connect_timeout(mut self, timeout: Duration) -> Self { self.connect_timeout = Some(timeout); self } /// Sets default headers. pub fn with_default_headers(mut self, headers: reqwest::header::HeaderMap) -> Self { self.default_headers = Some(headers); self } /// Sets the proxy. pub fn with_proxy(mut self, proxy: reqwest::Proxy) -> Self { self.proxy = Some(proxy); self } /// Sets the HTTP proxy from a URL. pub fn with_proxy_url(mut self, proxy_url: &str) -> Result { let proxy = reqwest::Proxy::http(proxy_url)?; self.proxy = Some(proxy); Ok(self) } /// Sets the HTTPS proxy from a URL. pub fn with_https_proxy_url(mut self, proxy_url: &str) -> Result { let proxy = reqwest::Proxy::https(proxy_url)?; self.proxy = Some(proxy); Ok(self) } /// Sets the proxy for all schemes from a URL. pub fn with_all_proxy_url(mut self, proxy_url: &str) -> Result { let proxy = reqwest::Proxy::all(proxy_url)?; self.proxy = Some(proxy); Ok(self) } } ``` -------------------------------- ### pub fn from_lower_str(name: &str) -> Option Source: https://docs.rs/genai/latest/genai/adapter/enum.AdapterKind.html Parses an adapter kind from a lowercase string representation. ```APIDOC ## pub fn from_lower_str(name: &str) -> Option ### Description Parses an adapter kind from a lowercase string representation. ### Method pub fn from_lower_str ### Signature pub fn from_lower_str(name: &str) -> Option ### Parameters - **name** (&str) - Required - The lowercase string to parse. ### Returns - **Option** - The adapter kind if the string matches, otherwise None. ``` -------------------------------- ### EmbedOptions::headers Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets the headers. ```APIDOC ## EmbedOptions::headers() -> Option<&Headers> ### Description Gets the headers. ### Method `pub fn headers(&self) -> Option<&Headers>` ### Parameters None. ### Returns - `Option<&Headers>` - The headers if set, otherwise `None`. ``` -------------------------------- ### Clone example for Box containing a value of type 5 clone equals original Source: https://docs.rs/genai/latest/genai/type.BoxError.html Example from the Clone trait implementation. It creates a boxed integer, clones it, and asserts equality, demonstrating that cloning preserves the value. ```rust let x = Box::new(5); let y = x.clone(); // The value is the same assert_eq!(x, y); ``` -------------------------------- ### OpenCodeGoAdapter implementation with model dispatch Source: https://docs.rs/genai/latest/src/genai/adapter/adapters/opencode_go/adapter_impl.rs.html Full Rust implementation of the OpenCodeGoAdapter. It uses the OpenAI adapter for most models and the Anthropic adapter for MiniMax models (prefix 'minimax-'). The default endpoint is 'https://opencode.ai/zen/go/v1/' and the API key environment variable is 'OPENCODE_GO_API_KEY'. For Anthropic requests, it sets the 'x-api-key' header instead of Bearer auth. ```rust use crate::adapter::adapters::support::get_api_key; use crate::adapter::anthropic::{AnthropicAdapter, AnthropicRequestParts}; use crate::adapter::openai::OpenAIAdapter; use crate::adapter::{Adapter, AdapterKind, ServiceType, WebRequestData}; use crate::chat::{ChatOptionsSet, ChatRequest, ChatResponse, ChatStreamResponse}; use crate::embed::{EmbedOptionsSet, EmbedRequest, EmbedResponse}; use crate::resolver::{AuthData, Endpoint}; use crate::webc::WebResponse; use crate::{Error, Headers, ModelIden, Result, ServiceTarget}; use reqwest::RequestBuilder; use serde_json::json; use value_ext::JsonValueExt; pub struct OpenCodeGoAdapter; impl OpenCodeGoAdapter { pub const API_KEY_DEFAULT_ENV_NAME: &str = "OPENCODE_GO_API_KEY"; } // region: --- OpenCodeGoModelKind /// Internal enum to dispatch wire format based on model name prefix. /// MiniMax models use Anthropic protocol; all others use OpenAI protocol. /// Unrecognized model names silently fall back to the OpenAI path, reflecting /// the dynamic nature of the OpenCode Go proxy which may add new OpenAI-compatible /// models at any time. enum OpenCodeGoModelKind { OpenAI, Anthropic, } impl OpenCodeGoModelKind { fn from_model_name(name: &str) -> Self { if name.to_lowercase().starts_with("minimax-") { Self::Anthropic } else { Self::OpenAI } } } // endregion: --- OpenCodeGoModelKind impl Adapter for OpenCodeGoAdapter { const DEFAULT_API_KEY_ENV_NAME: Option<&'static str> = Some(Self::API_KEY_DEFAULT_ENV_NAME); fn default_auth() -> AuthData { match Self::DEFAULT_API_KEY_ENV_NAME { Some(env_name) => AuthData::from_env(env_name), None => AuthData::None, } } fn default_endpoint() -> Endpoint { Endpoint::from_static("https://opencode.ai/zen/go/v1/") } async fn all_model_names(kind: AdapterKind, endpoint: Endpoint, auth: AuthData) -> Result> { OpenAIAdapter::list_model_names_for_end_target(kind, endpoint, auth).await } fn get_service_url(model: &ModelIden, _service_type: ServiceType, endpoint: Endpoint) -> Result { let base_url = endpoint.base_url(); let (_, model_name) = model.model_name.namespace_and_name(); let model_kind = OpenCodeGoModelKind::from_model_name(model_name); let suffix = match model_kind { OpenCodeGoModelKind::OpenAI => "chat/completions", OpenCodeGoModelKind::Anthropic => "messages", }; Ok(format!("{base_url}{suffix}")) } fn to_web_request_data( target: ServiceTarget, service_type: ServiceType, chat_req: ChatRequest, options_set: ChatOptionsSet<'_, '_>, ) -> Result { let ServiceTarget { endpoint, auth, model } = target; let (_, model_name) = model.model_name.namespace_and_name(); let model_kind = OpenCodeGoModelKind::from_model_name(model_name); match model_kind { OpenCodeGoModelKind::OpenAI => OpenAIAdapter::util_to_web_request_data( ServiceTarget { endpoint, auth, model }, service_type, chat_req, options_set, None, ), OpenCodeGoModelKind::Anthropic => { let model_name = model_name.to_string(); let AnthropicRequestParts { system, messages, tools, } = AnthropicAdapter::into_anthropic_request_parts(chat_req)?; let stream = matches!(service_type, ServiceType::ChatStream); let mut payload = json!({ "model": model_name, "messages": messages, "stream": stream, }); if let Some(system) = system { payload.x_insert("system", system)?; } if let Some(tools) = tools { payload.x_insert("tools", tools)?; } if let Some(temperature) = options_set.temperature() { payload.x_insert("temperature", temperature)?; } if let Some(top_p) = options_set.top_p() { payload.x_insert("top_p", top_p)?; } if !options_set.stop_sequences().is_empty() { payload.x_insert("stop_sequences", options_set.stop_sequences())?; } if let Some(max_tokens) = options_set.max_tokens() { payload.x_insert("max_tokens", max_tokens)?; } // MiniMax /v1/messages requires `x-api-key` (Bearer returns 401). let api_key = get_api_key(auth, &model)?; let headers = Headers::from(("x-api-key".to_string(), api_key)); let url = Self::get_service_url(&model, service_type, endpoint)?; Ok(WebRequestData { url, headers, payload }) } } } fn to_chat_response( model_iden: ModelIden, web_response: WebResponse, options_set: ChatOptionsSet<'_, '_>, ) -> Result { ``` -------------------------------- ### Box::new_uninit_in example Source: https://docs.rs/genai/latest/genai/type.BoxError.html Constructs a new box with uninitialized contents in the provided allocator. Uses deferred initialization with write and assume_init. ```rust #![feature(allocator_api)] use std::alloc::System; let mut five = Box::::new_uninit_in(System); // Deferred initialization: five.write(5); let five = unsafe { five.assume_init() }; assert_eq!(*five, 5) ``` -------------------------------- ### EmbedOptions::truncate Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets the truncation method. ```APIDOC ## EmbedOptions::truncate() -> Option<&str> ### Description Gets the truncation method. ### Method `pub fn truncate(&self) -> Option<&str>` ### Parameters None. ### Returns - `Option<&str>` - The truncation method if set, otherwise `None`. ``` -------------------------------- ### EmbedOptions::user Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets the user identifier. ```APIDOC ## EmbedOptions::user() -> Option<&str> ### Description Gets the user identifier. ### Method `pub fn user(&self) -> Option<&str>` ### Parameters None. ### Returns - `Option<&str>` - The user identifier if set, otherwise `None`. ``` -------------------------------- ### Box::new_uninit Source: https://docs.rs/genai/latest/genai/type.BoxError.html Constructs a new box with uninitialized contents, allowing deferred initialization. ```APIDOC ## Box::new_uninit() -> Box> ### Description Constructs a new box with uninitialized contents. ### Method Associated function ### Signature `pub fn new_uninit() -> Box>` ### Return Value - **Box>** - A box with uninitialized memory. ### Examples ```rust let mut five = Box::::new_uninit(); // Deferred initialization: five.write(5); let five = unsafe { five.assume_init() }; assert_eq!(*five, 5) ``` ``` -------------------------------- ### Manually create a Box from a NonNull pointer using from_non_null_in Source: https://docs.rs/genai/latest/genai/type.BoxError.html Demonstrates manual allocation and Box construction using the system allocator with NonNull. Requires the allocator_api feature. ```rust #![feature(allocator_api)] use std::alloc::{Allocator, Layout, System}; unsafe { let non_null = System.allocate(Layout::new::())?.cast::(); // In general .write is required to avoid attempting to destruct // the (uninitialized) previous contents of `non_null`. non_null.write(5); let x = Box::from_non_null_in(non_null, System); } ``` -------------------------------- ### EmbedOptions::encoding_format Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets the encoding format. ```APIDOC ## EmbedOptions::encoding_format() -> Option<&str> ### Description Gets the encoding format. ### Method `pub fn encoding_format(&self) -> Option<&str>` ### Parameters None. ### Returns - `Option<&str>` - The encoding format if set, otherwise `None`. ``` -------------------------------- ### Using ZAI Dual Endpoints with ServiceTargetResolver Source: https://docs.rs/genai/latest/src/genai/adapter/adapters/zai/mod.rs.html Demonstrates how to configure a client with a service target resolver and execute chat requests using different model namespaces to target either the regular API or the coding plan. ```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?; ``` -------------------------------- ### EmbedOptions::dimensions Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets the desired dimensions. ```APIDOC ## EmbedOptions::dimensions() -> Option ### Description Gets the desired dimensions. ### Method `pub fn dimensions(&self) -> Option` ### Parameters None. ### Returns - `Option` - The desired dimensions if set, otherwise `None`. ``` -------------------------------- ### Implement AihubmixAdapter by delegating to OpenAIAdapter Source: https://docs.rs/genai/latest/src/genai/adapter/adapters/aihubmix/adapter_impl.rs.html Defines the AihubmixAdapter struct and its Adapter implementation, delegating all operations to OpenAIAdapter because the AIHubMix API is OpenAI-compatible. Sets the default API key environment variable to AIHUBMIX_API_KEY and the base endpoint to https://aihubmix.com/v1/. Implements chat, streaming, and embedding request/response conversions. ```rust use crate::ModelIden; use crate::adapter::openai::OpenAIAdapter; use crate::adapter::{Adapter, AdapterKind, ServiceType, WebRequestData}; use crate::chat::{ChatOptionsSet, ChatRequest, ChatResponse, ChatStreamResponse}; use crate::resolver::{AuthData, Endpoint}; use crate::webc::WebResponse; use crate::{Result, ServiceTarget}; use reqwest::RequestBuilder; pub struct AihubmixAdapter; impl AihubmixAdapter { pub const API_KEY_DEFAULT_ENV_NAME: &str = "AIHUBMIX_API_KEY"; } // The AIHubMix API adapter is modeled after the OpenAI adapter, as the AIHubMix API is compatible with the OpenAI API. impl Adapter for AihubmixAdapter { const DEFAULT_API_KEY_ENV_NAME: Option<&'static str> = Some(Self::API_KEY_DEFAULT_ENV_NAME); fn default_endpoint() -> Endpoint { const BASE_URL: &str = "https://aihubmix.com/v1/"; Endpoint::from_static(BASE_URL) } fn default_auth() -> AuthData { match Self::DEFAULT_API_KEY_ENV_NAME { Some(env_name) => AuthData::from_env(env_name), None => AuthData::None, } } async fn all_model_names(kind: AdapterKind, endpoint: Endpoint, auth: AuthData) -> Result> { OpenAIAdapter::list_model_names_for_end_target(kind, endpoint, auth).await } fn get_service_url(model: &ModelIden, service_type: ServiceType, endpoint: Endpoint) -> Result { OpenAIAdapter::util_get_service_url(model, service_type, endpoint) } fn to_web_request_data( target: ServiceTarget, service_type: ServiceType, chat_req: ChatRequest, chat_options: ChatOptionsSet<'_, '_>, ) -> Result { OpenAIAdapter::util_to_web_request_data(target, service_type, chat_req, chat_options, None) } fn to_chat_response( model_iden: ModelIden, web_response: WebResponse, options_set: ChatOptionsSet<'_, '_>, ) -> Result { OpenAIAdapter::to_chat_response(model_iden, web_response, options_set) } fn to_chat_stream( model_iden: ModelIden, reqwest_builder: RequestBuilder, options_set: ChatOptionsSet<'_, '_>, ) -> Result { OpenAIAdapter::to_chat_stream(model_iden, reqwest_builder, options_set) } fn to_embed_request_data( service_target: crate::ServiceTarget, embed_req: crate::embed::EmbedRequest, options_set: crate::embed::EmbedOptionsSet<'_, '_>, ) -> Result { OpenAIAdapter::to_embed_request_data(service_target, embed_req, options_set) } fn to_embed_response( model_iden: crate::ModelIden, web_response: crate::webc::WebResponse, options_set: crate::embed::EmbedOptionsSet<'_, '_>, ) -> Result { OpenAIAdapter::to_embed_response(model_iden, web_response, options_set) } } ``` -------------------------------- ### EmbedRequest::input_count Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedRequest.html Gets the number of inputs. ```APIDOC ## EmbedRequest::input_count ### Description Gets the number of inputs. ### Method Getter ### Signature pub fn input_count(&self) -> usize ### Returns - **usize** - The number of inputs. ``` -------------------------------- ### EmbedOptions::embedding_type Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets the type/purpose of the embedding request. ```APIDOC ## EmbedOptions::embedding_type() -> Option<&str> ### Description Gets the type/purpose of the embedding request. ### Method `pub fn embedding_type(&self) -> Option<&str>` ### Parameters None. ### Returns - `Option<&str>` - The type/purpose if set, otherwise `None`. ``` -------------------------------- ### EmbedOptions::capture_usage Source: https://docs.rs/genai/latest/genai/embed/struct.EmbedOptions.html Gets whether to capture usage. ```APIDOC ## EmbedOptions::capture_usage() -> bool ### Description Gets whether to capture usage. ### Method `pub fn capture_usage(&self) -> bool` ### Parameters None. ### Returns - `bool` - Whether to capture usage. ``` -------------------------------- ### Binary::from_url Source: https://docs.rs/genai/latest/src/genai/chat/binary.rs.html Creates a Binary referencing a URL with the given content type and optional name. ```APIDOC ## Binary::from_url ### Description Creates a `Binary` referencing a URL. Note: Only some providers accept URL-based inputs. ### Method `pub fn from_url(content_type: impl Into, url: impl Into, name: Option) -> Binary` ### Parameters - **content_type** (`impl Into`) - Required - MIME type of the content. - **url** (`impl Into`) - Required - URL of the binary content. - **name** (`Option`) - Optional - Display name or filename. ### Returns - `Binary` - The constructed `Binary` instance. ``` -------------------------------- ### AuthData::single_key_value Source: https://docs.rs/genai/latest/genai/resolver/enum.AuthData.html Gets the single value from the AuthData. ```APIDOC ## AuthData::single_key_value ### Description Gets the single value from the `AuthData`. ### Method `pub fn single_key_value(&self) -> Result` ### Returns - `Result` - The single key value, or an error if the `AuthData` does not contain a single value. ```