ABVM commited on
Commit
51abc3c
·
verified ·
1 Parent(s): 4985a13

Upload OpenRouter_Agent.py

Browse files
Files changed (1) hide show
  1. OpenRouter_Agent.py +133 -0
OpenRouter_Agent.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """High level multi-agent system powered by OpenRouter models.
2
+
3
+ This module sets up a manager agent that delegates tasks to specialized
4
+ web and information agents. It relies on the ``smolagent`` framework and
5
+ OpenRouter API models for language generation and verification.
6
+ """
7
+
8
+ from smolagents import (
9
+ CodeAgent,
10
+ VisitWebpageTool,
11
+ WebSearchTool,
12
+ WikipediaSearchTool,
13
+ PythonInterpreterTool,
14
+ FinalAnswerTool,
15
+ OpenAIServerModel,
16
+ )
17
+ from smolagents.utils import encode_image_base64, make_image_url
18
+ from vision_tool import image_reasoning_tool
19
+ import os
20
+
21
+ OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
22
+ if not OPENROUTER_API_KEY:
23
+ raise EnvironmentError("OPENROUTER_API_KEY environment variable not set")
24
+
25
+ common = dict(
26
+ api_base="https://openrouter.ai/api/v1",
27
+ api_key=OPENROUTER_API_KEY,
28
+ )
29
+
30
+
31
+ class MultiAgentSystem:
32
+ """Coordinates specialized agents and their underlying models.
33
+
34
+ The system instantiates a ``web_agent`` for browsing and data collection,
35
+ an ``info_agent`` for computation and image reasoning, and a
36
+ ``manager_agent`` that plans tasks and verifies answers using several
37
+ OpenRouter models.
38
+ """
39
+ def __init__(self):
40
+ self.deepseek_model = OpenAIServerModel(
41
+ model_id="deepseek/deepseek-r1-0528:free",
42
+ max_tokens=8096,
43
+ **common,
44
+ )
45
+ self.qwen_model = OpenAIServerModel(
46
+ model_id="qwen/qwen-2.5-coder-32b-instruct:free",
47
+ max_tokens=8096,
48
+ **common,
49
+ )
50
+ self.gemini_model = OpenAIServerModel(
51
+ model_id="google/gemini-2.0-flash-exp:free",
52
+ max_tokens=8096,
53
+ **common,
54
+ )
55
+
56
+ self.web_agent = CodeAgent(
57
+ model_id=self.qwen_model,
58
+ tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
59
+ name="web_agent",
60
+ description=(
61
+ "You are a web browsing agent. Whenever the given {task} involves browsing "
62
+ "the web or a specific website such as Wikipedia or YouTube, you will use "
63
+ "the provided tools. For web-based factual and retrieval tasks, be as precise and source-reliable as possible."
64
+ ),
65
+ additional_authorized_imports=[
66
+ "markdownify",
67
+ "json",
68
+ "requests",
69
+ "urllib.request",
70
+ "urllib.parse",
71
+ "wikipedia-api",
72
+ ],
73
+ verbosity_level=0,
74
+ max_steps=10,
75
+ )
76
+
77
+ self.info_agent = CodeAgent(
78
+ model_id=self.qwen_model,
79
+ tools=[PythonInterpreterTool(), image_reasoning_tool],
80
+ name="info_agent",
81
+ description=(
82
+ "You are an agent tasked with cleaning, parsing, calculating information, and performing OCR if images are provided in the {task}. "
83
+ "You can also analyze images using a vision model. You handle all math, code, and data manipulation. Use numpy, math, and available libraries. "
84
+ "For image or chess tasks, use pytesseract, PIL, chess, or the image_reasoning_tool as required."
85
+ ),
86
+ additional_authorized_imports=[
87
+ "numpy",
88
+ "math",
89
+ "pytesseract",
90
+ "PIL",
91
+ "chess",
92
+ ],
93
+ max_tokens=8096,
94
+ )
95
+
96
+ self.manager_agent = CodeAgent(
97
+ model_id=self.deepseek_model,
98
+ tools=[FinalAnswerTool()],
99
+ managed_agents=[self.web_agent, self.info_agent],
100
+ name="manager_agent",
101
+ description=(
102
+ "You are the manager. Given a {task}, plan which agent to use: "
103
+ "If web data is needed, delegate to web_agent. If math, parsing, image reasoning, or code is needed, use info_agent. "
104
+ "After collecting outputs, optionally cross-validate and check correctness, then finalize and submit the best answer using FinalAnswerTool. "
105
+ "For each task, explicitly explain your planning steps and reasons for choosing which agent, and always prefer the most accurate and complete answer possible."
106
+ ),
107
+ additional_authorized_imports=[
108
+ "json",
109
+ "pandas",
110
+ "numpy",
111
+ ],
112
+ planning_interval=3,
113
+ verbosity_level=2,
114
+ max_tokens=8096,
115
+ final_answer_check=[self.check_reasoning],
116
+ max_steps=8,
117
+ )
118
+
119
+ def check_reasoning(self, final_answer, agent_memory):
120
+ model_id = self.gemini_model
121
+ verification_prompt = (
122
+ f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}. "
123
+ f"The proposed final answer is: {final_answer}. "
124
+ "Please check that the reasoning process is correct: do they correctly answer the given task? "
125
+ "First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
126
+ )
127
+ output = model(verification_prompt)
128
+ print("Feedback: ", output)
129
+ if "FAIL" in output:
130
+ raise Exception(output)
131
+ return True
132
+
133
+