Building Generative AI with Langraph

Sep 1, 2024

Notes on Generative AI Applications with Langraph

Introduction

  • Presenter: Krish Nayak
  • Topic: Building generative AI applications using Langraph
  • Focus: Integrating a chatbot with third-party tools
    • Example tools: Wikipedia, Google Search
  • Duration: 25-30 minutes

Overview of Langraph

  • Provided by Langchern for creating multi-AI agents.
  • Capabilities:
    • Create agents
    • Manage state
    • Develop workflows

Application Development

Architecture Flow

  1. User Query: User asks a question to the chatbot.
  2. Chatbot Response: The chatbot determines:
    • If it can answer using LLMs, it provides a response.
    • If not, it queries an external tool.
  3. External Tool Response: If the tool provides an answer, the chatbot replies with it. If not, the chatbot returns no answer or additional information from LLM.

Step-by-Step Implementation

  1. Library Installation:

    • Install necessary libraries: langraph, langsmith, langchain, langchain_grok.
    • Use pip commands to install libraries.
  2. Importing Libraries:

    • Import essential libraries for managing state and tools.
    • Example tools: Wikipedia API wrapper, R-CIF API wrapper.
  3. Tool Setup:

    • Create instances for wikipedia and R-CIF wrappers.
    • Set parameters for results and character limits.
  4. Creating the Langraph Application:

    • Define a state class utilizing type dict for managing messages.
    • Import necessary functions from Langraph.
    • Set up the LLM model using Grok API.
      • API keys must be stored securely.
  5. Defining the Chatbot:

    • Create chatbot functionality: it will manage state and interact with LLM.
    • Integrate tools within the chatbot's processing capabilities.
  6. Building the Workflow:

    • Use graph builder to create nodes and edges for the process flow:
      • Start node to Chatbot node.
      • Chatbot to Tool node.
      • Conditional edges to allow bi-directional communication between the Chatbot and tools.
      • End node to signify completion of the interaction.
  7. Compiling and Testing:

    • Compile the graph and visualize it using IPython.display.
    • Execute queries to test the chatbot's functionality.

Example Test Case

  • User input: "Hi there! My name is John."
    • The chatbot first tries to search for "John" in Wikipedia.
    • If no relevant result is found, it provides a default response: "Hello John, it's nice to meet you."

Conclusion

  • Encouragement to experiment with multiple tools using Langraph for various queries.
  • End of tutorial.
  • Final words of thanks and encouragement to watch further videos.