Last active
April 22, 2024 06:07
-
-
Save mandemeskel/9aa5ed6ccc6f33c50014912015d6341d to your computer and use it in GitHub Desktop.
Use prompt templates with LangChain RAG agent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# if not in your .env file | |
os.environ['OPENAI_API_KEY'] = '' | |
from langchain_community.document_loaders import TextLoader | |
from langchain.tools.retriever import create_retriever_tool | |
from langchain import hub | |
from langchain.agents import AgentExecutor, create_openai_tools_agent | |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings | |
from langchain_text_splitters import CharacterTextSplitter | |
from langchain_community.vectorstores import FAISS | |
from langchain_core.prompts import ( | |
ChatPromptTemplate, | |
) | |
loader = TextLoader("data.txt") | |
documents = loader.load() | |
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) | |
texts = text_splitter.split_documents(documents) | |
embeddings = OpenAIEmbeddings() | |
db = FAISS.from_documents(texts, embeddings) | |
retriever = db.as_retriever() | |
tool = create_retriever_tool( | |
retriever, | |
"name", | |
"description", | |
) | |
tools = [tool] | |
# base prompt template - you can use any other ones | |
prompt = hub.pull("hwchase17/openai-tools-agent") | |
# build the prompt template using the base | |
# THEN add in any custom system messages | |
# add format for the prompt | |
final_prompt = ChatPromptTemplate.from_messages( | |
[ | |
prompt, | |
("system", "Use only the first person. Don't include any extra text. Don't repeat yourself."), | |
("human", "Find excerpts that demonstrate: {input}"), | |
] | |
) | |
# create the agent, pass in the model, tools (DB/retriever), and the prompt template | |
llm = ChatOpenAI(temperature=0) | |
agent = create_openai_tools_agent(llm, tools, final_prompt) | |
agent_executor = AgentExecutor(agent=agent, tools=tools) | |
resp = agent_executor.invoke({"input": "Knowledge of butterflies."}) | |
print( resp['input'] + '\n' + resp['output'] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment