Building Advanced Research Agents with Langra

Sep 11, 2024

Lecture Notes on Building Advanced Agents with Langra

Introduction to Langra and Research Agents

  • Focus on building advanced agents using Langra.
  • Specifically using Lang graph compatible with V2 of Lang chain.
  • Aimed at creating a Research Agent for detailed responses rather than quick conversation.

Characteristics of Research Agents

  • Multi-step AI agents provide in-depth, well-researched answers.
  • Users are generally more patient for detailed responses.
  • Capable of referencing multiple sources, including Google searches and archive papers.
  • Designed to facilitate conversational interaction with users.

Overview of the Graph Structure

  • Graph Overview:
    • S: Start Node
    • Oracle: Decision maker (LLM with prompt), manages the nodes:
      • RAG Search Filter
      • RAG Search Fetch
      • Archive Web Search
      • Final Answer
  • The Oracle determines the flow based on user queries.

Graph-based Agent Frameworks

  • Traditional agent frameworks like React encourage iterative reasoning and action steps.
  • React breaks down LLM generation into reasoning and action steps, feeding observations back into the LLM.
  • Moving to a graph-based approach provides more control and transparency.

Lang Graph Overview

  • Lang Graph: A framework to build agents as graphs, providing fine-grained control.
  • Supports a higher level of customization and understanding of agent logic.

Components Needed for Building Agents

  1. Installation of Prerequisites:
    • Libraries for visualization and data handling.
    • Example libraries include Pine Cone, OpenAI, and Semantic Router.
  2. Components:
    • Archive Paper Fetch: Returns abstracts for academic papers based on IDs.
    • Web Search: Provides access to Google search for general queries.
    • RAG Search Components: Access AI archive papers and filter results.
    • Final Answer: Custom format for outputting structured research reports.

Setting Up the Knowledge Base

  • Use Pine Cone and a prebuilt dataset of AI archive papers.
  • Build a knowledge base using OpenAI's embedding model for paper indexing.

Building the Agent State

  • Agent State: Tracks current execution state through the graph.
  • Contains input from users, chat history, and intermediate results.
  • Includes an operator to add to intermediate steps without replacing them.

Creating Custom Tools

  • Tools Creation:
    • Archive Paper Fetch: Retrieves and formats paper abstracts.
    • Web Search: Uses an API to return structured search results.
    • RAG Search and RAG Search with Filter: Access knowledge base for targeted information.
    • Final Answer: Structures the report into a usable format.

Implementing the Oracle

  • The Oracle integrates the LLM and prompt components to manage decision-making.
  • Prompt Structure:
    • Includes system prompts, chat history, and user input.
    • Maintains a scratchpad for intermediate actions and reasoning.

Running the Agent

  • The router identifies which tool to use based on the Oracle's decisions.
  • The Run Tool function executes the tools defined in the graph.
  • The graph is constructed by defining nodes and edges between components.

Testing the Research Agent

  • Input different queries to evaluate the responses and flow.
  • Review the structured reports generated by the agent, including introductions, research steps, conclusions, and sources.

Conclusion

  • Langra allows for building complex agent structures with transparency and customization.
  • The graph-based approach is favorable for developing tailored research agents.
  • Other libraries like Haack and Llama Index are exploring similar functionalities.

Final Thoughts

  • The lecture summarized the process of building a research agent using Langra and emphasized the advantages of a graph-based structure.