Spaces:
Configuration error
Configuration error
File size: 2,451 Bytes
6b5a8ab 1769d8d 6b5a8ab 1769d8d 6b5a8ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import os
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.prebuilt import create_react_agent
from custom_tools import custom_tools
class ReActAgent:
def __init__(self):
load_dotenv()
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE")
# Initialize your LLM
llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash",
temperature=0,
max_retries=5
)
sys_prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, DON'T use comma to write your number NEITHER use units such as $ or percent sign unless specified otherwise. If you are asked for a string, DON'T use articles, NEITHER abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.\n\n\n\
You will be provided with tools to help you answer questions. If you are asked to look for an information or make a calculation, absolutely use the tools provided to you. You should AVOID calculating by yourself and ABSOLUTELY use appropriate tools. If needed, use one tool first, then use the output of that tool as an input to another thinking then to the use of another tool."
# Build the ReAct agent
self.agent = create_react_agent(
model=llm,
tools=custom_tools,
prompt=sys_prompt
)
print("ReActAgent initialized.")
def __call__(self, question: str) -> str:
# Wrap question in HumanMessage to match React expectations
input_msg = HumanMessage(content=question)
# Invoke the agent; returns a stream or single response
out = self.agent.invoke({"messages": [input_msg]})
# The last message contains the agent's reply
reply = out["messages"][-1].content
# Optionally, strip out “Final Answer:” headers
if "Final Answer:" in reply:
reply = reply.split("Final Answer:")[-1].strip()
return reply
|