registry
Module for defining registries of clients.
ClientRegistry
dataclass
A registry for managing and accessing different client factories for various AI models.
This class serves as a central repository for client factories, allowing easy registration, retrieval, and mapping of model names to their respective factories. It provides a flexible way to manage multiple AI model providers in a single application.
Attributes:
Name | Type | Description |
---|---|---|
factories |
Dict[str, Type[ClientFactory]]
|
A dictionary mapping model class names to their corresponding ClientFactory classes. |
model_map |
Dict[str, str]
|
A dictionary mapping model name patterns to their corresponding model class names. |
The default configuration includes factories for Anthropic, OpenAI, Databricks, and Ollama, with mappings for common model name patterns.
Source code in spark_instructor/registry.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
get_factory(model_class)
Retrieve a factory class for a given model class.
This method looks up and returns the appropriate ClientFactory subclass for the specified model class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class |
str
|
The name of the model class to look up. |
required |
Returns:
Type | Description |
---|---|
Type[ClientFactory]
|
Type[ClientFactory]: The corresponding ClientFactory subclass. |
Raises:
Type | Description |
---|---|
ValueError
|
If no factory is registered for the given model class. |
Source code in spark_instructor/registry.py
get_factory_from_model(model)
Retrieve a factory class based on a model name.
This method uses the model_map to determine the appropriate model class for a given model name, then returns the corresponding factory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
str
|
The name of the model (e.g., "gpt-4o", "claude-3-5-sonnet-20240620"). |
required |
Returns:
Type | Description |
---|---|
Type[ClientFactory]
|
Type[ClientFactory]: The corresponding ClientFactory subclass. |
Raises:
Type | Description |
---|---|
ValueError
|
If the model name doesn't match any known patterns in the model_map. |
Note
This method performs a partial match on the model name. For example, any model name containing "gpt" will be mapped to the OpenAI factory.
Source code in spark_instructor/registry.py
register(model_class, factory_class)
Register a new factory for a given model class.
This method allows adding new factory classes to the registry or overriding existing ones.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class |
str
|
The name of the model class (e.g., "openai", "anthropic"). |
required |
factory_class |
Type[ClientFactory]
|
The ClientFactory subclass to be registered. |
required |
Note
The model_class is converted to lowercase before registration to ensure case-insensitive lookups.
Source code in spark_instructor/registry.py
get_default_factories()
Get default factories based on whether anthropic is available.