Create agent.py
#73
by
RamV12
- opened
agent.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# agent.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 5 |
+
from llama_index.core.agent.workflow import AgentWorkflow, ReActAgent
|
| 6 |
+
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
| 7 |
+
from llama_index.core.tools import FunctionTool
|
| 8 |
+
|
| 9 |
+
class BasicAgent:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
print("Initializing BasicAgent...")
|
| 12 |
+
|
| 13 |
+
# Define tools
|
| 14 |
+
def add(a: int, b: int) -> int:
|
| 15 |
+
return a + b
|
| 16 |
+
|
| 17 |
+
def multiply(a: int, b: int) -> int:
|
| 18 |
+
return a * b
|
| 19 |
+
|
| 20 |
+
add_tool = FunctionTool.from_defaults(fn=add)
|
| 21 |
+
multiply_tool = FunctionTool.from_defaults(fn=multiply)
|
| 22 |
+
|
| 23 |
+
# Load web search tool
|
| 24 |
+
tool_spec = DuckDuckGoSearchToolSpec()
|
| 25 |
+
search_tools = tool_spec.to_tool_list()
|
| 26 |
+
|
| 27 |
+
all_tools = [add_tool, multiply_tool] + search_tools
|
| 28 |
+
|
| 29 |
+
# Load LLM
|
| 30 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 31 |
+
self.llm = HuggingFaceInferenceAPI(
|
| 32 |
+
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 33 |
+
token=hf_token,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Create the agent
|
| 37 |
+
self.agent = ReActAgent(
|
| 38 |
+
name="universal_agent",
|
| 39 |
+
description="An assistant that can search the web and do math.",
|
| 40 |
+
system_prompt="You are a helpful assistant with access to tools.",
|
| 41 |
+
tools=all_tools,
|
| 42 |
+
llm=self.llm,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Setup workflow
|
| 46 |
+
self.workflow = AgentWorkflow(
|
| 47 |
+
agents=[self.agent],
|
| 48 |
+
root_agent="universal_agent",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
async def __call__(self, question: str) -> str:
|
| 52 |
+
print(f"[BasicAgent] Received question: {question}")
|
| 53 |
+
response = await self.workflow.run(user_msg=question)
|
| 54 |
+
return str(response)
|