Blifai/blif-multi-agent
0
1from langchain import hub2from langchain.agents import AgentExecutor, create_react_agent3from langchain_openai import OpenAI4from langchain_community.tools import DuckDuckGoSearchResults5from langchain_community.tools.tavily_search import TavilySearchResults6from langchain.tools import tool7 8@tool9def search(query: str) -> str:10 """Search things online"""11 retriever = DuckDuckGoSearchResults()12 return retriever.run(query)13 14class ReActAgent:15 """16 A LangChain agent class with conversation history for contextual processing.17 """18 19 def __init__(self):20 """21 Initializes the agent with default tools, OpenAI LLM, and an empty history.22 """23 self.tools = [TavilySearchResults(max_results=15)]24 # self.tools = [DuckDuckGoSearchResults()]25 self.prompt = hub.pull("hwchase17/react-chat")26 self.llm = OpenAI()27 agent = self.create_agent()28 self.agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True)29 30 def create_agent(self):31 """32 Creates a ReAct agent based on the defined prompt, LLM, and history.33 """34 agent = create_react_agent(self.llm, self.tools, self.prompt)35 return agent36 37 def run(self, question,history=""):38 """39 Executes the agent with the provided question, verbosity option, and updates history.40 """41 answer = self.agent_executor.invoke({"input": question, "chat_history": history})42 return answer43 44 