DuckDB
此笔记本演示了如何使用DuckDB
作为向量存储。
! pip install duckdb langchain langchain-community langchain-openai
我们想要使用 OpenAIEmbeddings,因此必须获取 OpenAI API 密钥。
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
from langchain_community.vectorstores import DuckDB
from langchain_openai import OpenAIEmbeddings
API 参考:DuckDB | OpenAIEmbeddings
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
documents = CharacterTextSplitter().split_documents(documents)
embeddings = OpenAIEmbeddings()
API 参考:TextLoader | CharacterTextSplitter
docsearch = DuckDB.from_documents(documents, embeddings)
query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)
print(docs[0].page_content)