Spaces:
Runtime error
Runtime error
File size: 2,024 Bytes
8609b68 |
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 66 67 68 69 |
from typing import List, TypedDict, Annotated
from langchain_openai import ChatOpenAI
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
from langgraph.graph.message import add_messages
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.tools import Calculator
from dotenv import load_dotenv
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
search_tool = DuckDuckGoSearchRun()
calculator = Calculator()
tools = [search_tool, calculator]
load_dotenv()
llm = ChatOpenAI("gpt-4o")
llm_with_tools = llm.bind_tools(tools)
def assistant(state: AgentState):
system_prompt = """
You are a well-educated research assistant with access to the web and a calculator.
Please answer the questions by outputting only the answer and nothing else.
"""
system_message = SystemMessage(content=system_prompt)
return {
"messages": [llm_with_tools.invoke([system_message] + state["messages"])],
}
class Agent:
"""
A research assistant capable of searching the web and basic arithmetics.
"""
def __init__(self):
"""
Initializes the agent.
"""
builder = StateGraph(AgentState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "assistant")
self.agent = builder.compile()
def __call__(self, question: str) -> str:
"""
Answers a given question.
Args:
question (str): Question to be answered.
Returns:
str: The answer to the question.
"""
response = self.agent.invoke({"messages": [HumanMessage(content=f"Question:\n {question}")]})
return response['messages'][-1].content |