import pathlib
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.langchaindb import LangChainVectorDb
from langchain.text_splitter import CharacterTextSplitter
from langchain_chroma import Chroma
from langchain_community.document_loaders import TextLoader
from langchain_openai import OpenAIEmbeddings
# Define the directory where the Chroma database is located
chroma_db_dir = pathlib.Path("./chroma_db")
# Define the path to the document to be loaded into the knowledge base
state_of_the_union = pathlib.Path(
"cookbook/knowledge/testing_resources/state_of_the_union.txt"
)
# Load the document
raw_documents = TextLoader(str(state_of_the_union), encoding="utf-8").load()
# Split the document into chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
# Embed each chunk and load it into the vector store
Chroma.from_documents(
documents, OpenAIEmbeddings(), persist_directory=str(chroma_db_dir)
)
# Get the vector database
db = Chroma(embedding_function=OpenAIEmbeddings(), persist_directory=str(chroma_db_dir))
# Create a knowledge retriever from the vector store
knowledge_retriever = db.as_retriever()
knowledge = Knowledge(
vector_db=LangChainVectorDb(knowledge_retriever=knowledge_retriever)
)
agent = Agent(knowledge=knowledge)
agent.print_response("What did the president say?", markdown=True)
Create a virtual environment
Terminal
and create a python virtual environment.python3 -m venv .venv
source .venv/bin/activate
Install libraries
pip install -U langchain langchain-community langchain-openai langchain-chroma pypdf openai agno
Set environment variables
export OPENAI_API_KEY=xxx
Run Agent
python cookbook/knowledge/vector_db/langchain/langchain_db.py