ABVM commited on
Commit
7f5a92e
·
verified ·
1 Parent(s): aad1e32

Upload multi_agent.py

Browse files
Files changed (1) hide show
  1. multi_agent.py +137 -0
multi_agent.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from smolagents import (
3
+ CodeAgent,
4
+ InferenceClientModel,
5
+ VisitWebpageTool,
6
+ WebSearchTool,
7
+ WikipediaSearchTool,
8
+ PythonInterpreterTool,
9
+ FinalAnswerTool,
10
+ tool
11
+ )
12
+ from groq import Groq
13
+ from typing import Dict, Any
14
+ import os
15
+
16
+ # ---- TOOLS ----
17
+ @tool
18
+ def image_process(image_file: str) -> Dict[str, str]:
19
+ """
20
+ Extract text from an image file using OCR and return OCR text and base64 encoding.
21
+
22
+ Args:
23
+ image_file: Path to the image file
24
+
25
+ Returns:
26
+ Dict with keys 'ocr_text' and 'base64_image'
27
+ """
28
+ try:
29
+ import pytesseract
30
+ from PIL import Image
31
+ from smolagents.utils import encode_image_base64
32
+ image = Image.open(image_file)
33
+ base64_img = encode_image_base64(image) # <<<< CORRECT VARIABLE
34
+ text = pytesseract.image_to_string(image)
35
+ return {
36
+ "ocr_text": text,
37
+ "base64_image": base64_img
38
+ }
39
+ except Exception as e:
40
+ return {
41
+ "ocr_text": "",
42
+ "base64_image": "",
43
+ "error": str(e)
44
+ }
45
+
46
+ # ---- GROQ MODEL WRAPPER ----
47
+ class GroqModel:
48
+ def __init__(self, model_name=""):
49
+ self.model_name = model_name
50
+ self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
51
+
52
+ def __call__(self, prompt, max_tokens=8096):
53
+ if isinstance(prompt, str):
54
+ messages = [{"role": "user", "content": prompt}]
55
+ else:
56
+ messages = prompt
57
+ response = self.client.chat.completions.create(
58
+ messages=messages,
59
+ model=self.model_name,
60
+ stream=False,
61
+ max_tokens=max_tokens,
62
+ )
63
+ return response.choices[0].message.content
64
+
65
+ # ---- MULTI-AGENT SYSTEM ----
66
+ class MultyAgentSystem:
67
+ def __init__(self):
68
+ deepseek_model = GroqModel("deepseek-r1-distill-llama-70b")
69
+ qwen_model = GroqModel("qwen-qwq-32b")
70
+
71
+ # --- Web agent definition ---
72
+ self.web_agent = CodeAgent(
73
+ model=qwen_model,
74
+ tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
75
+ name="web_agent",
76
+ description=(
77
+ "You are a web browsing agent. Whenever the given {task} involves browsing "
78
+ "the web or a specific website such as Wikipedia or YouTube, you will use "
79
+ "the provided tools. For web-based factual and retrieval tasks, be as precise and source-reliable as possible."
80
+ ),
81
+ additional_authorized_imports=[
82
+ "markdownify",
83
+ "json",
84
+ "requests",
85
+ "urllib.request",
86
+ "urllib.parse",
87
+ "wikipedia-api",
88
+ ],
89
+ verbosity_level=0,
90
+ max_steps=10,
91
+ )
92
+
93
+ # --- Info agent definition ---
94
+ self.info_agent = CodeAgent(
95
+ model=qwen_model,
96
+ tools=[PythonInterpreterTool(), image_process],
97
+ name="info_agent",
98
+ description=(
99
+ "You are an agent tasked with cleaning, parsing, calculating information, and performing OCR if images are provided in the {task}. "
100
+ "You handle all math, code, and data manipulation. Use numpy, math, and available libraries. For image or chess tasks, use pytesseract, PIL, or chess as required."
101
+ ),
102
+ additional_authorized_imports=[
103
+ "numpy",
104
+ "math",
105
+ "pytesseract",
106
+ "PIL",
107
+ "chess",
108
+ ],
109
+ )
110
+
111
+ # --- Manager agent definition ---
112
+ self.manager_agent = CodeAgent(
113
+ model=deepseek_model,
114
+ tools=[FinalAnswerTool()],
115
+ managed_agents=[self.web_agent, self.info_agent],
116
+ name="manager_agent",
117
+ description=(
118
+ "You are the manager. Given a {task}, plan which agent to use: "
119
+ "If web data is needed, delegate to web_agent. If math, parsing, or code is needed, use info_agent. "
120
+ "After collecting outputs, optionally cross-validate and check correctness, then finalize and submit the best answer using FinalAnswerTool. "
121
+ "For each task, explicitly explain your planning steps and reasons for choosing which agent, and always prefer the most accurate and complete answer possible."
122
+ ),
123
+ additional_authorized_imports=[
124
+ "json",
125
+ "pandas",
126
+ "numpy",
127
+ ],
128
+ planning_interval=5,
129
+ verbosity_level=2,
130
+ max_steps=20,
131
+ )
132
+
133
+ def __call__(self, question, **kwargs):
134
+ # You could add logic here to pass images or files when needed
135
+ return self.manager_agent(question, **kwargs)
136
+
137
+