Picture credit — LangChain

Understanding LangGraph — Note-2

@Anil's Notes

--

In this second note, we’re picking up where we left off in “Understanding LangGraph — Note-1.”. we’ll dive into LangGraph by piecing together a basic RAG (Retrieval Augmented Generation) pipeline. To make sure it’s easy for you to follow along, we’ll be using the Google Serper API and an OpenAI key that you need as prerequisites to try.

Basic RAG sample (using LangGraph)

# install modules
!pip install langchain_openai python-dotenv langgraph

#setup environment variables - prerequisites
os.environ["OPENAI_API_KEY"] = "REPLACE_ME"
os.environ["SERPER_API_KEY"] = "REPLACE_ME"

# Sample-3 - Basic RAG pipeline
from langgraph.graph import Graph
from langchain_openai import ChatOpenAI
from langchain.utilities import GoogleSerperAPIWrapper
from langchain.agents import Tool
import operator
from typing import Annotated, Sequence, TypedDict
from langchain_core.messages import BaseMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage, SystemMessage


# Set the model as ChatOpenAI
model = ChatOpenAI(temperature=0)

#Define agent state
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], operator.add]

# Define the function that retrieves information from serper
def retrieve_results(state):
messages = state["messages"]
# Based on the continue condition
# we know the last message involves a function call
last_message = messages[-1]
search_serper_api_wrapper = GoogleSerperAPIWrapper()
search_tool = Tool(name="Search", func=search_serper_api_wrapper.run, description="Get an answer");
response = search_tool.invoke(last_message)
return {"messages": [response]}

# Define the function that calls the LLM to generate
def generate_answer(state):
messages = state['messages']
last_message = messages[-1]
prompt_input_messages = [
SystemMessage(
content="You are a helpful assistant that can understand the given message, context and generate an answer in less than 30 words only."
),
HumanMessage(
content=last_message
),
]
response = model.invoke(prompt_input_messages)
# We return a list, because this will get added to the existing list
return {"messages": [response]}


# Define a new graph
workflow = StateGraph(AgentState)

# Define the two nodes we will cycle between
workflow.add_node("retriever", retrieve_results)
workflow.add_node("generate", generate_answer)

workflow.add_edge("retriever", "generate")

# Set the entrypoint as `agent` where we start
workflow.set_entry_point("retriever")
workflow.set_finish_point("generate")

app = workflow.compile()


input = {"messages": ["What is the most expensive car Elon musk drives?"]}

for output in app.stream(input):
for key, value in output.items():
print(f"Output from node '{key}':")
print("---")
print(value)
print("\n---\n")

final_response = app.invoke(input)["messages"][-1].content
print(final_response)

Output:

Output from node 'retriever':
---
{'messages': ['The most expensive car currently in Elon\'s collection, then, is a Lotus Esprit, according to Automobile Informer. He paid $920,000 for ... The most expensive car in Musk\'s current collection is a Lotus Esprit that he paid $920,000 for at auction. Moreover, it\'s no ordinary Esprit — it\'s the ... Missing: drives? | Show results with:drives?. tesla #teslanews #teslamodels #luxurycars Most Expensive Cars Elon Musk Owns - Tesla News ... Duration: 10:11. Posted: Dec 10, 2023. Elon Musk owns a Tesla Model S, Jaguar E-Type Roadster and a Ford Model T. These Are The 10 Coolest Cars Elon Musk Ever Owned · 10 1978 BMW 320i: You Have To Start From Somewhere · 9 1997 McLaren F1: Big Wins Deserve Big ... 6 of the most expensive celebrity-owned cars ever, from Cristiano Ronaldo\'s limited-edition Bugatti Centodieci to Elon Musk\'s US$20.5 million ... Elon Musk, the visionary entrepreneur and CEO of Tesla and SpaceX, boasts an impressive ... Duration: 6:13. Posted: Jan 18, 2024. and found a 1976 Lotus Esprit buried inside. But the car looked kind of weird, so they had some ... Duration: 1:17. Posted: Dec 29, 2023. The 7 most expensive things Elon Musk owns are mind-blowing · Private jets · Luxury real estate portfolio · Car collection · Luxury accessories · One ... Embark on a journey into the extravagant world of automotive luxury with our latest video, "The ... Duration: 9:09. Posted: Nov 6, 2023.']}

---

Output from node 'generate':
---
{'messages': [AIMessage(content="Elon Musk's most expensive car is a Lotus Esprit, purchased for $920,000.", response_metadata={'token_usage': {'completion_tokens': 20, 'prompt_tokens': 406, 'total_tokens': 426}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3bc1b5746c', 'finish_reason': 'stop', 'logprobs': None})]}

---

Output from node '__end__':
---
{'messages': ['What is the most expensive car Elon musk drives?', 'The most expensive car currently in Elon\'s collection, then, is a Lotus Esprit, according to Automobile Informer. He paid $920,000 for ... The most expensive car in Musk\'s current collection is a Lotus Esprit that he paid $920,000 for at auction. Moreover, it\'s no ordinary Esprit — it\'s the ... Missing: drives? | Show results with:drives?. tesla #teslanews #teslamodels #luxurycars Most Expensive Cars Elon Musk Owns - Tesla News ... Duration: 10:11. Posted: Dec 10, 2023. Elon Musk owns a Tesla Model S, Jaguar E-Type Roadster and a Ford Model T. These Are The 10 Coolest Cars Elon Musk Ever Owned · 10 1978 BMW 320i: You Have To Start From Somewhere · 9 1997 McLaren F1: Big Wins Deserve Big ... 6 of the most expensive celebrity-owned cars ever, from Cristiano Ronaldo\'s limited-edition Bugatti Centodieci to Elon Musk\'s US$20.5 million ... Elon Musk, the visionary entrepreneur and CEO of Tesla and SpaceX, boasts an impressive ... Duration: 6:13. Posted: Jan 18, 2024. and found a 1976 Lotus Esprit buried inside. But the car looked kind of weird, so they had some ... Duration: 1:17. Posted: Dec 29, 2023. The 7 most expensive things Elon Musk owns are mind-blowing · Private jets · Luxury real estate portfolio · Car collection · Luxury accessories · One ... Embark on a journey into the extravagant world of automotive luxury with our latest video, "The ... Duration: 9:09. Posted: Nov 6, 2023.', AIMessage(content="Elon Musk's most expensive car is a Lotus Esprit, purchased for $920,000.", response_metadata={'token_usage': {'completion_tokens': 20, 'prompt_tokens': 406, 'total_tokens': 426}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3bc1b5746c', 'finish_reason': 'stop', 'logprobs': None})]}

---

Elon Musk's most expensive car is a Lotus Esprit, purchased for $920,000. He drives a Tesla Model S as his most used vehicle.

Code step by step explanation

1. Importing Libraries and Modules

Importing essential python modules from LangGraph, LangChain, and other libraries like ChatOpenAI, GoogleSerperAPIWrapper, and operator to facilitate various functionalities within the workflow.

2. Setting Up the ChatOpenAI Model

The ChatOpenAI model is configured with a temperature setting of 0 to ensure precise response generation based on input messages for the “generator” using LLM model.

3. Defining Agent State and Functions

  • AgentState: A typed dictionary is introduced to manage the agent’s state, storing a sequence of messages crucial for processing.
  • retrieve_results: This function retrieves information using GoogleSerperAPIWrapper based on the last message in the agent’s state.
  • generate_answer: The function generates an answer utilizing the ChatOpenAI model, leveraging the last message in the agent’s state for context.

4. Creating a StateGraph Workflow

A StateGraph is instantiated with AgentState to oversee the agent’s state transitions and data flow management throughout the execution.

5. Adding Nodes and Edges

Nodes “retriever” and “generate” are incorporated into the workflow to represent distinct functions that cyclically interact during processing. An edge is established from “retriever” to “generate” to signify the flow of data between these nodes.

6. Setting Entry and Finish Points

The entry point is designated as “retriever,” marking the starting point of the workflow, while “generate” serves as the finish point where the final response is generated.

7. Compiling and Executing the Workflow

The workflow undergoes compilation into an executable application ready for processing input data. An initial message regarding news on LangGraph is provided to initiate the workflow’s execution.

8. Displaying Outputs

During execution, outputs from each node are systematically printed to track the processing steps. The final response generated by the workflow encapsulates the essence of all preceding interactions, culminating in a comprehensive output for further analysis or action.

In the next note, we will explore expanding basic RAG to more advanced RAG pipeline.

--

--

@Anil's Notes

Thoughts I add here are my personal notes and learnings only