hughpearse/langchain-react-agent
0
1import os2import gradio as gr3import json4from gradio import ChatMessage5from huggingface_hub import login6from langchain.agents import AgentExecutor, create_react_agent7from langchain.tools import BaseTool, StructuredTool, tool8from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder9from langchain_core.prompts import PromptTemplate10from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint11 12login(os.environ['HUGGINGFACE_HUB_API_KEY'])13 14@tool15def add(a: float, b: float) -> float:16 """Add two numbers: a+b"""17 try:18 return a + b19 except ValueError:20 raise ValueError(f"Inputs must be floats, but got: a={a}, b={b}")21 22@tool23def subtract(a: float, b: float) -> float:24 """Subtract two numbers: a-b"""25 try:26 return a - b27 except ValueError:28 raise ValueError(f"Inputs must be floats, but got: a={a}, b={b}")29 30@tool31def multiply(a: float, b: float) -> float:32 """Multiply two numbers: a*b"""33 try:34 return a * b35 except ValueError:36 raise ValueError(f"Inputs must be floats, but got: a={a}, b={b}")37 38@tool39def divide(a: float, b: float) -> float:40 """Divide two numbers: a/b"""41 try:42 return a // b43 except ValueError:44 raise ValueError(f"Inputs must be floats, but got: a={a}, b={b}")45 46@tool47def square(a: float) -> float:48 """Square a number: a^a"""49 try:50 return a * a51 except ValueError:52 raise ValueError(f"Input must be a float, but got: a={a}")53 54def get_llm():55 endpoint = HuggingFaceEndpoint(56 repo_id="mistralai/Mistral-7B-Instruct-v0.3",57 huggingfacehub_api_token=os.environ['HUGGINGFACE_HUB_API_KEY'],58 temperature=0.0,59 task="text-generation",60 max_new_tokens=102461 )62 model = ChatHuggingFace(llm=endpoint)63 return model64 65template = '''Answer the following questions as best you can. You have access to the following tools:66{tools}67Use the following format:68Question: the input question you must answer69Thought: you should always think about what to do70Action: the action to take, should be one of [{tool_names}]71Action Input: the numeric input to the action (must be a number)72Observation: the result of the action73... (this Thought/Action/Action Input/Observation can repeat N times)74Thought: I now know the final answer75Final Answer: the final answer to the original input question76Begin!77Question: {input}78Thought:{agent_scratchpad}'''79tools = [add, subtract, multiply, divide, square]80prompt = PromptTemplate.from_template(template=template).partial(81 tools="\n".join([f"{t.name}: {t.description}" for t in tools]),82 tool_names=", ".join([t.name for t in tools])83)84llm = get_llm()85agent = create_react_agent(llm, tools, prompt)86agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)87 88def chat(message, history):89 response = agent_executor.invoke({"input": message})90 return ChatMessage(role="assistant", content=response['output'])91 92gr.ChatInterface(93 chat, 94 type="messages",95 title="Natural language calculator",96 description="Transform a prompt to a number.",97 examples=["what's five times 18 and add a million plus a billion?", "What is 13 multiplied 15 minus 3?"]98).launch()99 