Cmuroc27 commited on
Commit
dc7da23
Β·
1 Parent(s): 59a5f16

react agent in react agent

Browse files
Files changed (1) hide show
  1. agents.py +58 -0
agents.py CHANGED
@@ -3,9 +3,67 @@ from llama_index.core.agent.workflow import AgentWorkflow
3
  from llama_index.llms.openai import OpenAI
4
  from tools import (image_analyzer_tool, youtube_transcript_tool, calculator_tool, read_document_tool, search_tool)
5
  from llama_index.core.workflow import (Workflow, Context, step, Context, StartEvent, StopEvent, Event)
 
6
  from dotenv import load_dotenv
7
  load_dotenv()
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Events
10
  class RouteEvent(Event):
11
  question: str
 
3
  from llama_index.llms.openai import OpenAI
4
  from tools import (image_analyzer_tool, youtube_transcript_tool, calculator_tool, read_document_tool, search_tool)
5
  from llama_index.core.workflow import (Workflow, Context, step, Context, StartEvent, StopEvent, Event)
6
+ from llama_index.core.agent import ReActAgent
7
  from dotenv import load_dotenv
8
  load_dotenv()
9
 
10
+ # Alfred React Agent
11
+ class AlfredReactAgent(ReActAgent):
12
+ def __init__(self, llm:OpenAI):
13
+ tools = [image_analyzer_tool, youtube_transcript_tool, calculator_tool, read_document_tool, search_tool]
14
+ system_prompt = """You are a helpful assistant that answers questions concisely.
15
+
16
+ CRITICAL RULES:
17
+ 1. Answer with THE MINIMUM words possible
18
+ 2. For "how many" questions β†’ just the number
19
+ 3. For "what is the name" β†’ just the name
20
+ 4. For calculations β†’ just the result
21
+ 5. Use tools ONLY when absolutely necessary
22
+ 6. After using a tool ONCE, provide answer and STOP
23
+ 7. If you can't find answer after ONE tool use, say "Unable to find" and STOP
24
+
25
+ EXAMPLES:
26
+ Q: "Capital of Japan?"
27
+ A: "Tokyo"
28
+
29
+ Q: "20 * 20?"
30
+ A: "400"
31
+
32
+ Q: "Surname of X?"
33
+ A: "Murakami"
34
+
35
+ Q: "What video shows X?"
36
+ Use youtube_transcript β†’ "quote from video"
37
+
38
+ Q: "Total from file.xlsx?"
39
+ Use read_document β†’ "$1234.56"
40
+
41
+ BE EXTREMELY CONCISE. No explanations."""
42
+
43
+
44
+
45
+
46
+ self.agent = ReActAgent.from_tools(
47
+ tools,
48
+ llm = llm,
49
+ verbose = False,
50
+ max_iterations = 8,
51
+ system_prompt = system_prompt
52
+ )
53
+
54
+ async def run(self, question: str) -> str:
55
+ try:
56
+ response = await self.agent.achat(question)
57
+ return str(response)
58
+ except Exception as e:
59
+ return f"Error: {str(e)}"
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
  # Events
68
  class RouteEvent(Event):
69
  question: str