Spaces:
Runtime error
Runtime error
build
Browse files- real_agent.py +42 -39
real_agent.py
CHANGED
|
@@ -19,44 +19,47 @@ 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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
chat
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
builder
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
builder.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 19 |
|
| 20 |
os.environ["OPENAI_API_KEY"] = "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"
|
| 21 |
|
|
|
|
| 22 |
|
| 23 |
+
def call_build():
|
| 24 |
+
langfuse_handler = CallbackHandler()
|
| 25 |
+
|
| 26 |
+
# Initialize the web search tool
|
| 27 |
+
new_search_tool = DuckDuckGoSearchRun()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Generate the chat interface, including the tools
|
| 31 |
+
#llm = ChatOpenAI(model="gpt-4o")
|
| 32 |
+
|
| 33 |
+
chat = ChatOpenAI(model="gpt-4o")
|
| 34 |
+
tools = [guest_info_tool, new_search_tool, weather_info_tool, hub_stats_tool]
|
| 35 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 36 |
+
#llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
|
| 37 |
+
|
| 38 |
+
# Generate the AgentState and Agent graph
|
| 39 |
+
class AgentState(TypedDict):
|
| 40 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 41 |
+
|
| 42 |
+
def assistant(state: AgentState):
|
| 43 |
+
return {
|
| 44 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
## The graph
|
| 48 |
+
builder = StateGraph(AgentState)
|
| 49 |
+
|
| 50 |
+
# Define nodes: these do the work
|
| 51 |
+
builder.add_node("assistant", assistant)
|
| 52 |
+
builder.add_node("tools", ToolNode(tools))
|
| 53 |
+
|
| 54 |
+
# Define edges: these determine how the control flow moves
|
| 55 |
+
builder.add_edge(START, "assistant")
|
| 56 |
+
builder.add_conditional_edges(
|
| 57 |
+
"assistant",
|
| 58 |
+
# If the latest message requires a tool, route to tools
|
| 59 |
+
# Otherwise, provide a direct response
|
| 60 |
+
tools_condition,
|
| 61 |
+
)
|
| 62 |
+
builder.add_edge("tools", "assistant")
|
| 63 |
+
|
| 64 |
+
return builder.compile()
|
| 65 |
|