Simple LangChain Agent

Throughout this course, you will be adapting a simple LangChain agent to interact with Neo4j.

You will update the agent to query a Neo4j graph database, retrieve information using RAG and GraphRAG, and dynamically generate Cypher queries based on user input.

In this lesson, you will review the agents code to understand how it works.

Agent

Open the genai-integration-langchain/simple_agent.py file.

python
simple_agent.py
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/simple_agent.py[tag=**]

Review the code and try to answer the following questions:

  1. What is the agents purpose?

  2. What context is added to the agents prompt?

  3. What do you think the final answer will be?

Run the agent to see what it does.

Click to reveal the answers

The agent is designed to answer questions about the information that is provided in the context.

The context contains weather information about London and San Francisco, so:

  • When passed the question "What is the weather in San Francisco?".

  • The agent responds with "Sunny skies, raining overnight.".

Regardless of what data is in the context, the agent will provide an answer based on the information it has.

The application is a simple LangGraph agent that has 2 steps:

  1. Retrieve information

  2. Generate an answer based on the retrieved information

The code has 4 main sections:

  1. Create an LLM and Prompt

  2. Define the application state

  3. Create the application workflow

  4. Invoke the application

LLM and Prompt

The agent uses an OpenAI LLM and a simple prompt to generate an answer based on the retrieved information.

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/simple_agent.py[tag=llm_prompt]

The prompt sets the instructions for the LLM to generate an answer based on the retrieved information.

The variables context and question are used to provide the information to the LLM.

Application State

The application state holds the information that is required to run the agent. The state includes the original question, the context, and the generated answer.

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/simple_agent.py[tag=application_state]

The context can be any information that is relevant to the question being asked.

Application Workflow

The application workflow consists of:

  1. Functions that will be executed to retrieve the context and generate the answer:

    python
    Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/simple_agent.py[tag=application_functions]

    The application state will be updated with data returned by the functions.

  2. The workflow defines the order the functions are executed:

    python
    Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/simple_agent.py[tag=application_workflow]

    The retrieve function is called at the START of the workflow, before the generate function.

Invoke

Finally, the application is invoked passing a question to the agent and printing the answer:

python
Unresolved directive in lesson.adoc - include::{repository-raw}/new-course/genai-integration-langchain/solutions/simple_agent.py[tag=invoke]

Check Your Understanding

No Context

What will the agent most likely do if asked a question relating to a subject not in the context?

  • ❏ Raise an error

  • ❏ Generate a response based on the LLM’s training data

  • ❏ Ask for more information

  • ✓ Respond with "I don’t know"

Hint

Review the prompt and consider how it will influence the agent when questions that are not covered by the provided context.

Solution

The prompt gives specific instructions to the LLM to answer questions based solely on the provided context. If the context does not contain relevant information, the agent will likely respond with "I don’t know".

Lesson Summary

In this lesson, you review a simple LangChain agent that generates an answer based on a provided context.

In the next lesson, you will modify the agent to retrieve the schema from a Neo4j database and use it to answer questions about a graph.