Skip to content

Commit 666eea9

Browse files
Adam BaloghAdam Balogh
authored andcommitted
suggestions simplify
1 parent ef777ee commit 666eea9

4 files changed

Lines changed: 42 additions & 76 deletions

File tree

agent/agent_executors.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
)
2020
GROK_MODEL = "x-ai/grok-2-1212" # $2/M input tokens; $10/M output tokens
2121

22-
SUGGESTIONS_MODEL = GOOGLE_GEMINI_20_FLASH_MODEL
22+
SUGGESTIONS_MODEL = GOOGLE_GEMINI_FLASH_15_8B_MODEL
2323
ROUTING_MODEL = GOOGLE_GEMINI_FLASH_15_8B_MODEL
2424
REASONING_MODEL = GROK_MODEL
2525

@@ -35,23 +35,17 @@ def create_routing_model() -> ChatOpenAI:
3535
)
3636

3737

38-
def create_suggestions_executor() -> CompiledGraph:
39-
openai_model = ChatOpenAI(
38+
def create_suggestions_model() -> ChatOpenAI:
39+
return ChatOpenAI(
4040
model=SUGGESTIONS_MODEL,
4141
temperature=0.0,
4242
openai_api_base="https://openrouter.ai/api/v1",
4343
openai_api_key=os.getenv("OPENROUTER_API_KEY"),
4444
request_timeout=60,
45-
max_tokens=100,
45+
max_tokens=500,
4646
streaming=False,
4747
default_headers={"X-Title": "two-ligma"},
4848
)
49-
agent_executor = create_react_agent(
50-
model=openai_model,
51-
tools=[],
52-
)
53-
54-
return agent_executor
5549

5650

5751
def create_investor_executor() -> CompiledGraph:

agent/prompts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def get_investor_agent_prompt(
4747

4848

4949
def get_suggestions_prompt(
50+
conversation_history: List[Message],
5051
tokens: List[WalletTokenHolding],
5152
tools: str,
5253
) -> str:
@@ -68,6 +69,7 @@ def get_suggestions_prompt(
6869

6970
agent_prompt = suggestions_template.render(
7071
tokens=token_metadata,
72+
conversation_history=conversation_history,
7173
tools=tools,
7274
)
7375

server/server.py

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
)
3434
from agent.agent_executors import (
3535
create_investor_executor,
36-
create_suggestions_executor,
36+
create_suggestions_model,
3737
create_analytics_executor,
3838
create_routing_model,
3939
)
@@ -101,12 +101,11 @@ def create_flask_app() -> Flask:
101101
invite_manager = InviteCodeManager(invite_codes_table)
102102

103103
# Initialize agents
104-
suggestions_agent = create_suggestions_executor()
104+
router_model = create_routing_model()
105+
suggestions_model = create_suggestions_model()
105106
analytics_agent = create_analytics_executor()
106107
investor_agent = create_investor_executor()
107108

108-
router_model = create_routing_model()
109-
110109
token_metadata_repo = TokenMetadataRepo(tokens_table)
111110
portfolio_fetcher = PortfolioFetcher(token_metadata_repo)
112111

@@ -229,7 +228,7 @@ def run_suggestions():
229228
suggestions = handle_suggestions_request(
230229
request=agent_request,
231230
portfolio=portfolio,
232-
suggestions_agent=suggestions_agent,
231+
suggestions_model=suggestions_model,
233232
)
234233
return jsonify({"suggestions": suggestions})
235234

@@ -435,43 +434,48 @@ def handle_investor_chat_request(
435434
def handle_suggestions_request(
436435
request: AgentChatRequest,
437436
portfolio: Portfolio,
438-
suggestions_agent: CompiledGraph,
437+
suggestions_model: ChatOpenAI,
439438
) -> List[str]:
440439
# Get tools from agent config and format them
441440
tools = create_investor_agent_toolkit() + create_analytics_agent_toolkit()
442441
tools_list = "\n".join([f"- {tool.name}: {tool.description}" for tool in tools])
443442

444-
# Build suggestions agent system prompt
443+
# Build suggestions system prompt
445444
suggestions_system_prompt = get_suggestions_prompt(
445+
conversation_history=request.context.conversationHistory,
446446
tokens=portfolio.holdings,
447447
tools=tools_list,
448448
)
449449

450-
# Prepare message history (last 10 messages)
451-
message_history = [
452-
convert_to_agent_msg(m)
453-
for m in request.context.conversationHistory[-NUM_MESSAGES_TO_KEEP:]
454-
]
450+
# Run suggestions model directly
451+
response = suggestions_model.invoke(suggestions_system_prompt)
452+
content = response.content
455453

456-
# Create messages for suggestions agent
457-
suggestions_messages = [
458-
("system", suggestions_system_prompt),
459-
*message_history,
460-
]
461-
462-
# Create config for the agent
463-
agent_config = RunnableConfig(
464-
configurable={
465-
"tokens": portfolio.holdings,
466-
}
467-
)
454+
# Clean the content by removing markdown code block syntax if present
455+
if content.startswith("```json"):
456+
content = content[7:] # Remove ```json
457+
if content.endswith("```"):
458+
content = content[:-3] # Remove ```
459+
content = content.strip()
468460

469-
# Run suggestions agent
470-
suggestions = run_suggestions_agent(
471-
suggestions_agent, suggestions_messages, agent_config
472-
)
461+
try:
462+
# First try parsing as JSON
463+
suggestions = json.loads(content)
464+
if isinstance(suggestions, list):
465+
return suggestions
466+
except json.JSONDecodeError:
467+
# If JSON parsing fails, try parsing as string array
468+
try:
469+
# Remove any JSON-like syntax and split by comma
470+
cleaned = content.strip("[]")
471+
# Split by comma and remove quotes
472+
suggestions = [item.strip().strip("'\"") for item in cleaned.split(",")]
473+
return suggestions
474+
except Exception as e:
475+
logger.error(f"Error parsing suggestions string: {e}")
476+
return []
473477

474-
return suggestions
478+
return []
475479

476480

477481
def run_main_agent(
@@ -510,43 +514,6 @@ def run_main_agent(
510514
}
511515

512516

513-
def run_suggestions_agent(
514-
agent: CompiledGraph, messages: List, config: RunnableConfig
515-
) -> List[str]:
516-
# Run agent directly
517-
result = agent.invoke({"messages": messages}, config=config)
518-
519-
# Extract final message
520-
last_message = result["messages"][-1]
521-
522-
# Clean the content by removing markdown code block syntax if present
523-
content = last_message.content
524-
if content.startswith("```json"):
525-
content = content[7:] # Remove ```json
526-
if content.endswith("```"):
527-
content = content[:-3] # Remove ```
528-
content = content.strip()
529-
530-
try:
531-
# First try parsing as JSON
532-
suggestions = json.loads(content)
533-
if isinstance(suggestions, list):
534-
return suggestions
535-
except json.JSONDecodeError:
536-
# If JSON parsing fails, try parsing as string array
537-
try:
538-
# Remove any JSON-like syntax and split by comma
539-
cleaned = content.strip("[]")
540-
# Split by comma and remove quotes
541-
suggestions = [item.strip().strip("'\"") for item in cleaned.split(",")]
542-
return suggestions
543-
except Exception as e:
544-
logger.error(f"Error parsing suggestions string: {e}")
545-
return []
546-
547-
return []
548-
549-
550517
def convert_to_agent_msg(message: Message) -> Tuple[str, str]:
551518
if isinstance(message, UserMessage):
552519
return ("user", message.message)

templates/suggestions.jinja2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ EXAMPLE RESPONSES:
4545
- After risk concern: ["Check max drawdown", "Portfolio volatility", "Compare Ethereum top pools", "Global TVL data"]
4646

4747
User's Solana wallet tokens: {{ tokens }}
48+
49+
Conversation history:
50+
{{ conversation_history }}

0 commit comments

Comments
 (0)