Spaces:
Sleeping
Sleeping
not working, but hey, keep it safe.
Browse files- basic_agent.py +58 -5
- basic_agent_test.py +13 -0
- requirements.txt +6 -1
basic_agent.py
CHANGED
|
@@ -2,17 +2,70 @@ import os
|
|
| 2 |
import logging
|
| 3 |
import sys
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
LOG = logging.getLogger(__name__)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# --- Basic Agent Definition ---
|
| 8 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 9 |
class BasicAgent:
|
| 10 |
-
def __init__(self):
|
| 11 |
print("BasicAgent initialized.")
|
| 12 |
-
LOG.warning("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def __call__(self, question: str) -> str:
|
| 15 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
| 2 |
import logging
|
| 3 |
import sys
|
| 4 |
|
| 5 |
+
import smolagents
|
| 6 |
+
|
| 7 |
+
|
| 8 |
LOG = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
+
SYSTEM_PROMPT = """
|
| 11 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. 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. 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. 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.
|
| 12 |
+
|
| 13 |
+
Take the time to plan the steps to reach the solution. Show the steps and then execute the steps.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
class BasicAgent:
|
| 19 |
+
def __init__(self, model_id=None):
|
| 20 |
print("BasicAgent initialized.")
|
| 21 |
+
LOG.warning("logging BasicAgent initialized.")
|
| 22 |
+
|
| 23 |
+
if model_id:
|
| 24 |
+
self.model_id = model_id
|
| 25 |
+
else:
|
| 26 |
+
self.model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 27 |
+
|
| 28 |
+
# Run locally.
|
| 29 |
+
self.model = smolagents.TransformersModel(
|
| 30 |
+
model_id=self.model_id,
|
| 31 |
+
max_new_tokens=8000,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
#self.model = smolagents.HfApiModel(
|
| 35 |
+
# max_tokens=8000,
|
| 36 |
+
# temperature=0.1,
|
| 37 |
+
# model_id=self.model_id,
|
| 38 |
+
# custom_role_conversions=None,
|
| 39 |
+
# )
|
| 40 |
+
ddg = smolagents.DuckDuckGoSearchTool()
|
| 41 |
+
self.tools = [smolagents.DuckDuckGoSearchTool(), smolagents.VisitWebpageTool(), smolagents.FinalAnswerTool()]
|
| 42 |
+
|
| 43 |
+
self.search_agent = smolagents.CodeAgent(
|
| 44 |
+
name="search_agent",
|
| 45 |
+
description="Search the web",
|
| 46 |
+
model=self.model,
|
| 47 |
+
tools=self.tools,
|
| 48 |
+
max_steps=6,
|
| 49 |
+
verbosity_level=2,
|
| 50 |
+
planning_interval=None,
|
| 51 |
+
additional_authorized_imports=["duckduckgo_search"],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
self.manager_agent = smolagents.CodeAgent(
|
| 55 |
+
name="manager_agent",
|
| 56 |
+
description="Manger of other agents",
|
| 57 |
+
tools=[smolagents.FinalAnswerTool()],
|
| 58 |
+
model=self.model,
|
| 59 |
+
max_steps=6,
|
| 60 |
+
verbosity_level=2,
|
| 61 |
+
planning_interval=None,
|
| 62 |
+
additional_authorized_imports=["duckduckgo_search"],
|
| 63 |
+
managed_agents=[self.search_agent]
|
| 64 |
+
)
|
| 65 |
|
| 66 |
def __call__(self, question: str) -> str:
|
| 67 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 68 |
+
prompt = f"{SYSTEM_PROMPT}\n\n{question}"
|
| 69 |
+
answer = self.manager_agent.run(prompt)
|
| 70 |
+
LOG.warning(f"{answer=}")
|
| 71 |
+
return answer
|
basic_agent_test.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
import basic_agent
|
| 6 |
+
|
| 7 |
+
LOG = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
ba = basic_agent.BasicAgent()
|
| 10 |
+
|
| 11 |
+
answer = ba("What color is the sky?")
|
| 12 |
+
|
| 13 |
+
LOG.warning(f"{answer=}")
|
requirements.txt
CHANGED
|
@@ -1,2 +1,7 @@
|
|
| 1 |
gradio
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
huggingface_hub[hf_xet]
|
| 3 |
+
langchain-core
|
| 4 |
+
langcahin-ollama
|
| 5 |
+
requests
|
| 6 |
+
smolagents
|
| 7 |
+
smolagents[transformers]
|