pub struct ModelRegistry { /* private fields */ }Expand description
Maps provider names (as used in node configs: model.provider) to models.
Register it with graphrt_core::Services::insert(registry).
Name resolution (see ModelRegistry::get) is exact, with two explicit
exceptions: aliases added with ModelRegistry::alias, and plugin-style
identifiers <org>/<provider>/<provider> (for example
acme/openai/openai), which resolve to <provider>. Other names, such as
acme/openai, are not guessed.
Implementations§
Source§impl ModelRegistry
impl ModelRegistry
Sourcepub fn from_config(config: &ProvidersConfig) -> Result<Self, LlmError>
pub fn from_config(config: &ProvidersConfig) -> Result<Self, LlmError>
Build a registry from a providers config, reading secrets and ${VAR}
references from the process environment.
Sourcepub fn from_config_with_vars(
config: &ProvidersConfig,
vars: &[(String, String)],
) -> Result<Self, LlmError>
pub fn from_config_with_vars( config: &ProvidersConfig, vars: &[(String, String)], ) -> Result<Self, LlmError>
Like ModelRegistry::from_config with explicit variables.
Top level prices for a provider the file does not configure are not
kept: use ProvidersConfig::load, whose unplaced_prices the host
places on its own providers.
A typesafe entry in the credentials section is not a chat model; use
ProvidersConfig::load to get its System One client as well.
Source§impl ModelRegistry
impl ModelRegistry
Sourcepub fn place_prices(
&mut self,
prices: impl IntoIterator<Item = (String, String, Price)>,
overwrite: bool,
) -> Vec<(String, String, Price)>
pub fn place_prices( &mut self, prices: impl IntoIterator<Item = (String, String, Price)>, overwrite: bool, ) -> Vec<(String, String, Price)>
Place (provider, model, price) on the providers of this registry,
under the name a provider name or alias resolves to (so a workflow that
names the provider either way finds the price). With overwrite false,
a model this registry already prices keeps its price. Returns the prices
whose provider this registry does not have.
Sourcepub fn place_guest_prices(
&mut self,
file: &Value,
) -> Result<GuestPrices, LlmError>
pub fn place_guest_prices( &mut self, file: &Value, ) -> Result<GuestPrices, LlmError>
Take only the prices of a providers file that is not the host’s own (a
bundle’s): the top level prices and the prices of its entries, for
the providers this registry has and the models it does not price yet.
Nothing of the file is built, so it never adds a provider, never
changes the endpoint, headers or credentials of one, and never reads
a variable of the host. A price too low to be one is not used
(implausible_price).
The file is read for its prices only: of an entry, name, prices and
models (whose aliases a price follows, as in a host’s file); every
other field, and fields this build does not know, are not looked at.
@throws LlmError - Config for a file that is not an object or a list,
or a price that is not a price (negative, not finite, absurd, unknown fields).
Source§impl ModelRegistry
impl ModelRegistry
Sourcepub fn merge(&mut self, other: ModelRegistry) -> &mut Self
pub fn merge(&mut self, other: ModelRegistry) -> &mut Self
Add every provider and alias of other. other wins every conflict: a
same-named provider is replaced, and an alias of other shadows a provider
of self with that name (so a providers file can redirect openai to a
local server). Used to layer a providers file over ModelRegistry::from_env.
Source§impl ModelRegistry
impl ModelRegistry
pub fn new() -> Self
Sourcepub fn set_price(
&mut self,
provider: impl Into<String>,
model: impl Into<String>,
price: Price,
) -> &mut Self
pub fn set_price( &mut self, provider: impl Into<String>, model: impl Into<String>, price: Price, ) -> &mut Self
Set the price of model at provider (the names a workflow uses).
Sourcepub fn price(&self, provider: &str, model: &str) -> Option<Price>
pub fn price(&self, provider: &str, model: &str) -> Option<Price>
The price of model at provider, also when provider is an alias.
Sourcepub fn add(
&mut self,
name: impl Into<String>,
model: Arc<dyn ChatModel>,
) -> &mut Self
pub fn add( &mut self, name: impl Into<String>, model: Arc<dyn ChatModel>, ) -> &mut Self
Register a provider, replacing any provider already registered under
name. Use ModelRegistry::try_add to reject duplicates instead.
Sourcepub fn try_add(
&mut self,
name: impl Into<String>,
model: Arc<dyn ChatModel>,
) -> Result<&mut Self, LlmError>
pub fn try_add( &mut self, name: impl Into<String>, model: Arc<dyn ChatModel>, ) -> Result<&mut Self, LlmError>
Register a provider, failing if name is already registered.
Sourcepub fn alias(
&mut self,
alias: impl Into<String>,
target: impl Into<String>,
) -> &mut Self
pub fn alias( &mut self, alias: impl Into<String>, target: impl Into<String>, ) -> &mut Self
Make alias resolve to the provider registered as target.
Sourcepub fn with(self, name: impl Into<String>, model: Arc<dyn ChatModel>) -> Self
pub fn with(self, name: impl Into<String>, model: Arc<dyn ChatModel>) -> Self
Builder-style ModelRegistry::add.
Sourcepub fn remove(&mut self, name: &str) -> bool
pub fn remove(&mut self, name: &str) -> bool
Remove a provider (and any alias with that name). Returns whether something was removed.
Sourcepub fn into_parts(
self,
) -> (IndexMap<String, Arc<dyn ChatModel>>, IndexMap<String, String>)
pub fn into_parts( self, ) -> (IndexMap<String, Arc<dyn ChatModel>>, IndexMap<String, String>)
Decompose into providers and aliases.
pub fn names(&self) -> impl Iterator<Item = &str>
pub fn is_empty(&self) -> bool
Sourcepub fn has(&self, name: &str) -> bool
pub fn has(&self, name: &str) -> bool
Whether name resolves to a provider, as ModelRegistry::get resolves it.
Sourcepub fn resolve<'a>(&'a self, name: &'a str) -> Option<&'a str>
pub fn resolve<'a>(&'a self, name: &'a str) -> Option<&'a str>
The name of the provider name resolves to, as ModelRegistry::get
resolves it: name itself, the target of an alias, or the provider of a
plugin-style identifier.
Sourcepub fn get(&self, name: &str) -> Result<Arc<dyn ChatModel>, LlmError>
pub fn get(&self, name: &str) -> Result<Arc<dyn ChatModel>, LlmError>
Resolve a provider name: an exact name, then an alias, then a
plugin-style <org>/<provider>/<provider> identifier.
Sourcepub fn from_env() -> Result<Self, LlmError>
pub fn from_env() -> Result<Self, LlmError>
Build a registry from environment variables:
| Variables | Provider name |
|---|---|
OPENAI_API_KEY, optional OPENAI_BASE_URL | openai |
ANTHROPIC_API_KEY, optional ANTHROPIC_BASE_URL | anthropic |
OLLAMA_HOST (default http://localhost:11434; always registered) | ollama |
GRAPHRT_OPENAI_COMPAT_<NAME>_BASE_URL, optional ..._API_KEY | <name> (lowercase) |
GEMINI_API_KEY or GOOGLE_API_KEY, optional GEMINI_BASE_URL | gemini |
AWS_REGION (or AWS_DEFAULT_REGION) plus AWS_BEARER_TOKEN_BEDROCK or AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY (+ AWS_SESSION_TOKEN), optional BEDROCK_ENDPOINT_URL | bedrock |