Romain FAYOUX
commited on
Commit
·
24559eb
1
Parent(s):
4f492fe
added tools to the agent, and new requirements
Browse files- .gitignore +4 -0
- app.py +6 -4
- llm_only_agent.py +29 -14
- requirements.txt +6 -1
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
.venv
|
| 3 |
+
.ropeproject
|
| 4 |
+
__pycache__
|
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from llm_only_agent import LLMOnlyAgent
|
|
|
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
@@ -74,8 +75,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, limit: int | None):
|
|
| 74 |
results_log = []
|
| 75 |
answers_payload = []
|
| 76 |
# Limit for test purposes
|
|
|
|
| 77 |
if limit is not None:
|
| 78 |
-
|
| 79 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 80 |
for item in questions_data:
|
| 81 |
task_id = item.get("task_id")
|
|
@@ -95,7 +97,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, limit: int | None):
|
|
| 95 |
print("Agent did not produce any answers to submit.")
|
| 96 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 97 |
|
| 98 |
-
# 4. Prepare Submission
|
| 99 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 100 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 101 |
print(status_update)
|
|
@@ -171,7 +173,7 @@ with gr.Blocks() as demo:
|
|
| 171 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 172 |
|
| 173 |
run_button.click(
|
| 174 |
-
fn=run_and_submit_all
|
| 175 |
outputs=[status_output, results_table]
|
| 176 |
)
|
| 177 |
|
|
@@ -197,4 +199,4 @@ if __name__ == "__main__":
|
|
| 197 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 198 |
|
| 199 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 200 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from llm_only_agent import LLMOnlyAgent
|
| 7 |
+
import profile
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 75 |
results_log = []
|
| 76 |
answers_payload = []
|
| 77 |
# Limit for test purposes
|
| 78 |
+
limit = None
|
| 79 |
if limit is not None:
|
| 80 |
+
questions_data = questions_data[:limit]
|
| 81 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 82 |
for item in questions_data:
|
| 83 |
task_id = item.get("task_id")
|
|
|
|
| 97 |
print("Agent did not produce any answers to submit.")
|
| 98 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 99 |
|
| 100 |
+
# 4. Prepare Submission
|
| 101 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 102 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 103 |
print(status_update)
|
|
|
|
| 173 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 174 |
|
| 175 |
run_button.click(
|
| 176 |
+
fn=run_and_submit_all,
|
| 177 |
outputs=[status_output, results_table]
|
| 178 |
)
|
| 179 |
|
|
|
|
| 199 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 200 |
|
| 201 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 202 |
+
demo.launch(debug=True, share=False)
|
llm_only_agent.py
CHANGED
|
@@ -1,10 +1,22 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class LLMOnlyAgent:
|
| 4 |
def __init__(self):
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
max_tokens=2096,
|
| 9 |
temperature=0.5,
|
| 10 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
|
@@ -14,22 +26,25 @@ class LLMOnlyAgent:
|
|
| 14 |
# Code Agent
|
| 15 |
self.agent = CodeAgent(
|
| 16 |
model=model,
|
| 17 |
-
|
| 18 |
-
tools=[FinalAnswerTool()],
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
|
| 22 |
print("BasicAgent initialized.")
|
|
|
|
| 23 |
def __call__(self, question: str) -> str:
|
| 24 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 25 |
answer = self.agent.run(question)
|
| 26 |
print(f"Agent returning answer: {answer}")
|
| 27 |
return answer
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from smolagents import AgentMemory, CodeAgent, InferenceClientModel, FinalAnswerTool, WebSearchTool
|
| 3 |
+
from collections.abc import Callable
|
| 4 |
+
|
| 5 |
+
from smolagents.default_tools import VisitWebpageTool, WikipediaSearchTool
|
| 6 |
|
| 7 |
class LLMOnlyAgent:
|
| 8 |
def __init__(self):
|
| 9 |
|
| 10 |
+
# Instructions prompt
|
| 11 |
+
self.instructions = """finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 12 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 13 |
+
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.
|
| 14 |
+
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.
|
| 15 |
+
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."""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Basic inference model
|
| 19 |
+
model = InferenceClientModel(
|
| 20 |
max_tokens=2096,
|
| 21 |
temperature=0.5,
|
| 22 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
|
|
|
| 26 |
# Code Agent
|
| 27 |
self.agent = CodeAgent(
|
| 28 |
model=model,
|
| 29 |
+
instructions=self.instructions,
|
| 30 |
+
tools=[FinalAnswerTool(), WikipediaSearchTool(), WebSearchTool(), VisitWebpageTool()],
|
| 31 |
+
additional_authorized_imports=[ "markdownify" , "requests" ],
|
| 32 |
+
max_steps=10
|
| 33 |
+
# final_answer_checks=self.final_answer_checks()
|
| 34 |
)
|
| 35 |
+
|
| 36 |
print("BasicAgent initialized.")
|
| 37 |
+
|
| 38 |
def __call__(self, question: str) -> str:
|
| 39 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 40 |
answer = self.agent.run(question)
|
| 41 |
print(f"Agent returning answer: {answer}")
|
| 42 |
return answer
|
| 43 |
|
| 44 |
+
def final_answer_checks(self) -> list[Callable] :
|
| 45 |
+
return [ self.check_func ]
|
| 46 |
+
|
| 47 |
+
def check_func(self, answer: str, memory: AgentMemory) -> bool:
|
| 48 |
+
check = bool(re.match(r'^(\d+(\.\d+)?|\w+(\s+\w+){0,4}|(\d+(\.\d+)?|"[^"]*"|\w+)(\s*,\s*(\d+(\.\d+)?|"[^"]*"|\w+))+)$', answer))
|
| 49 |
+
print(f"FINAL ANSWER CHECK is {check}")
|
| 50 |
+
return check
|
requirements.txt
CHANGED
|
@@ -1,3 +1,8 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
-
smolagents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
+
smolagents
|
| 4 |
+
gradio[oauth]
|
| 5 |
+
huggingface_hub[cli]
|
| 6 |
+
wikipedia-api
|
| 7 |
+
markdownify
|
| 8 |
+
requests
|