Initialize LLM client (Anthropic or OpenAI) based on API key availability
(self)
| 309 | return {} |
| 310 | |
| 311 | async def _initialize_llm_client(self): |
| 312 | """Initialize LLM client (Anthropic or OpenAI) based on API key availability""" |
| 313 | if self.llm_client is not None: |
| 314 | return self.llm_client, self.llm_client_type |
| 315 | |
| 316 | # Check if mock responses are enabled |
| 317 | if self.mock_llm_responses: |
| 318 | self.logger.info("Using mock LLM responses for testing") |
| 319 | self.llm_client = "mock" |
| 320 | self.llm_client_type = "mock" |
| 321 | return "mock", "mock" |
| 322 | |
| 323 | # Check which API has available key and try that first |
| 324 | anthropic_key = self.api_config.get("anthropic", {}).get("api_key", "") |
| 325 | openai_key = self.api_config.get("openai", {}).get("api_key", "") |
| 326 | |
| 327 | # Try Anthropic API first if key is available |
| 328 | if anthropic_key and anthropic_key.strip(): |
| 329 | try: |
| 330 | from anthropic import AsyncAnthropic |
| 331 | |
| 332 | client = AsyncAnthropic(api_key=anthropic_key) |
| 333 | # Test connection with default model from config |
| 334 | await client.messages.create( |
| 335 | model=self.default_models["anthropic"], |
| 336 | max_tokens=10, |
| 337 | messages=[{"role": "user", "content": "test"}], |
| 338 | ) |
| 339 | self.logger.info( |
| 340 | f"Using Anthropic API with model: {self.default_models['anthropic']}" |
| 341 | ) |
| 342 | self.llm_client = client |
| 343 | self.llm_client_type = "anthropic" |
| 344 | return client, "anthropic" |
| 345 | except Exception as e: |
| 346 | self.logger.warning(f"Anthropic API unavailable: {e}") |
| 347 | |
| 348 | # Try OpenAI API if Anthropic failed or key not available |
| 349 | if openai_key and openai_key.strip(): |
| 350 | try: |
| 351 | from openai import AsyncOpenAI |
| 352 | |
| 353 | # Handle custom base_url if specified |
| 354 | openai_config = self.api_config.get("openai", {}) |
| 355 | base_url = openai_config.get("base_url") |
| 356 | |
| 357 | if base_url: |
| 358 | client = AsyncOpenAI(api_key=openai_key, base_url=base_url) |
| 359 | else: |
| 360 | client = AsyncOpenAI(api_key=openai_key) |
| 361 | |
| 362 | # Test connection with default model from config |
| 363 | await client.chat.completions.create( |
| 364 | model=self.default_models["openai"], |
| 365 | max_tokens=10, |
| 366 | messages=[{"role": "user", "content": "test"}], |
| 367 | ) |
| 368 | self.logger.info( |