- CustomAgent.py +23 -23
- app.py +1 -1
CustomAgent.py
CHANGED
|
@@ -6,27 +6,6 @@ from BasicAgent import BasicAgent
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
|
| 9 |
-
def websearch(query: str) -> str:
|
| 10 |
-
"""
|
| 11 |
-
Performs a web search using an external web search API. Note that better results may be obtained from varying the wording of the query.
|
| 12 |
-
Args:
|
| 13 |
-
query (str): The search query string.
|
| 14 |
-
Returns:
|
| 15 |
-
str: The response from the web search API as a string.
|
| 16 |
-
"""
|
| 17 |
-
try:
|
| 18 |
-
r = requests.post(
|
| 19 |
-
f"{os.environ['WEBSEARCH_API_URL']}/tools/websearch",
|
| 20 |
-
json={"query": query}
|
| 21 |
-
)
|
| 22 |
-
r.raise_for_status()
|
| 23 |
-
response = r.text
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print("Error:", e)
|
| 26 |
-
response = "An error has occurred while using websearch, please inform the user of this"
|
| 27 |
-
print(f"Agent searched for {query}. Response: {response}")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
class CustomAgent(BasicAgent):
|
| 31 |
def __init__(self):
|
| 32 |
super().__init__()
|
|
@@ -40,7 +19,7 @@ If you are asked for a comma separated list, apply the above rules depending of
|
|
| 40 |
Question: {question}
|
| 41 |
""" # System prompt, adapted from the GAIA team's example at https://huggingface.co/spaces/gaia-benchmark/leaderboard
|
| 42 |
self._model = OpenaiChatModel(
|
| 43 |
-
model="llama-3.
|
| 44 |
api_key=os.environ["GROQ_API_KEY"],
|
| 45 |
base_url="https://api.groq.com/openai/v1/",
|
| 46 |
temperature=0.7,
|
|
@@ -56,7 +35,7 @@ Question: {question}
|
|
| 56 |
@prompt_chain(
|
| 57 |
template=self._system_prompt,
|
| 58 |
model=self._model,
|
| 59 |
-
functions=[websearch],
|
| 60 |
)
|
| 61 |
def _magic(question: str) -> FunctionCall | str:
|
| 62 |
"""
|
|
@@ -67,3 +46,24 @@ Question: {question}
|
|
| 67 |
result = _magic(question)
|
| 68 |
print("Agent returning actual answer:", result)
|
| 69 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class CustomAgent(BasicAgent):
|
| 10 |
def __init__(self):
|
| 11 |
super().__init__()
|
|
|
|
| 19 |
Question: {question}
|
| 20 |
""" # System prompt, adapted from the GAIA team's example at https://huggingface.co/spaces/gaia-benchmark/leaderboard
|
| 21 |
self._model = OpenaiChatModel(
|
| 22 |
+
model="llama-3.1-8b-instant",
|
| 23 |
api_key=os.environ["GROQ_API_KEY"],
|
| 24 |
base_url="https://api.groq.com/openai/v1/",
|
| 25 |
temperature=0.7,
|
|
|
|
| 35 |
@prompt_chain(
|
| 36 |
template=self._system_prompt,
|
| 37 |
model=self._model,
|
| 38 |
+
functions=[self.websearch],
|
| 39 |
)
|
| 40 |
def _magic(question: str) -> FunctionCall | str:
|
| 41 |
"""
|
|
|
|
| 46 |
result = _magic(question)
|
| 47 |
print("Agent returning actual answer:", result)
|
| 48 |
return result
|
| 49 |
+
|
| 50 |
+
@staticmethod
|
| 51 |
+
def websearch(query: str) -> str:
|
| 52 |
+
"""
|
| 53 |
+
Performs a web search using an external web search API. Note that better results may be obtained from varying the wording of the query.
|
| 54 |
+
Args:
|
| 55 |
+
query (str): The search query string.
|
| 56 |
+
Returns:
|
| 57 |
+
str: The response from the web search API as a string.
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
r = requests.post(
|
| 61 |
+
f"{os.environ['WEBSEARCH_API_URL']}/tools/websearch",
|
| 62 |
+
json={"query": query}
|
| 63 |
+
)
|
| 64 |
+
r.raise_for_status()
|
| 65 |
+
response = r.text
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print("Error:", e)
|
| 68 |
+
response = "An error has occurred while using websearch, please inform the user of this"
|
| 69 |
+
print(f"Agent searched for {query}. Response: {response}")
|
app.py
CHANGED
|
@@ -21,7 +21,7 @@ Note: Once clicking on the "submit button, it can take quite some time (this is
|
|
| 21 |
---
|
| 22 |
**Implementation:**
|
| 23 |
|
| 24 |
-
CustomAgent is a subclass of BasicAgent that uses a custom system prompt and integrates with an OpenAI-compatible model (llama-3.
|
| 25 |
"""
|
| 26 |
)
|
| 27 |
gr.LoginButton()
|
|
|
|
| 21 |
---
|
| 22 |
**Implementation:**
|
| 23 |
|
| 24 |
+
CustomAgent is a subclass of BasicAgent that uses a custom system prompt and integrates with an OpenAI-compatible model (llama-3.1-8b-instant via Groq API). It leverages the magentic library for prompt chaining and function calling, allowing the agent to use external tools such as web search.
|
| 25 |
"""
|
| 26 |
)
|
| 27 |
gr.LoginButton()
|