mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-15 22:37:24 -04:00
refactor: use model factory in BaseAgent
Replaces direct ChatOpenAI instantiation with create_model() factory. Benefits: - DeepSeek models now use native ChatDeepSeek - Other models continue using ChatOpenAI - Provider-specific optimizations in one place - Easier to add new providers Logging now shows both model name and provider class for debugging.
This commit is contained in:
@@ -12,7 +12,6 @@ from typing import Dict, List, Optional, Any
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from langchain_mcp_adapters.client import MultiServerMCPClient
|
from langchain_mcp_adapters.client import MultiServerMCPClient
|
||||||
from langchain_openai import ChatOpenAI
|
|
||||||
from langchain.agents import create_agent
|
from langchain.agents import create_agent
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
@@ -33,6 +32,7 @@ from tools.deployment_config import (
|
|||||||
from agent.context_injector import ContextInjector
|
from agent.context_injector import ContextInjector
|
||||||
from agent.pnl_calculator import DailyPnLCalculator
|
from agent.pnl_calculator import DailyPnLCalculator
|
||||||
from agent.reasoning_summarizer import ReasoningSummarizer
|
from agent.reasoning_summarizer import ReasoningSummarizer
|
||||||
|
from agent.model_factory import create_model
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@@ -126,7 +126,7 @@ class BaseAgent:
|
|||||||
# Initialize components
|
# Initialize components
|
||||||
self.client: Optional[MultiServerMCPClient] = None
|
self.client: Optional[MultiServerMCPClient] = None
|
||||||
self.tools: Optional[List] = None
|
self.tools: Optional[List] = None
|
||||||
self.model: Optional[ChatOpenAI] = None
|
self.model: Optional[Any] = None
|
||||||
self.agent: Optional[Any] = None
|
self.agent: Optional[Any] = None
|
||||||
|
|
||||||
# Context injector for MCP tools
|
# Context injector for MCP tools
|
||||||
@@ -211,14 +211,18 @@ class BaseAgent:
|
|||||||
self.model = MockChatModel(date="2025-01-01") # Date will be updated per session
|
self.model = MockChatModel(date="2025-01-01") # Date will be updated per session
|
||||||
print(f"🤖 Using MockChatModel (DEV mode)")
|
print(f"🤖 Using MockChatModel (DEV mode)")
|
||||||
else:
|
else:
|
||||||
self.model = ChatOpenAI(
|
# Use model factory for provider-specific implementations
|
||||||
model=self.basemodel,
|
self.model = create_model(
|
||||||
base_url=self.openai_base_url,
|
basemodel=self.basemodel,
|
||||||
api_key=self.openai_api_key,
|
api_key=self.openai_api_key,
|
||||||
max_retries=3,
|
base_url=self.openai_base_url,
|
||||||
|
temperature=0.7,
|
||||||
timeout=30
|
timeout=30
|
||||||
)
|
)
|
||||||
print(f"🤖 Using {self.basemodel} (PROD mode)")
|
|
||||||
|
# Determine model type for logging
|
||||||
|
model_class = self.model.__class__.__name__
|
||||||
|
print(f"🤖 Using {self.basemodel} via {model_class} (PROD mode)")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"❌ Failed to initialize AI model: {e}")
|
raise RuntimeError(f"❌ Failed to initialize AI model: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user