09. RAG + Image Generator Agent (Reporting)
In this tutorial, we will create an agent that creates reports through web search, PDF document-based search (RAG), image generation, and more.
Reference
Copy
# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv
# Load API key information
load_dotenv()Copy
True Copy
# LangSmith Set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH15-Agent-Projects")Copy
Tool definition
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=발급 받은 Tavily API KEY 입력
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
The following creates a retriever, and a tool based on the retriever you created.
First, load the document, split it, then create a retriever.
Copy
The following defines retriever as a tool.
At this time, document_prompt Defines a template that displays the contents of the document.
Reference
The default value of the document
page_contentJust mark.Therefore, in order to display the page number or source light of the document later, the template must be defined separately.
Copy
DallE image creation tool
This time create a Dall-E image creation tool.
Main properties
model: DALL-E model name to use (default: "dall-e-2", "dall-e-3")n: Number of images to create (default: 1)size: Image size to create"dall-e-2": "1024x1024", "512x512", "256x256"
"dall-e-3": "1024x1024", "1792x1024", "1024x1792"
style: Style of the image to be created (default: "natural", "vivid")quality: Quality of the image to be created (default: "standard", "hd")max_retries: Maximum number of retries at creation
Copy
File management tools
File management tools
CopyFileTool: Copy fileDeleteFileTool: Delete fileFileSearchTool: File SearchMoveFileTool: Move fileReadFileTool: Read fileWriteFileTool: Write fileListDirectoryTool: Directory list lookup
Copy
Copy
Now, put all the tools together.
Copy
Copy
Agent creation
Copy
Now, let's run the agent.
Copy
Copy
Report file created report.md When I check the contents of ), it is output as follows.
Copy

Updated report files ( report.md When I check the contents of ), it is output as follows.
Copy
Copy
Next, let's update the report file via web search.
Copy
Newly written report files ( report-2.md When I check the contents of ), it is output as follows.

Finally, let's request image creation based on the report content.
Copy
Copy
Last generated report file report-3.md If you check some of the contents of ), it will output as follows:

Last updated