Skip to main content

ModelRegistry

Struct ModelRegistry 

Source
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

Source

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.

Source

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

Source

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.

Source

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

Source

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

Source

pub fn new() -> Self

Source

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).

Source

pub fn price(&self, provider: &str, model: &str) -> Option<Price>

The price of model at provider, also when provider is an alias.

Source

pub fn breakers(&self) -> Arc<Breakers> ⓘ

The providers’ circuit breakers (see crate::failover), shared by clones.

Source

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.

Source

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.

Source

pub fn alias( &mut self, alias: impl Into<String>, target: impl Into<String>, ) -> &mut Self

Make alias resolve to the provider registered as target.

Source

pub fn with(self, name: impl Into<String>, model: Arc<dyn ChatModel>) -> Self

Builder-style ModelRegistry::add.

Source

pub fn remove(&mut self, name: &str) -> bool

Remove a provider (and any alias with that name). Returns whether something was removed.

Source

pub fn into_parts( self, ) -> (IndexMap<String, Arc<dyn ChatModel>>, IndexMap<String, String>)

Decompose into providers and aliases.

Source

pub fn names(&self) -> impl Iterator<Item = &str>

Source

pub fn is_empty(&self) -> bool

Source

pub fn has(&self, name: &str) -> bool

Whether name resolves to a provider, as ModelRegistry::get resolves it.

Source

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.

Source

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.

Source

pub fn from_env() -> Result<Self, LlmError>

Build a registry from environment variables:

VariablesProvider name
OPENAI_API_KEY, optional OPENAI_BASE_URLopenai
ANTHROPIC_API_KEY, optional ANTHROPIC_BASE_URLanthropic
OLLAMA_HOST (default http://localhost:11434; always registered)ollama
GRAPHRT_OPENAI_COMPAT_<NAME>_BASE_URL, optional ..._API_KEY<name> (lowercase)
DEEPSEEK_API_KEY, optional DEEPSEEK_BASE_URL (default https://api.deepseek.com/v1)deepseek
AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY, optional AZURE_OPENAI_API_VERSIONazure-openai (v1 API; model = deployment name)
GEMINI_API_KEY or GOOGLE_API_KEY, optional GEMINI_BASE_URLgemini
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_URLbedrock
Source

pub fn from_vars(vars: &[(String, String)]) -> Result<Self, LlmError>

Like ModelRegistry::from_env but reads from the given variables.

Trait Implementations§

Source§

impl Clone for ModelRegistry

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModelRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ModelRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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