Wednesday, May 8, 2024

Langgraph Building a Basic Chatbot

Part 1: Build a Basic Chatbot

Start by creating a StateGraph. A StateGraph object defines the structure of our chatbot as a "state machine". We'll add nodes to represent the llm and functions our chatbot can call and edges to specify how the bot should transition between these functions.

defined our State as a TypedDict with a single key: messages. The messages key is annotated with the add_messages function, which tells LangGraph to append new messages to the existing list, rather than overwriting it.

So now our graph knows two things:

Every node we define will receive the current State as input and return a value that updates that state.

messages will be appended to the current list, rather than directly overwritten. This is communicated via the prebuilt add_messages function in the Annotated syntax.

from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(model="claude-3-haiku-20240307")

def chatbot(state: State):

    return {"messages": [llm.invoke(state["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)

add an entry point. This tells our graph where to start its work each time we run it.

graph_builder.set_entry_point("chatbot")

Similarly, set a finish point. This instructs the graph "any time this node is run, you can exit."

graph_builder.set_finish_point("chatbot")

Finally, we'll want to be able to run our graph. To do so, call "compile()" on the graph builder. This creates a "CompiledGraph" we can use invoke on our state.

graph = graph_builder.compile()

You can visualize the graph using the get_graph method and one of the "draw" methods, like draw_ascii or draw_png. The draw methods each require additional dependencies.

from IPython.display import Image, display

try:

    display(Image(graph.get_graph().draw_mermaid_png()))

except:

    # This requires some extra dependencies and is optional

    pass

You can exit the chat loop at any time by typing "quit", "exit", or "q".

while True:

    user_input = input("User: ")

    if user_input.lower() in ["quit", "exit", "q"]:

        print("Goodbye!")

        break

    for event in graph.stream({"messages": ("user", user_input)}):

        for value in event.values():

            print("Assistant:", value["messages"][-1].content)


References

https://langchain-ai.github.io/langgraph/how-tos/docs/quickstart/

No comments:

Post a Comment