Spaces:
Runtime error
Runtime error
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain import hub
|
| 2 |
+
from langchain.agents import AgentExecutor, create_react_agent
|
| 3 |
+
from langchain_openai import OpenAI
|
| 4 |
+
from langchain_community.tools import DuckDuckGoSearchResults
|
| 5 |
+
|
| 6 |
+
class ReActAgent:
|
| 7 |
+
"""
|
| 8 |
+
A LangChain agent class with conversation history for contextual processing.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def __init__(self):
|
| 12 |
+
"""
|
| 13 |
+
Initializes the agent with default tools, OpenAI LLM, and an empty history.
|
| 14 |
+
"""
|
| 15 |
+
self.tools = [DuckDuckGoSearchResults()]
|
| 16 |
+
self.prompt_template = '''
|
| 17 |
+
Answer the following questions as best you can, considering the conversation history:
|
| 18 |
+
|
| 19 |
+
{history}
|
| 20 |
+
|
| 21 |
+
You have access to the following tools:
|
| 22 |
+
|
| 23 |
+
{tools}
|
| 24 |
+
|
| 25 |
+
Use the following format:
|
| 26 |
+
|
| 27 |
+
Question: the input question you must answer
|
| 28 |
+
Thought: you should always think about what to do
|
| 29 |
+
Action: the action to take (use history first, then tools if needed)
|
| 30 |
+
Action Input: the input to the action
|
| 31 |
+
Observation: the result of the action
|
| 32 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 33 |
+
Thought: I now know the final answer
|
| 34 |
+
Final Answer: the final answer to the original input question
|
| 35 |
+
|
| 36 |
+
Begin!
|
| 37 |
+
|
| 38 |
+
Question: {input}
|
| 39 |
+
Thought:{agent_scratchpad}
|
| 40 |
+
|
| 41 |
+
'''
|
| 42 |
+
self.llm = OpenAI()
|
| 43 |
+
|
| 44 |
+
def update_history(self, question, answer):
|
| 45 |
+
"""
|
| 46 |
+
Updates the conversation history with the latest question and answer.
|
| 47 |
+
"""
|
| 48 |
+
self.history.append({"question": question, "answer": answer})
|
| 49 |
+
|
| 50 |
+
def create_agent(self, history):
|
| 51 |
+
"""
|
| 52 |
+
Creates a ReAct agent based on the defined prompt, LLM, and history.
|
| 53 |
+
"""
|
| 54 |
+
history_str = "\n".join([f"{i['question']}: {i['answer']}" for i in history])
|
| 55 |
+
prompt = self.prompt_template.format(history=history_str, tools=",".join(t.__name__ for t in self.tools))
|
| 56 |
+
agent = create_react_agent(self.llm, self.tools, prompt)
|
| 57 |
+
return agent
|
| 58 |
+
|
| 59 |
+
def run(self, question,history=[], verbose=True):
|
| 60 |
+
"""
|
| 61 |
+
Executes the agent with the provided question, verbosity option, and updates history.
|
| 62 |
+
"""
|
| 63 |
+
agent = self.create_agent(history)
|
| 64 |
+
agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=verbose)
|
| 65 |
+
answer = agent_executor.invoke({"input": question, "history": history})
|
| 66 |
+
return answer
|
| 67 |
+
|