Spaces:
Sleeping
Sleeping
""" | |
from typing import Annotated, TypedDict | |
from langchain_community.chat_models import ChatHuggingFace | |
from langchain_community.llms import HuggingFaceEndpoint | |
from langchain_core.messages import AIMessage, AnyMessage, HumanMessage | |
from langgraph.graph import START, StateGraph | |
from langgraph.graph.message import add_messages | |
from langgraph.prebuilt import ToolNode, tools_condition | |
from retriever import guest_info_tool | |
from tools import (absolute, add, divide, exponential, floor_divide, | |
get_current_time_in_timezone, logarithm, modulus, multiply, | |
power, roman_calculator_converter, square_root, subtract, | |
web_search) | |
# Generate the chat interface, including the tools | |
llm = HuggingFaceEndpoint( | |
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", | |
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN, | |
) | |
chat = ChatHuggingFace(llm=llm, verbose=True) | |
tools = [ | |
multiply, | |
add, | |
subtract, | |
power, | |
divide, | |
modulus, | |
square_root, | |
floor_divide, | |
absolute, | |
logarithm, | |
exponential, | |
web_search, | |
roman_calculator_converter, | |
get_current_time_in_timezone, | |
] | |
chat_with_tools = chat.bind_tools(tools) | |
# Generate the AgentState and Agent graph | |
class AgentState(TypedDict): | |
messages: Annotated[list[AnyMessage], add_messages] | |
def assistant(state: AgentState): | |
return { | |
"messages": [chat_with_tools.invoke(state["messages"])], | |
} | |
## The graph | |
builder = StateGraph(AgentState) | |
# Define nodes: these do the work | |
builder.add_node("assistant", assistant) | |
builder.add_node("tools", ToolNode(tools)) | |
# Define edges: these determine how the control flow moves | |
builder.add_edge(START, "assistant") | |
builder.add_conditional_edges( | |
"assistant", | |
# If the latest message requires a tool, route to tools | |
# Otherwise, provide a direct response | |
tools_condition, | |
) | |
builder.add_edge("tools", "assistant") | |
alfred = builder.compile() | |
""" | |