|
33 | 33 | ) |
34 | 34 | from agent.agent_executors import ( |
35 | 35 | create_investor_executor, |
36 | | - create_suggestions_executor, |
| 36 | + create_suggestions_model, |
37 | 37 | create_analytics_executor, |
38 | 38 | create_routing_model, |
39 | 39 | ) |
@@ -101,12 +101,11 @@ def create_flask_app() -> Flask: |
101 | 101 | invite_manager = InviteCodeManager(invite_codes_table) |
102 | 102 |
|
103 | 103 | # Initialize agents |
104 | | - suggestions_agent = create_suggestions_executor() |
| 104 | + router_model = create_routing_model() |
| 105 | + suggestions_model = create_suggestions_model() |
105 | 106 | analytics_agent = create_analytics_executor() |
106 | 107 | investor_agent = create_investor_executor() |
107 | 108 |
|
108 | | - router_model = create_routing_model() |
109 | | - |
110 | 109 | token_metadata_repo = TokenMetadataRepo(tokens_table) |
111 | 110 | portfolio_fetcher = PortfolioFetcher(token_metadata_repo) |
112 | 111 |
|
@@ -229,7 +228,7 @@ def run_suggestions(): |
229 | 228 | suggestions = handle_suggestions_request( |
230 | 229 | request=agent_request, |
231 | 230 | portfolio=portfolio, |
232 | | - suggestions_agent=suggestions_agent, |
| 231 | + suggestions_model=suggestions_model, |
233 | 232 | ) |
234 | 233 | return jsonify({"suggestions": suggestions}) |
235 | 234 |
|
@@ -435,43 +434,48 @@ def handle_investor_chat_request( |
435 | 434 | def handle_suggestions_request( |
436 | 435 | request: AgentChatRequest, |
437 | 436 | portfolio: Portfolio, |
438 | | - suggestions_agent: CompiledGraph, |
| 437 | + suggestions_model: ChatOpenAI, |
439 | 438 | ) -> List[str]: |
440 | 439 | # Get tools from agent config and format them |
441 | 440 | tools = create_investor_agent_toolkit() + create_analytics_agent_toolkit() |
442 | 441 | tools_list = "\n".join([f"- {tool.name}: {tool.description}" for tool in tools]) |
443 | 442 |
|
444 | | - # Build suggestions agent system prompt |
| 443 | + # Build suggestions system prompt |
445 | 444 | suggestions_system_prompt = get_suggestions_prompt( |
| 445 | + conversation_history=request.context.conversationHistory, |
446 | 446 | tokens=portfolio.holdings, |
447 | 447 | tools=tools_list, |
448 | 448 | ) |
449 | 449 |
|
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 |
455 | 453 |
|
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() |
468 | 460 |
|
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 [] |
473 | 477 |
|
474 | | - return suggestions |
| 478 | + return [] |
475 | 479 |
|
476 | 480 |
|
477 | 481 | def run_main_agent( |
@@ -510,43 +514,6 @@ def run_main_agent( |
510 | 514 | } |
511 | 515 |
|
512 | 516 |
|
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 | | - |
550 | 517 | def convert_to_agent_msg(message: Message) -> Tuple[str, str]: |
551 | 518 | if isinstance(message, UserMessage): |
552 | 519 | return ("user", message.message) |
|
0 commit comments