blif-multi-agent / agent.py
ageraustine's picture
Update agent.py
0fdfad8 verified
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
from langchain_openai import OpenAI
from langchain_community.tools import DuckDuckGoSearchResults
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.tools import tool
@tool
def search(query: str) -> str:
"""Search things online"""
retriever = DuckDuckGoSearchResults()
return retriever.run(query)
class ReActAgent:
"""
A LangChain agent class with conversation history for contextual processing.
"""
def __init__(self):
"""
Initializes the agent with default tools, OpenAI LLM, and an empty history.
"""
self.tools = [TavilySearchResults(max_results=15)]
# self.tools = [DuckDuckGoSearchResults()]
self.prompt = hub.pull("hwchase17/react-chat")
self.llm = OpenAI()
agent = self.create_agent()
self.agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True)
def create_agent(self):
"""
Creates a ReAct agent based on the defined prompt, LLM, and history.
"""
agent = create_react_agent(self.llm, self.tools, self.prompt)
return agent
def run(self, question,history=""):
"""
Executes the agent with the provided question, verbosity option, and updates history.
"""
answer = self.agent_executor.invoke({"input": question, "chat_history": history})
return answer