Romain Fayoux
commited on
Commit
·
7658a4e
1
Parent(s):
3ce511d
Added multi-agent and increased tokens
Browse files- app.py +2 -3
- llm_only_agent.py +3 -4
- multi_agent.py +57 -0
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from phoenix.otel import register
|
| 7 |
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
| 8 |
from llm_only_agent import LLMOnlyAgent
|
| 9 |
-
import
|
| 10 |
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
|
@@ -45,7 +44,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, limit: int | None):
|
|
| 45 |
|
| 46 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 47 |
try:
|
| 48 |
-
agent =
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Error instantiating agent: {e}")
|
| 51 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from phoenix.otel import register
|
| 6 |
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
| 7 |
from llm_only_agent import LLMOnlyAgent
|
| 8 |
+
from multi_agent import MultiAgent
|
| 9 |
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
|
|
|
| 44 |
|
| 45 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 46 |
try:
|
| 47 |
+
agent = MultiAgent()
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Error instantiating agent: {e}")
|
| 50 |
return f"Error initializing agent: {e}", None
|
llm_only_agent.py
CHANGED
|
@@ -17,8 +17,7 @@ class LLMOnlyAgent:
|
|
| 17 |
|
| 18 |
# Basic inference model
|
| 19 |
model = InferenceClientModel(
|
| 20 |
-
max_tokens=
|
| 21 |
-
temperature=0.5,
|
| 22 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 23 |
custom_role_conversions=None,
|
| 24 |
)
|
|
@@ -30,8 +29,8 @@ class LLMOnlyAgent:
|
|
| 30 |
tools=[FinalAnswerTool(), WikipediaSearchTool(), WebSearchTool(), VisitWebpageTool()],
|
| 31 |
additional_authorized_imports=[ "markdownify" , "requests" ],
|
| 32 |
max_steps=5,
|
| 33 |
-
planning_interval=3
|
| 34 |
-
final_answer_checks=self.final_answer_checks()
|
| 35 |
)
|
| 36 |
|
| 37 |
print("LLM-only Agent initialized.")
|
|
|
|
| 17 |
|
| 18 |
# Basic inference model
|
| 19 |
model = InferenceClientModel(
|
| 20 |
+
max_tokens=8096,
|
|
|
|
| 21 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 22 |
custom_role_conversions=None,
|
| 23 |
)
|
|
|
|
| 29 |
tools=[FinalAnswerTool(), WikipediaSearchTool(), WebSearchTool(), VisitWebpageTool()],
|
| 30 |
additional_authorized_imports=[ "markdownify" , "requests" ],
|
| 31 |
max_steps=5,
|
| 32 |
+
planning_interval=3
|
| 33 |
+
#final_answer_checks=self.final_answer_checks()
|
| 34 |
)
|
| 35 |
|
| 36 |
print("LLM-only Agent initialized.")
|
multi_agent.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydoc import describe
|
| 2 |
+
import re
|
| 3 |
+
from smolagents import AgentMemory, CodeAgent, InferenceClientModel, FinalAnswerTool, WebSearchTool
|
| 4 |
+
from collections.abc import Callable
|
| 5 |
+
|
| 6 |
+
from smolagents.default_tools import VisitWebpageTool, WikipediaSearchTool
|
| 7 |
+
|
| 8 |
+
class MultiAgent:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
|
| 11 |
+
# Instructions prompt
|
| 12 |
+
self.instructions = """finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 13 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 14 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 15 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 16 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
| 17 |
+
|
| 18 |
+
# WEB AGENT
|
| 19 |
+
self.web_agent = CodeAgent(
|
| 20 |
+
model=InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct", provider="together", max_tokens=8096),
|
| 21 |
+
tools=[WikipediaSearchTool(), WebSearchTool(), VisitWebpageTool()],
|
| 22 |
+
name="web_agent",
|
| 23 |
+
description="A code agent that can search the web and visit webpages",
|
| 24 |
+
additional_authorized_imports=[ "markdownify" , "requests" ],
|
| 25 |
+
max_steps=5
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# MANAGER AGENT
|
| 29 |
+
self.manager_agent = CodeAgent(
|
| 30 |
+
model=InferenceClientModel("deepseek-ai/DeepSeek-R1", provider="together", max_tokens=8096),
|
| 31 |
+
tools=[FinalAnswerTool()],
|
| 32 |
+
managed_agents=[self.web_agent],
|
| 33 |
+
instructions=self.instructions,
|
| 34 |
+
additional_authorized_imports=[
|
| 35 |
+
"pandas",
|
| 36 |
+
"numpy",
|
| 37 |
+
],
|
| 38 |
+
planning_interval=5,
|
| 39 |
+
verbosity_level=2,
|
| 40 |
+
max_steps=5,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
print("Multi Agent initialized.")
|
| 44 |
+
|
| 45 |
+
def __call__(self, question: str) -> str:
|
| 46 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 47 |
+
answer = self.manager_agent.run(question)
|
| 48 |
+
print(f"Agent returning answer: {answer}")
|
| 49 |
+
return answer
|
| 50 |
+
|
| 51 |
+
def final_answer_checks(self) -> list[Callable] :
|
| 52 |
+
return [ self.check_func ]
|
| 53 |
+
|
| 54 |
+
def check_func(self, answer: str, memory: AgentMemory) -> bool:
|
| 55 |
+
check = bool(re.match(r'^(\d+(\.\d+)?|\w+(\s+\w+){0,4}|(\d+(\.\d+)?|"[^"]*"|\w+)(\s*,\s*(\d+(\.\d+)?|"[^"]*"|\w+))+)$', answer))
|
| 56 |
+
print(f"FINAL ANSWER CHECK is {check}")
|
| 57 |
+
return check
|