| """ |
| LLM client factory for creating the appropriate client based on model type. |
| """ |
|
|
| from typing import Literal, Union, Optional |
| from src.config.settings import get_settings, LLM_MODELS, get_llm_model_config |
| from src.llm.claude_client import ClaudeClient |
| from src.llm.grok_client import GrokClient |
| from src.utils.logging import get_logger |
|
|
| logger = get_logger(__name__) |
|
|
| LLMType = Literal["claude-sonnet", "claude-opus", "grok", "claude"] |
|
|
|
|
| def get_llm_client(llm_type: Optional[LLMType] = None) -> Union[ClaudeClient, GrokClient]: |
| """ |
| Get an LLM client instance based on the specified type. |
| |
| Args: |
| llm_type: Type of LLM ("claude-sonnet", "claude-opus", or "grok"). |
| Uses default from settings if None. |
| |
| Returns: |
| LLM client instance (ClaudeClient or GrokClient) |
| """ |
| settings = get_settings() |
|
|
| |
| if llm_type == "claude": |
| llm_type = "claude-sonnet" |
|
|
| llm = llm_type or "claude-sonnet" |
|
|
| logger.info(f"Creating LLM client: {llm}") |
|
|
| |
| if llm not in LLM_MODELS: |
| raise ValueError(f"Unknown LLM type: {llm}. Available: {list(LLM_MODELS.keys())}") |
|
|
| model_config = LLM_MODELS[llm] |
| provider = model_config.get("provider", "anthropic") |
|
|
| if provider == "anthropic": |
| return ClaudeClient(model_id=model_config["model_id"]) |
| elif provider == "xai": |
| return GrokClient() |
| else: |
| raise ValueError(f"Unknown provider: {provider}") |
|
|
|
|
| def get_available_models() -> list[dict]: |
| """ |
| Get list of available LLM models. |
| |
| Returns: |
| List of model info dictionaries |
| """ |
| settings = get_settings() |
|
|
| models = [] |
|
|
| |
| claude_available = bool(settings.anthropic_api_key) |
| for model_key, config in LLM_MODELS.items(): |
| if config.get("provider") == "anthropic": |
| models.append({ |
| "id": model_key, |
| "name": config["name"], |
| "model": config["model_id"], |
| "description": config.get("description", ""), |
| "available": claude_available, |
| }) |
|
|
| |
| grok_available = ( |
| settings.grok_api_key |
| and settings.grok_api_key != "xai-your-key-here" |
| ) |
| grok_config = LLM_MODELS.get("grok", {}) |
| models.append({ |
| "id": "grok", |
| "name": grok_config.get("name", "Grok"), |
| "model": grok_config.get("model_id", settings.grok_model), |
| "description": grok_config.get("description", ""), |
| "available": grok_available, |
| }) |
|
|
| return models |
|
|