mirror of
https://github.com/farcasclaudiu/TradingAgents.git
synced 2026-06-29 05:01:31 +03:00
feat: add multi-provider LLM support with factory pattern
- Add tradingagents/llm_clients/ with unified factory pattern - Support OpenAI, Anthropic, Google, xAI, OpenRouter, Ollama, vLLM - Replace direct LLM imports in trading_graph.py with create_llm_client() - Handle provider-specific params (reasoning_effort, thinking_config)
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from .base_client import BaseLLMClient
|
||||
from .validators import validate_model
|
||||
|
||||
|
||||
class UnifiedChatOpenAI(ChatOpenAI):
|
||||
"""ChatOpenAI subclass that strips incompatible params for certain models."""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
model = kwargs.get("model", "")
|
||||
if self._is_reasoning_model(model):
|
||||
kwargs.pop("temperature", None)
|
||||
kwargs.pop("top_p", None)
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@staticmethod
|
||||
def _is_reasoning_model(model: str) -> bool:
|
||||
"""Check if model is a reasoning model that doesn't support temperature."""
|
||||
model_lower = model.lower()
|
||||
return (
|
||||
model_lower.startswith("o1")
|
||||
or model_lower.startswith("o3")
|
||||
or "gpt-5" in model_lower
|
||||
)
|
||||
|
||||
|
||||
class OpenAIClient(BaseLLMClient):
|
||||
"""Client for OpenAI, Ollama, OpenRouter, and xAI providers."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: str,
|
||||
base_url: Optional[str] = None,
|
||||
provider: str = "openai",
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__(model, base_url, **kwargs)
|
||||
self.provider = provider.lower()
|
||||
|
||||
def get_llm(self) -> Any:
|
||||
"""Return configured ChatOpenAI instance."""
|
||||
llm_kwargs = {"model": self.model}
|
||||
|
||||
if self.provider == "xai":
|
||||
llm_kwargs["base_url"] = "https://api.x.ai/v1"
|
||||
api_key = os.environ.get("XAI_API_KEY")
|
||||
if api_key:
|
||||
llm_kwargs["api_key"] = api_key
|
||||
elif self.base_url:
|
||||
llm_kwargs["base_url"] = self.base_url
|
||||
|
||||
for key in ("timeout", "max_retries", "reasoning_effort", "api_key"):
|
||||
if key in self.kwargs:
|
||||
llm_kwargs[key] = self.kwargs[key]
|
||||
|
||||
return UnifiedChatOpenAI(**llm_kwargs)
|
||||
|
||||
def validate_model(self) -> bool:
|
||||
"""Validate model for the provider."""
|
||||
return validate_model(self.provider, self.model)
|
||||
Reference in New Issue
Block a user