Skip to content

Commit

Permalink
feat: Alter typing of config properties
Browse files Browse the repository at this point in the history
reindervdw-cmi committed Jan 13, 2025
1 parent 1a5df56 commit 8419c4f
Showing 3 changed files with 37 additions and 26 deletions.
22 changes: 11 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ fastapi = "^0.115.6"
pydantic = "^2.10.4"
uvicorn = "^0.34.0"
cloai = "^1.0.0"
openai = "^1.59.7"

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.3"
40 changes: 25 additions & 15 deletions src/cloaiservice/config.py
Original file line number Diff line number Diff line change
@@ -5,26 +5,36 @@
from os import environ
from typing import Literal

import fastapi
import pydantic
from fastapi import status

import cloai
from cloai.llm import bedrock as cloai_bedrock
from openai.types import chat_model
from pydantic import BaseModel, Field


class AnthropicConfig(BaseModel):
"""Anthropic client configuration."""
class BedrockAnthropicConfig(BaseModel):
"""Bedrock Anthropic client configuration."""

type: Literal["anthropic"]
model: str
aws_access_key: str
aws_secret_key: str
type: Literal["bedrock-anthropic"]
model: cloai_bedrock.ANTHROPIC_BEDROCK_MODELS
aws_access_key: pydantic.SecretStr = pydantic.Field(
..., min_length=20, max_length=20
)
aws_secret_key: pydantic.SecretStr = pydantic.Field(
..., min_length=40, max_length=40
)
region: str

def create_client(self) -> cloai.LargeLanguageModel:
"""Create an Anthropic client instance."""
return cloai.LargeLanguageModel(
client=cloai.AnthropicBedrockLlm(
model=self.model, # type: ignore # don't want to need to keep literal union in sync with cloai
aws_access_key=self.aws_access_key,
aws_secret_key=self.aws_secret_key,
model=self.model,
aws_access_key=self.aws_access_key.get_secret_value(),
aws_secret_key=self.aws_secret_key.get_secret_value(),
region=self.region,
)
)
@@ -34,16 +44,16 @@ class OpenAIConfig(BaseModel):
"""OpenAI client configuration."""

type: Literal["openai"]
model: str
api_key: str
model: chat_model.ChatModel | str
api_key: pydantic.SecretStr
base_url: str | None = Field(default=None)

def create_client(self) -> cloai.LargeLanguageModel:
"""Create an OpenAI client instance."""
return cloai.LargeLanguageModel(
client=cloai.OpenAiLlm(
model=self.model,
api_key=self.api_key,
api_key=self.api_key.get_secret_value(),
base_url=self.base_url,
)
)
@@ -53,7 +63,7 @@ class AzureConfig(BaseModel):
"""Azure client configuration."""

type: Literal["azure"]
api_key: str
api_key: pydantic.SecretStr
endpoint: str
deployment: str
api_version: str
@@ -62,7 +72,7 @@ def create_client(self) -> cloai.LargeLanguageModel:
"""Create an Azure client instance."""
return cloai.LargeLanguageModel(
client=cloai.AzureLlm(
api_key=self.api_key,
api_key=self.api_key.get_secret_value(),
endpoint=self.endpoint,
deployment=self.deployment,
api_version=self.api_version,
@@ -73,7 +83,7 @@ def create_client(self) -> cloai.LargeLanguageModel:
class ClientConfig(BaseModel):
"""Configuration for all clients."""

clients: dict[str, AnthropicConfig | OpenAIConfig | AzureConfig]
clients: dict[str, BedrockAnthropicConfig | OpenAIConfig | AzureConfig]

def create_clients(self) -> dict[str, cloai.LargeLanguageModel]:
"""Create all client instances."""

0 comments on commit 8419c4f

Please sign in to comment.