pub struct ModelConfig {
pub spec: String,
pub api_key: Option<String>,
pub base_url: Option<String>,
pub timeout: Option<Duration>,
}Expand description
Configuration for creating a model from a string spec.
This allows specifying a model using the standard provider:model format
while also providing custom API keys, base URLs, and other options.
§Examples
use serdes_ai_agent::ModelConfig;
// Simple: just a model spec (uses env vars for keys)
let config = ModelConfig::new("openai:gpt-4o");
// With explicit API key
let config = ModelConfig::new("anthropic:claude-3-5-sonnet-20241022")
.with_api_key("sk-ant-your-key");
// With custom base URL (for proxies or compatible APIs)
let config = ModelConfig::new("openai:gpt-4o")
.with_api_key("your-key")
.with_base_url("https://your-proxy.com/v1");Fields§
§spec: StringModel spec in provider:model format (e.g., “openai:gpt-4o”)
api_key: Option<String>Optional API key (overrides environment variable)
base_url: Option<String>Optional base URL (for custom endpoints)
timeout: Option<Duration>Optional request timeout
Implementations§
Source§impl ModelConfig
impl ModelConfig
Sourcepub fn new(spec: impl Into<String>) -> ModelConfig
pub fn new(spec: impl Into<String>) -> ModelConfig
Create a new model config from a spec string.
The spec should be in provider:model format, e.g.:
"openai:gpt-4o""anthropic:claude-3-5-sonnet-20241022""groq:llama-3.1-70b-versatile""ollama:llama3.1"
If no provider prefix is given, OpenAI is assumed.
Sourcepub fn with_api_key(self, api_key: impl Into<String>) -> ModelConfig
pub fn with_api_key(self, api_key: impl Into<String>) -> ModelConfig
Set an explicit API key (overrides environment variable).
Sourcepub fn with_base_url(self, base_url: impl Into<String>) -> ModelConfig
pub fn with_base_url(self, base_url: impl Into<String>) -> ModelConfig
Set a custom base URL (for proxies or compatible APIs).
Sourcepub fn with_timeout(self, timeout: Duration) -> ModelConfig
pub fn with_timeout(self, timeout: Duration) -> ModelConfig
Set a request timeout.
Sourcepub fn build_model(&self) -> Result<Arc<dyn Model>, ModelError>
pub fn build_model(&self) -> Result<Arc<dyn Model>, ModelError>
Build a model from this configuration.
This creates the appropriate model type based on the provider, applying any custom API key, base URL, or timeout settings.
§Note
This method delegates to serdes_ai_models::infer_model_with_config when
using default settings (no custom API key/base URL), or creates the model
directly when custom configuration is provided.
The available providers depend on the features enabled in serdes-ai-models:
openai(default) - OpenAI models (gpt-4o, gpt-4, etc.)anthropic- Anthropic models (claude-3-5-sonnet, etc.)groq- Groq modelsmistral- Mistral modelsollama- Local Ollama modelsgoogle- Google/Gemini models
Trait Implementations§
Source§impl Clone for ModelConfig
impl Clone for ModelConfig
Source§fn clone(&self) -> ModelConfig
fn clone(&self) -> ModelConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more