mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-09 04:07:24 -04:00
@@ -147,21 +147,41 @@ class BaseAgent:
|
|||||||
"""Initialize MCP client and AI model"""
|
"""Initialize MCP client and AI model"""
|
||||||
print(f"🚀 Initializing agent: {self.signature}")
|
print(f"🚀 Initializing agent: {self.signature}")
|
||||||
|
|
||||||
# Create MCP client
|
# Validate OpenAI configuration
|
||||||
self.client = MultiServerMCPClient(self.mcp_config)
|
if not self.openai_api_key:
|
||||||
|
raise ValueError("❌ OpenAI API key not set. Please configure OPENAI_API_KEY in environment or config file.")
|
||||||
|
if not self.openai_base_url:
|
||||||
|
print("⚠️ OpenAI base URL not set, using default")
|
||||||
|
|
||||||
# Get tools
|
try:
|
||||||
self.tools = await self.client.get_tools()
|
# Create MCP client
|
||||||
print(f"✅ Loaded {len(self.tools)} MCP tools")
|
self.client = MultiServerMCPClient(self.mcp_config)
|
||||||
|
|
||||||
|
# Get tools
|
||||||
|
self.tools = await self.client.get_tools()
|
||||||
|
if not self.tools:
|
||||||
|
print("⚠️ Warning: No MCP tools loaded. MCP services may not be running.")
|
||||||
|
print(f" MCP configuration: {self.mcp_config}")
|
||||||
|
else:
|
||||||
|
print(f"✅ Loaded {len(self.tools)} MCP tools")
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"❌ Failed to initialize MCP client: {e}\n"
|
||||||
|
f" Please ensure MCP services are running at the configured ports.\n"
|
||||||
|
f" Run: python agent_tools/start_mcp_services.py"
|
||||||
|
)
|
||||||
|
|
||||||
# Create AI model
|
try:
|
||||||
self.model = ChatOpenAI(
|
# Create AI model
|
||||||
model=self.basemodel,
|
self.model = ChatOpenAI(
|
||||||
base_url=self.openai_base_url,
|
model=self.basemodel,
|
||||||
api_key=self.openai_api_key,
|
base_url=self.openai_base_url,
|
||||||
max_retries=3,
|
api_key=self.openai_api_key,
|
||||||
timeout=30
|
max_retries=3,
|
||||||
)
|
timeout=30
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(f"❌ Failed to initialize AI model: {e}")
|
||||||
|
|
||||||
# Note: agent will be created in run_trading_session() based on specific date
|
# Note: agent will be created in run_trading_session() based on specific date
|
||||||
# because system_prompt needs the current date and price information
|
# because system_prompt needs the current date and price information
|
||||||
|
|||||||
@@ -2,11 +2,14 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
def _load_runtime_env() -> dict:
|
def _load_runtime_env() -> dict:
|
||||||
path = os.environ.get("RUNTIME_ENV_PATH")
|
path = os.environ.get("RUNTIME_ENV_PATH")
|
||||||
|
if path is None:
|
||||||
|
return {}
|
||||||
try:
|
try:
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
@@ -25,12 +28,18 @@ def get_config_value(key: str, default=None):
|
|||||||
return _RUNTIME_ENV[key]
|
return _RUNTIME_ENV[key]
|
||||||
return os.getenv(key, default)
|
return os.getenv(key, default)
|
||||||
|
|
||||||
def write_config_value(key: str, value: any):
|
def write_config_value(key: str, value: Any):
|
||||||
|
path = os.environ.get("RUNTIME_ENV_PATH")
|
||||||
|
if path is None:
|
||||||
|
print(f"⚠️ WARNING: RUNTIME_ENV_PATH not set, config value '{key}' not persisted")
|
||||||
|
return
|
||||||
_RUNTIME_ENV = _load_runtime_env()
|
_RUNTIME_ENV = _load_runtime_env()
|
||||||
_RUNTIME_ENV[key] = value
|
_RUNTIME_ENV[key] = value
|
||||||
path = os.environ.get("RUNTIME_ENV_PATH")
|
try:
|
||||||
with open(path, "w", encoding="utf-8") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
json.dump(_RUNTIME_ENV, f, ensure_ascii=False, indent=4)
|
json.dump(_RUNTIME_ENV, f, ensure_ascii=False, indent=4)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error writing config to {path}: {e}")
|
||||||
|
|
||||||
def extract_conversation(conversation: dict, output_type: str):
|
def extract_conversation(conversation: dict, output_type: str):
|
||||||
"""Extract information from a conversation payload.
|
"""Extract information from a conversation payload.
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ load_dotenv()
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional, Tuple
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# 将项目根目录加入 Python 路径,便于从子目录直接运行本文件
|
# 将项目根目录加入 Python 路径,便于从子目录直接运行本文件
|
||||||
@@ -95,7 +95,7 @@ def get_open_prices(today_date: str, symbols: List[str], merged_path: Optional[s
|
|||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def get_yesterday_open_and_close_price(today_date: str, symbols: List[str], merged_path: Optional[str] = None) -> tuple[Dict[str, Optional[float]], Dict[str, Optional[float]]]:
|
def get_yesterday_open_and_close_price(today_date: str, symbols: List[str], merged_path: Optional[str] = None) -> Tuple[Dict[str, Optional[float]], Dict[str, Optional[float]]]:
|
||||||
"""从 data/merged.jsonl 中读取指定日期与股票的昨日买入价和卖出价。
|
"""从 data/merged.jsonl 中读取指定日期与股票的昨日买入价和卖出价。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -260,7 +260,7 @@ def get_today_init_position(today_date: str, modelname: str) -> Dict[str, float]
|
|||||||
|
|
||||||
return latest_positions
|
return latest_positions
|
||||||
|
|
||||||
def get_latest_position(today_date: str, modelname: str) -> Dict[str, float]:
|
def get_latest_position(today_date: str, modelname: str) -> Tuple[Dict[str, float], int]:
|
||||||
"""
|
"""
|
||||||
获取最新持仓。从 ../data/agent_data/{modelname}/position/position.jsonl 中读取。
|
获取最新持仓。从 ../data/agent_data/{modelname}/position/position.jsonl 中读取。
|
||||||
优先选择当天 (today_date) 中 id 最大的记录;
|
优先选择当天 (today_date) 中 id 最大的记录;
|
||||||
@@ -273,7 +273,7 @@ def get_latest_position(today_date: str, modelname: str) -> Dict[str, float]:
|
|||||||
Returns:
|
Returns:
|
||||||
(positions, max_id):
|
(positions, max_id):
|
||||||
- positions: {symbol: weight} 的字典;若未找到任何记录,则为空字典。
|
- positions: {symbol: weight} 的字典;若未找到任何记录,则为空字典。
|
||||||
- max_id: 选中记录的最大 id;若未找到任何记录,则为 -1。
|
- max_id: 选中记录的最大 id;若未找到任何记录,则为 -1.
|
||||||
"""
|
"""
|
||||||
base_dir = Path(__file__).resolve().parents[1]
|
base_dir = Path(__file__).resolve().parents[1]
|
||||||
position_file = base_dir / "data" / "agent_data" / modelname / "position" / "position.jsonl"
|
position_file = base_dir / "data" / "agent_data" / modelname / "position" / "position.jsonl"
|
||||||
|
|||||||
Reference in New Issue
Block a user