Spaces:
Runtime error
Runtime error
builder
Browse files- real_agent.py +62 -0
real_agent.py
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TypedDict, Annotated
|
| 2 |
+
from langgraph.graph.message import add_messages
|
| 3 |
+
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
| 4 |
+
from langgraph.prebuilt import ToolNode
|
| 5 |
+
from langgraph.graph import START, StateGraph
|
| 6 |
+
from langgraph.prebuilt import tools_condition
|
| 7 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 8 |
+
from langchain_openai import ChatOpenAI
|
| 9 |
+
import os
|
| 10 |
+
from tools import DuckDuckGoSearchRun, weather_info_tool, hub_stats_tool
|
| 11 |
+
from retriever import guest_info_tool
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from langfuse.langchain import CallbackHandler
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-842782bf-0e8d-4f50-8fb0-157464d5fe28"
|
| 17 |
+
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-3179ce99-f0e4-46f1-bbde-df3752414be3"
|
| 18 |
+
os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com"
|
| 19 |
+
|
| 20 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 21 |
+
|
| 22 |
+
langfuse_handler = CallbackHandler()
|
| 23 |
+
|
| 24 |
+
# Initialize the web search tool
|
| 25 |
+
new_search_tool = DuckDuckGoSearchRun()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Generate the chat interface, including the tools
|
| 29 |
+
#llm = ChatOpenAI(model="gpt-4o")
|
| 30 |
+
|
| 31 |
+
chat = ChatOpenAI(model="gpt-4o")
|
| 32 |
+
tools = [guest_info_tool, new_search_tool, weather_info_tool, hub_stats_tool]
|
| 33 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 34 |
+
#llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
|
| 35 |
+
|
| 36 |
+
# Generate the AgentState and Agent graph
|
| 37 |
+
class AgentState(TypedDict):
|
| 38 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 39 |
+
|
| 40 |
+
def assistant(state: AgentState):
|
| 41 |
+
return {
|
| 42 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
## The graph
|
| 46 |
+
builder = StateGraph(AgentState)
|
| 47 |
+
|
| 48 |
+
# Define nodes: these do the work
|
| 49 |
+
builder.add_node("assistant", assistant)
|
| 50 |
+
builder.add_node("tools", ToolNode(tools))
|
| 51 |
+
|
| 52 |
+
# Define edges: these determine how the control flow moves
|
| 53 |
+
builder.add_edge(START, "assistant")
|
| 54 |
+
builder.add_conditional_edges(
|
| 55 |
+
"assistant",
|
| 56 |
+
# If the latest message requires a tool, route to tools
|
| 57 |
+
# Otherwise, provide a direct response
|
| 58 |
+
tools_condition,
|
| 59 |
+
)
|
| 60 |
+
builder.add_edge("tools", "assistant")
|
| 61 |
+
alfred = builder.compile()
|
| 62 |
+
|