Unified LLM client using LiteLLM.
| 84 | |
| 85 | |
| 86 | class LLMClient: |
| 87 | """Unified LLM client using LiteLLM.""" |
| 88 | |
| 89 | def __init__(self, config: Optional[LLMConfig] = None): |
| 90 | self.config = config or LLMConfig() |
| 91 | |
| 92 | def completion( |
| 93 | self, |
| 94 | model: str, |
| 95 | messages: List[Dict[str, Any]], |
| 96 | *, |
| 97 | tools: Optional[List[Dict[str, Any]]] = None, |
| 98 | tool_choice: Optional[str] = None, |
| 99 | temperature: Optional[float] = None, |
| 100 | max_tokens: Optional[int] = None, |
| 101 | timeout: Optional[int] = None, |
| 102 | num_retries: Optional[int] = None, |
| 103 | api_base: Optional[str] = None, |
| 104 | api_key: Optional[str] = None, |
| 105 | **kwargs, |
| 106 | ) -> Any: |
| 107 | """Make a chat completion request with automatic retry.""" |
| 108 | normalized_model = normalize_model_name(model) |
| 109 | provider = get_provider_from_model(model) |
| 110 | |
| 111 | completion_kwargs: Dict[str, Any] = { |
| 112 | "model": normalized_model, |
| 113 | "messages": messages, |
| 114 | "timeout": timeout or self.config.timeout, |
| 115 | "num_retries": ( |
| 116 | num_retries if num_retries is not None else self.config.num_retries |
| 117 | ), |
| 118 | } |
| 119 | |
| 120 | if tools: |
| 121 | completion_kwargs["tools"] = tools |
| 122 | if tool_choice and tools: |
| 123 | completion_kwargs["tool_choice"] = tool_choice |
| 124 | if temperature is not None: |
| 125 | completion_kwargs["temperature"] = temperature |
| 126 | elif self.config.temperature is not None: |
| 127 | completion_kwargs["temperature"] = self.config.temperature |
| 128 | if max_tokens is not None: |
| 129 | completion_kwargs["max_tokens"] = max_tokens |
| 130 | |
| 131 | effective_api_base = api_base or self.config.api_base |
| 132 | if effective_api_base: |
| 133 | completion_kwargs["api_base"] = effective_api_base |
| 134 | |
| 135 | # Prioritize provider-specific API keys, then explicit api_key, then default |
| 136 | if provider in self.config._env_keys: |
| 137 | completion_kwargs["api_key"] = self.config._env_keys[provider] |
| 138 | elif api_key: |
| 139 | completion_kwargs["api_key"] = api_key |
| 140 | elif self.config.api_key: |
| 141 | completion_kwargs["api_key"] = self.config.api_key |
| 142 | |
| 143 | if self.config.drop_params: |
no outgoing calls