mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-10 12:47:25 -04:00
Compare commits
4 Commits
v0.4.0-alp
...
v0.4.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 31e346ecbb | |||
| abb9cd0726 | |||
| 6d126db03c | |||
| 1e7bdb509b |
@@ -419,7 +419,7 @@ class BaseAgent:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if tool_name:
|
if tool_name:
|
||||||
message["tool_name"] = tool_name
|
message["name"] = tool_name # Use "name" not "tool_name" for consistency with summarizer
|
||||||
if tool_input:
|
if tool_input:
|
||||||
message["tool_input"] = tool_input
|
message["tool_input"] = tool_input
|
||||||
|
|
||||||
@@ -643,6 +643,11 @@ Summary:"""
|
|||||||
tool_msgs = extract_tool_messages(response)
|
tool_msgs = extract_tool_messages(response)
|
||||||
for tool_msg in tool_msgs:
|
for tool_msg in tool_msgs:
|
||||||
tool_name = getattr(tool_msg, 'name', None) or tool_msg.get('name') if isinstance(tool_msg, dict) else None
|
tool_name = getattr(tool_msg, 'name', None) or tool_msg.get('name') if isinstance(tool_msg, dict) else None
|
||||||
|
tool_content = getattr(tool_msg, 'content', '') or tool_msg.get('content', '') if isinstance(tool_msg, dict) else str(tool_msg)
|
||||||
|
|
||||||
|
# Capture tool message to conversation history
|
||||||
|
self._capture_message("tool", tool_content, tool_name=tool_name)
|
||||||
|
|
||||||
if tool_name in ['buy', 'sell']:
|
if tool_name in ['buy', 'sell']:
|
||||||
action_count += 1
|
action_count += 1
|
||||||
|
|
||||||
@@ -665,6 +670,15 @@ Summary:"""
|
|||||||
session_duration = time.time() - session_start
|
session_duration = time.time() - session_start
|
||||||
|
|
||||||
# 7. Generate reasoning summary
|
# 7. Generate reasoning summary
|
||||||
|
# Debug: Log conversation history size
|
||||||
|
print(f"\n[DEBUG] Generating summary from {len(self.conversation_history)} messages")
|
||||||
|
assistant_msgs = [m for m in self.conversation_history if m.get('role') == 'assistant']
|
||||||
|
tool_msgs = [m for m in self.conversation_history if m.get('role') == 'tool']
|
||||||
|
print(f"[DEBUG] Assistant messages: {len(assistant_msgs)}, Tool messages: {len(tool_msgs)}")
|
||||||
|
if assistant_msgs:
|
||||||
|
first_assistant = assistant_msgs[0]
|
||||||
|
print(f"[DEBUG] First assistant message preview: {first_assistant.get('content', '')[:200]}...")
|
||||||
|
|
||||||
summarizer = ReasoningSummarizer(model=self.model)
|
summarizer = ReasoningSummarizer(model=self.model)
|
||||||
summary = await summarizer.generate_summary(self.conversation_history)
|
summary = await summarizer.generate_summary(self.conversation_history)
|
||||||
|
|
||||||
|
|||||||
@@ -52,10 +52,6 @@ class ContextInjector:
|
|||||||
"""
|
"""
|
||||||
# Inject context parameters for trade tools
|
# Inject context parameters for trade tools
|
||||||
if request.name in ["buy", "sell"]:
|
if request.name in ["buy", "sell"]:
|
||||||
# Debug: Log self attributes BEFORE injection
|
|
||||||
print(f"[ContextInjector.__call__] ENTRY: id={id(self)}, self.signature={self.signature}, self.today_date={self.today_date}, self.job_id={self.job_id}, self.session_id={self.session_id}, self.trading_day_id={self.trading_day_id}")
|
|
||||||
print(f"[ContextInjector.__call__] Args BEFORE injection: {request.args}")
|
|
||||||
|
|
||||||
# ALWAYS inject/override context parameters (don't trust AI-provided values)
|
# ALWAYS inject/override context parameters (don't trust AI-provided values)
|
||||||
request.args["signature"] = self.signature
|
request.args["signature"] = self.signature
|
||||||
request.args["today_date"] = self.today_date
|
request.args["today_date"] = self.today_date
|
||||||
@@ -66,8 +62,5 @@ class ContextInjector:
|
|||||||
if self.trading_day_id:
|
if self.trading_day_id:
|
||||||
request.args["trading_day_id"] = self.trading_day_id
|
request.args["trading_day_id"] = self.trading_day_id
|
||||||
|
|
||||||
# Debug logging
|
|
||||||
print(f"[ContextInjector] Tool: {request.name}, Args after injection: {request.args}")
|
|
||||||
|
|
||||||
# Call the actual tool handler
|
# Call the actual tool handler
|
||||||
return await handler(request)
|
return await handler(request)
|
||||||
|
|||||||
@@ -36,15 +36,17 @@ class ReasoningSummarizer:
|
|||||||
summary_prompt = f"""You are reviewing your own trading decisions for the day.
|
summary_prompt = f"""You are reviewing your own trading decisions for the day.
|
||||||
Summarize your trading strategy and key decisions in 2-3 sentences.
|
Summarize your trading strategy and key decisions in 2-3 sentences.
|
||||||
|
|
||||||
|
IMPORTANT: Explicitly state what trades you executed (e.g., "sold 2 GOOGL shares" or "bought 10 NVDA shares"). If you made no trades, state that clearly.
|
||||||
|
|
||||||
Focus on:
|
Focus on:
|
||||||
- What you analyzed
|
- What specific trades you executed (buy/sell, symbols, quantities)
|
||||||
- Why you made the trades you did
|
- Why you made those trades
|
||||||
- Your overall strategy for the day
|
- Your overall strategy for the day
|
||||||
|
|
||||||
Trading session log:
|
Trading session log:
|
||||||
{log_text}
|
{log_text}
|
||||||
|
|
||||||
Provide a concise summary:"""
|
Provide a concise summary that includes the actual trades executed:"""
|
||||||
|
|
||||||
response = await self.model.ainvoke([
|
response = await self.model.ainvoke([
|
||||||
{"role": "user", "content": summary_prompt}
|
{"role": "user", "content": summary_prompt}
|
||||||
@@ -67,21 +69,39 @@ Provide a concise summary:"""
|
|||||||
reasoning_log: List of message dicts
|
reasoning_log: List of message dicts
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Formatted text representation
|
Formatted text representation with emphasis on trades
|
||||||
"""
|
"""
|
||||||
|
# Debug: Log what we're formatting
|
||||||
|
print(f"[DEBUG ReasoningSummarizer] Formatting {len(reasoning_log)} messages")
|
||||||
|
assistant_count = sum(1 for m in reasoning_log if m.get('role') == 'assistant')
|
||||||
|
tool_count = sum(1 for m in reasoning_log if m.get('role') == 'tool')
|
||||||
|
print(f"[DEBUG ReasoningSummarizer] Breakdown: {assistant_count} assistant, {tool_count} tool")
|
||||||
|
|
||||||
formatted_parts = []
|
formatted_parts = []
|
||||||
|
trades_executed = []
|
||||||
|
|
||||||
for msg in reasoning_log:
|
for msg in reasoning_log:
|
||||||
role = msg.get("role", "")
|
role = msg.get("role", "")
|
||||||
content = msg.get("content", "")
|
content = msg.get("content", "")
|
||||||
|
tool_name = msg.get("name", "")
|
||||||
|
|
||||||
if role == "assistant":
|
if role == "assistant":
|
||||||
# AI's thoughts
|
# AI's thoughts
|
||||||
formatted_parts.append(f"AI: {content[:200]}")
|
formatted_parts.append(f"AI: {content[:200]}")
|
||||||
elif role == "tool":
|
elif role == "tool":
|
||||||
# Tool results
|
# Highlight trade tool calls
|
||||||
tool_name = msg.get("name", "tool")
|
if tool_name in ["buy", "sell"]:
|
||||||
formatted_parts.append(f"{tool_name}: {content[:100]}")
|
trades_executed.append(f"{tool_name.upper()}: {content[:150]}")
|
||||||
|
formatted_parts.append(f"TRADE - {tool_name.upper()}: {content[:150]}")
|
||||||
|
else:
|
||||||
|
# Other tool results (search, price, etc.)
|
||||||
|
formatted_parts.append(f"{tool_name}: {content[:100]}")
|
||||||
|
|
||||||
|
# Add summary of trades at the top
|
||||||
|
if trades_executed:
|
||||||
|
trade_summary = f"TRADES EXECUTED ({len(trades_executed)}):\n" + "\n".join(trades_executed)
|
||||||
|
formatted_parts.insert(0, trade_summary)
|
||||||
|
formatted_parts.insert(1, "\n--- FULL LOG ---")
|
||||||
|
|
||||||
return "\n".join(formatted_parts)
|
return "\n".join(formatted_parts)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user