Get startedGet started for free

Generating chatbot responses

1. Generating chatbot responses

Now that you've built your chatbot's graph, it's time to

2. Streaming graph events

test it out! In LangGraph, streaming events from a graph allows us to process the steps in an agent's workflow in real time. As we stream, each event represents a single step in the graph, like generating a response or calling a tool. This lets us track the chatbot agent's progress by displaying responses as soon as they're ready. Here, we've printed four events, which include the user's query. Let's first test the chatbot

3. Streaming LLM responses

with just the LLM. We'll start by defining a function called stream_graph_updates() to run the chatbot based on queries supplied as a string called user_input. Inside the function, we'll apply the .stream() method to the graph, passing in the user_input labeled "user" to a dictionary key called "messages". This lets the graph start processing the user's question. Next, we'll loop through each event generated by the graph, retrieving the last chatbot response by accessing event.values() and printing the "messages" in the value dictionary. This displays the chatbot response as soon as it's ready. Finally, we'll define a test query with the string 'Who is Mary Shelley?', passing it to stream_graph_updates() to return a response based just on the LLM.

4. Streaming LLM responses

Since our chatbot currently doesn't use any tools, it'll use the LLM's knowledge base to return an answer about the celebrated science fiction author of "Frankenstein" - Mary Shelley, in the AIMessage field labeled "content". When viewing the rest of the response, we see the LLM alone has detailed information on the topic of Mary Shelley. We can also view the response_metadata attached to the message, including the name of the model used to generate the response. When using LLMs,

5. LLMs and hallucinations

always check original sources, as even powerful LLMs can be prone to hallucinations. Here, NASA engineer Judith Love Cohen's famous son is actually Jack Black, not "Adam Cohen". To limit errors, we can ask

6. Generate a LangGraph diagram

LangGraph to show us how the nodes and edges are connected by displaying the graph. We'll first import the modules Image and display from IPython.display. Then we’ll create a try-and-except block where the .get_graph() method is applied to graph to retrieve its structure. The .draw_mermaid_png() method is then used to render this structure as a visual diagram. Finally, the entire statement is wrapped in Image() and passed to display() to show the diagram. We'll then define an exception that returns an error message if other dependencies for Python visualization are required, depending on your coding environment. Let's go ahead and practice

7. Let's practice!

streaming responses and generating diagrams!