|
| 1 | +from .provider import ProviderFactory, ProviderNames |
| 2 | + |
| 3 | + |
| 4 | +class Client: |
| 5 | + def __init__(self, provider_configs: dict = {}): |
| 6 | + """ |
| 7 | + Initialize the client with provider configurations. |
| 8 | + Use the ProviderFactory to create provider instances. |
| 9 | +
|
| 10 | + Args: |
| 11 | + provider_configs (dict): A dictionary containing provider configurations. |
| 12 | + Each key should be a ProviderNames enum or its string representation, |
| 13 | + and the value should be a dictionary of configuration options for that provider. |
| 14 | + For example: |
| 15 | + { |
| 16 | + ProviderNames.OPENAI: {"api_key": "your_openai_api_key"}, |
| 17 | + "aws-bedrock": { |
| 18 | + "aws_access_key": "your_aws_access_key", |
| 19 | + "aws_secret_key": "your_aws_secret_key", |
| 20 | + "aws_region": "us-west-2" |
| 21 | + } |
| 22 | + } |
| 23 | + """ |
| 24 | + self.providers = {} |
| 25 | + self.provider_configs = provider_configs |
| 26 | + self._chat = None |
| 27 | + self._initialize_providers() |
| 28 | + |
| 29 | + def _initialize_providers(self): |
| 30 | + """Helper method to initialize or update providers.""" |
| 31 | + for provider_key, config in self.provider_configs.items(): |
| 32 | + provider_key = self._validate_provider_key(provider_key) |
| 33 | + self.providers[provider_key.value] = ProviderFactory.create_provider( |
| 34 | + provider_key, config |
| 35 | + ) |
| 36 | + |
| 37 | + def _validate_provider_key(self, provider_key): |
| 38 | + """ |
| 39 | + Validate if the provider key is part of ProviderNames enum. |
| 40 | + Allow strings as well and convert them to ProviderNames. |
| 41 | + """ |
| 42 | + if isinstance(provider_key, str): |
| 43 | + if provider_key not in ProviderNames._value2member_map_: |
| 44 | + raise ValueError(f"Provider {provider_key} is not a valid provider") |
| 45 | + return ProviderNames(provider_key) |
| 46 | + |
| 47 | + if isinstance(provider_key, ProviderNames): |
| 48 | + return provider_key |
| 49 | + |
| 50 | + raise ValueError( |
| 51 | + f"Provider {provider_key} should either be a string or enum ProviderNames" |
| 52 | + ) |
| 53 | + |
| 54 | + def configure(self, provider_configs: dict = None): |
| 55 | + """ |
| 56 | + Configure the client with provider configurations. |
| 57 | + """ |
| 58 | + if provider_configs is None: |
| 59 | + return |
| 60 | + |
| 61 | + self.provider_configs.update(provider_configs) |
| 62 | + self._initialize_providers() # NOTE: This will override existing provider instances. |
| 63 | + |
| 64 | + @property |
| 65 | + def chat(self): |
| 66 | + """Return the chat API interface.""" |
| 67 | + if not self._chat: |
| 68 | + self._chat = Chat(self) |
| 69 | + return self._chat |
| 70 | + |
| 71 | + |
| 72 | +class Chat: |
| 73 | + def __init__(self, client: "Client"): |
| 74 | + self.client = client |
| 75 | + self._completions = Completions(self.client) |
| 76 | + |
| 77 | + @property |
| 78 | + def completions(self): |
| 79 | + """Return the completions interface.""" |
| 80 | + return self._completions |
| 81 | + |
| 82 | + |
| 83 | +class Completions: |
| 84 | + def __init__(self, client: "Client"): |
| 85 | + self.client = client |
| 86 | + |
| 87 | + def create(self, model: str, messages: list, **kwargs): |
| 88 | + """ |
| 89 | + Create chat completion based on the model, messages, and any extra arguments. |
| 90 | + """ |
| 91 | + # Check that correct format is used |
| 92 | + if ":" not in model: |
| 93 | + raise ValueError( |
| 94 | + f"Invalid model format. Expected 'provider:model', got '{model}'" |
| 95 | + ) |
| 96 | + |
| 97 | + # Extract the provider key from the model identifier, e.g., "aws-bedrock:model-name" |
| 98 | + provider_key, model_name = model.split(":", 1) |
| 99 | + |
| 100 | + if provider_key not in ProviderNames._value2member_map_: |
| 101 | + # If the provider key does not match, give a clearer message to guide the user |
| 102 | + valid_providers = ", ".join([p.value for p in ProviderNames]) |
| 103 | + raise ValueError( |
| 104 | + f"Invalid provider key '{provider_key}'. Expected one of: {valid_providers}. " |
| 105 | + "Make sure the model string is formatted correctly as 'provider:model'." |
| 106 | + ) |
| 107 | + |
| 108 | + if provider_key not in self.client.providers: |
| 109 | + config = {} |
| 110 | + if provider_key in self.client.provider_configs: |
| 111 | + config = self.client.provider_configs[provider_key] |
| 112 | + self.client.providers[provider_key] = ProviderFactory.create_provider( |
| 113 | + ProviderNames(provider_key), config |
| 114 | + ) |
| 115 | + |
| 116 | + provider = self.client.providers.get(provider_key) |
| 117 | + if not provider: |
| 118 | + raise ValueError(f"Could not load provider for {provider_key}.") |
| 119 | + |
| 120 | + # Delegate the chat completion to the correct provider's implementation |
| 121 | + # Any additional arguments will be passed to the provider's implementation. |
| 122 | + # Eg: max_tokens, temperature, etc. |
| 123 | + return provider.chat_completions_create(model_name, messages, **kwargs) |
0 commit comments