Spaces:
Sleeping
Sleeping
File size: 3,316 Bytes
d3b49b4 24639c3 d3b49b4 24639c3 e5782f0 d3b49b4 24639c3 8ae792e 6c9ca3f aec3e3b 919fd15 8ae792e d3b49b4 6bdf8ce 8ae792e 7561460 8ae792e 6bdf8ce 760f1a8 9ea2377 8b26a35 919fd15 24639c3 d3b49b4 24639c3 e5782f0 24639c3 d3b49b4 24639c3 226791d 24639c3 8ae792e 24639c3 d3b49b4 24639c3 9ea2377 d3b49b4 24639c3 9ea2377 24639c3 14fa0cc 24639c3 |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
from langchain_openai import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
from langgraph.prebuilt import create_react_agent
from tools import (
wikipedia_search_tool, arxiv_search_tool,
audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool,
add_tool, subtract_tool, multiply_tool, divide_tool, web_search_tool
)
# ββββββββββββββββββββββββββββββββ Config ββββββββββββββββββββββββββββββββ
SYSTEM_PROMPT = """
You are a helpful AI assistant. You will answer questions by thinking step-by-step, taking actions using tools when necessary, and finishing with a final answer.
Action Input: [input]
When you receive an observation, continue reasoning. Write:
IMPORTANT:
- 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.
- If using tools that require a `task_id`, only use the value provided.
- Do not make up tool names.
- Do not loop unnecessarily.
- Provide FINAL ANSWER fast.
- Try to use the web search tool first before using wiki and arxiv tools.
- If wiki tool doesnt give you enough information, try again with a shorter query, or a more genral query. Dont use a query that is too specific.
- DOnt user wiki tool, web search tool and arxiv tool more than 3 times.
Return the final answer in the following format:
Thought: [final reasoning]
FINAL ANSWER: [your answer]
"""
TOOLS = [
wikipedia_search_tool, arxiv_search_tool,
audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool,
add_tool, subtract_tool, multiply_tool, divide_tool, web_search_tool
]
# βββββββββββββββββββββββββββββ Agent Wrapper βββββββββββββββββββββββββββββ
class SimpleLangGraphAgent:
def __init__(self, model_name="gpt-4.1-mini"):
self.agent = create_react_agent(
model=ChatOpenAI(model_name=model_name, temperature=0.3),
tools=TOOLS,
)
def run(self, question: str, task_id: str = None, max_steps: int = 10) -> str:
messages = [SystemMessage(content=SYSTEM_PROMPT)]
if task_id:
messages[0].content += f"\n\nYour task_id is: {task_id}. Use it only with the tools that require it."
messages.append(HumanMessage(content=question))
state = {"messages": messages}
final_state = self.agent.invoke(state)
for msg in final_state["messages"][::-1]:
if "FINAL ANSWER:" in msg.content.upper():
print(f"\n\n thoughts = {msg.content.split('Thought:')[-1].strip()} \n\n")
return msg.content.split("FINAL ANSWER:")[-1].strip()
return "No FINAL ANSWER found."
|