Implement CustomAgent, using magentic
Browse files- CustomAgent.py +53 -1
- requirements.txt +2 -1
CustomAgent.py
CHANGED
|
@@ -1,7 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from BasicAgent import BasicAgent
|
| 2 |
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class CustomAgent(BasicAgent):
|
| 5 |
def __init__(self):
|
| 6 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
print("CustomAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from magentic import FunctionCall, OpenaiChatModel, prompt_chain
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
from BasicAgent import BasicAgent
|
| 5 |
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
|
| 9 |
class CustomAgent(BasicAgent):
|
| 10 |
def __init__(self):
|
| 11 |
+
self._system_prompt = """\
|
| 12 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer like so: [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 |
+
""" # System prompt, adapted from the GAIA team's example at https://huggingface.co/spaces/gaia-benchmark/leaderboard
|
| 18 |
+
self._model = OpenaiChatModel(
|
| 19 |
+
model="llama-3.3-70b-versatile",
|
| 20 |
+
api_key=os.environ["GROQ_API_KEY"],
|
| 21 |
+
base_url="https://api.groq.com/openai/v1/",
|
| 22 |
+
temperature=0.2,
|
| 23 |
+
max_tokens=512
|
| 24 |
+
)
|
| 25 |
print("CustomAgent initialized.")
|
| 26 |
+
|
| 27 |
+
def __call__(self, question: str) -> str:
|
| 28 |
+
"""
|
| 29 |
+
this is where the magic happens
|
| 30 |
+
"""
|
| 31 |
+
super().__call__(question)
|
| 32 |
+
@prompt_chain(
|
| 33 |
+
template=self._system_prompt,
|
| 34 |
+
model=self._model,
|
| 35 |
+
functions=[self._banana],
|
| 36 |
+
)
|
| 37 |
+
def _magic(question: str) -> FunctionCall | str:
|
| 38 |
+
"""
|
| 39 |
+
An agent, using websearch to gather information and
|
| 40 |
+
answer a user's query.
|
| 41 |
+
"""
|
| 42 |
+
...
|
| 43 |
+
return _magic(question)
|
| 44 |
+
|
| 45 |
+
def _banana(query: str) -> str:
|
| 46 |
+
"""
|
| 47 |
+
✨ banana ✨
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
r = requests.post(
|
| 51 |
+
f"{os.environ["WEBSEARCH_API_URL"]}/tools/websearch",
|
| 52 |
+
json={"query": query}
|
| 53 |
+
)
|
| 54 |
+
r.raise_for_status()
|
| 55 |
+
response = r.json()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(e)
|
| 58 |
+
response = f'{{"error": "An error has occurred while using websearch, please inform the user of this"}}'
|
| 59 |
+
print(f"Agent searched for {query}. Response: {response}")
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
gradio
|
| 2 |
-
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
magentic
|
| 3 |
+
requests
|