joronoso commited on
Commit
1cc8ff6
·
verified ·
1 Parent(s): 81917a3

Create agent.py

Browse files
Files changed (1) hide show
  1. agent.py +91 -0
agent.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_index.llms.groq import Groq
2
+ from llama_index.tools.wikipedia.base import WikipediaToolSpec
3
+ from llama_index.core.llms import ChatMessage
4
+ from llama_index.core.agent import ReActAgent
5
+ import logging
6
+ from llama_index.llms.deepinfra import DeepInfraLLM
7
+ import os
8
+ from llama_index.tools.tavily_research import TavilyToolSpec
9
+ from dotenv import load_dotenv
10
+ from llama_index.core.prompts import PromptTemplate
11
+ import requests
12
+ import json
13
+
14
+ logging.basicConfig(level=logging.INFO)
15
+ load_dotenv()
16
+
17
+ class BasicAgent:
18
+ """
19
+ Agent using LlamaIndex to fetch data from the web and answer GAIA benchmark questions.
20
+ """
21
+ def __init__(self):
22
+ system_prompt = """
23
+ Value: You are designed to help with a variety of tasks, from answering questions to providing summaries to other types of analyses.
24
+
25
+ ## Tools
26
+
27
+ You have access to a wide variety of tools. You are responsible for using the tools in any sequence you deem appropriate to complete the task at hand.
28
+ This may require breaking the task into subtasks and using different tools to complete each subtask.
29
+
30
+ You have access to the following tools:
31
+ {tool_desc}
32
+
33
+
34
+ ## Output Format
35
+
36
+ Please answer in the same language as the question and use the following format:
37
+
38
+ ```
39
+ Thought: The current language of the user is: (user's language). I need to use a tool to help me answer the question.
40
+ Action: tool name (one of {tool_names}) if using a tool.
41
+ Action Input: the input to the tool, in a JSON format representing the kwargs (e.g. {{"input": "hello world", "num_beams": 5}})
42
+ ```
43
+
44
+ Please ALWAYS start with a Thought.
45
+
46
+ NEVER surround your response with markdown code markers. You may use code markers within your response if you need to.
47
+
48
+ Please use a valid JSON format for the Action Input. Do NOT do this {{'input': 'hello world', 'num_beams': 5}}.
49
+
50
+ If this format is used, the tool will respond in the following format:
51
+
52
+ ```
53
+ Observation: tool response
54
+ ```
55
+
56
+ You should keep repeating the above format till you have enough information to answer the question without using any more tools. At that point, you MUST respond in one of the following two formats:
57
+
58
+ ```
59
+ Thought: I can answer without using any more tools. I'll use the user's language to answer
60
+ Answer: [your answer here (In the same language as the user's question)]
61
+ ```
62
+
63
+ ```
64
+ Thought: I cannot answer the question with the provided tools.
65
+ Answer: [your answer here (In the same language as the user's question)]
66
+ ```
67
+
68
+ The answer should be concise and to the point. For example, if the answer is a number, just return the number without any additional text. If the question is "What is the capital of France?", the answer should be "Paris".
69
+
70
+ If the question includes guidelines regarding the format of the answer, please follow those guidelines faithfully
71
+ ## Current Conversation
72
+
73
+ Below is the current conversation consisting of interleaving human and assistant messages.
74
+ """
75
+ react_system_prompt = PromptTemplate(system_prompt)
76
+ #llm = Groq(model="qwen-qwq-32b")
77
+ llm = DeepInfraLLM(
78
+ model="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",api_key=os.getenv("DEEPINFRA_API_KEY"))
79
+
80
+ agent = ReActAgent.from_tools(
81
+ llm=llm,
82
+ tools=WikipediaToolSpec().to_tool_list() + TavilyToolSpec(api_key=os.getenv('TAVILY_API_KEY')).to_tool_list(),
83
+ verbose=True,
84
+ )
85
+
86
+ agent.update_prompts({"agent_worker:system_prompt": react_system_prompt})
87
+ self.agent = agent
88
+
89
+ def __call__(self, question: str) -> str:
90
+ answer = self.agent.query(question)
91
+ return answer