06. Agentic RAG

In this chapter, we will create an agent that accesses the latest information through document search and generates answers with search results.

Let's create an agent that searches for and answers documents based on your questions, or uses Internet search tools to answer them.

Reference

  • If you do RAG, but use Agent to do RAG, Agentic RAG It is called.

Tools

You can define the tools that Agent will utilize to make them utilized when performing Agent's inference.

Tavily Search is a representative of them Search tools is. Search allows you to access the latest information and generate answers with search results. Tools offer a variety of different types and methodologies, including search tools like this, tools that can run Python code, and tools that run directly defined functions.

Web Search Tool: Tavily Search

LangChain has built-in tools that make Tavily search engines easy to use as tools.

API KEY must be issued to use Tavily Search.

Register the issued API KEY for environmental variables as follows:

.env Register as follows in the file:

  • TAVILY_API_KEY=Enter the issued Tavily API KEY

Copy

# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key infomation
load_dotenv()

Copy

Copy

Copy

Copy

search.invoke The function executes a search for a given string.

invoke() Perform a search by putting the search terms you want to search for in the function.

Copy

Copy

Document-based document search tool: Retriever

It also generates retrievers to perform queries on our data.

Documents utilized for practice

Software Policy Institute (SPRi)-December 2023

  • Author: Jaeheung Lee (AI Policy Institute Office Liability Institute), Lee Ji-soo (AI Policy Lab Yi Phyang Institute)

  • Link: https://spri.kr/posts/view/23669

  • File name: SPRI_AI_Brief_2023년12월호_F.pdf

Files downloaded for practice data Please copy to folder

This code builds a document retrieval system using web-based document loaders, document splitters, vector repositories, and OpenAI embedding.

PDF documents here FAISS Generates retrievers that are stored and viewed in DB.

Copy

This function retriever Object invoke() Use to most of your questions Relevance documents Used to find.

Copy

Copy

Now that we have filled the index to perform the search, we can easily convert it into a tool that agents can use properly.

create_retriever_tool As a function retriever Convert to tools.

Copy

Agent Defines a list of tools to use

Now that you have created both, you can create a list of tools for Agent to use.

tools List search Wow retriever_tool Includes.

Copy

Agent creation

Now that you have defined the tool, you can create an agent.

First, the Agent defines the LLM to utilize, and the Agent defines the Prompt to refer to.

Reference -If you don't support multi-turn conversations, you can remove "chat_history".

Copy

Next, create a Tool Calling Agent.

Copy

Finally, created agent Run AgentExecutor Generate.

Reference

  • verbose=False Set to omit the intermediate step output.

Copy

Execute agent

Now you can run the agent for some queries!

All these queries are now Stateless It is a question (I do not remember the previous interaction).

agent_executor Object invoke Methods are processed by taking a factor in the form of a dictionary. In this example input Kier hi! I am passing the dictator assigned a value as a factor. It is commonly used to process input from objects such as AI agents, function launchers, or command handlers.

Copy

Copy

Copy

agent_executor Object invoke Using the method, the question is provided as input.

Copy

Copy

Agent to remember the previous conversation

To remember the previous conversation RunnableWithMessageHistory Using AgentExecutor Wraps.

RunnableWithMessageHistory Please refer to the link below for more information on this.

Reference

Copy

Copy

Copy

Copy

Copy

Agent template

Here is the full template code.

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Last updated