Spaces:
Sleeping
Sleeping
File size: 1,610 Bytes
83a4aff 33dc83e 83a4aff | 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 | from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain.chat_models import init_chat_model
import os
class State(TypedDict):
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
key = os.getenv("OPENAI_API_KEY")
llm = init_chat_model("openai:gpt-4o-mini")
tool = {"type": "web_search_preview"}
llm_with_tools = llm.bind_tools(
[
{
"type": "mcp",
"server_label": "clickup_data",
"server_url": "https://roniorque-fastapi-test-server.hf.space/mcp",
"require_approval": "never",
}
]
+ [tool]
)
def chatbot(state: State):
return {"messages": [llm_with_tools.invoke(state["messages"])]}
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()
if __name__ == "__main__":
print("Chatbot started! Type 'quit' to exit.")
while True:
user_input = input("\nYou: ")
if user_input.lower() in ['quit', 'exit', 'bye']:
print("Goodbye!")
break
# Create initial state with user message
initial_state = {
"messages": [{"role": "user", "content": user_input}]
}
# Run the graph
result = graph.invoke(initial_state)
# Get the bot's response
bot_response = result["messages"][-1].content
print(f"Bot: {bot_response}")
|