from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages
classState(TypedDict): # Messages have the type "list". The `add_messages` function # in the annotation defines how this state key should be updated # (in this case, it appends messages to the list, rather than overwriting them) messages: Annotated[list, add_messages]
# The first argument is the unique node name # The second argument is the function or object that will be called whenever # the node is used. graph_builder.add_node("chatbot", chatbot)
withopen("graph.png", "wb") as f: f.write(png_bytes)
import os os.system("open graph.png")
运行聊天机器人
现在运行聊天机器人!
您可以随时通过输入、或退出quit聊天exit循环q。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
defstream_graph_updates(user_input: str): for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}): for value in event.values(): print("Assistant:", value["messages"][-1].content)
whileTrue: try: user_input = input("User: ") if user_input.lower() in ["quit", "exit", "q"]: print("Goodbye!") break stream_graph_updates(user_input) except: # fallback if input() is not available user_input = "What do you know about LangGraph?" print("User: " + user_input) stream_graph_updates(user_input) break
from typing_extensions import TypedDict from dotenv import load_dotenv from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages from langchain_openai import ChatOpenAI
classState(TypedDict): # Messages have the type "list". The `add_messages` function # in the annotation defines how this state key should be updated # (in this case, it appends messages to the list, rather than overwriting them) messages: Annotated[list, add_messages]
# The first argument is the unique node name # The second argument is the function or object that will be called whenever # the node is used. graph_builder.add_node("chatbot", chatbot)
withopen("graph.png", "wb") as f: f.write(png_bytes)
import os os.system("open graph.png")
defstream_graph_updates(user_input: str): for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}): for value in event.values(): print("Assistant:", value["messages"][-1].content)
whileTrue: try: user_input = input("User: ") if user_input.lower() in ["quit", "exit", "q"]: print("Goodbye!") break stream_graph_updates(user_input) except: # fallback if input() is not available user_input = "What do you know about LangGraph?" print("User: " + user_input) stream_graph_updates(user_input) break
对话过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User: What do you know about LangGraph? Assistant: LangGraph is an open-source knowledge graph project initiated and developed by Alibaba Cloud. It aims to provide researchers and developers with tools and resources for building and utilizing knowledge graphs in various applications, including but not limited to natural language processing (NLP), semantic search, recommendation systems, and more.
Key features of LangGraph include:
1. **Data Integration**: Supports the integration of diverse data sources into a unified knowledge graph. 2. **Knowledge Extraction**: Provides tools for extracting structured knowledge from unstructured or semi-structured data. 3. **Query Processing**: Offers efficient query processing capabilities to support complex queries over large-scale graphs. 4. **Visualization and Analysis**: Includes visualization tools to help users understand the structure and content of their knowledge graphs.
LangGraph is designed to be flexible, scalable, and easy to integrate into existing systems or workflows. It leverages Alibaba Cloud's expertise in big data and AI technologies to provide robust support for knowledge graph applications.
If you need more specific information about LangGraph, such as its technical details, use cases, or documentation, I can try to provide that based on publicly available information. User: q Goodbye!