跳至主要内容

DuckDB

本笔记本展示了如何使用DuckDB作为向量存储。

! pip install duckdb langchain langchain-community langchain-openai

我们想使用 OpenAIEmbeddings,因此我们必须获取 OpenAI API 密钥。

import getpass
import os

if "OPENAI_API_KEY" not in os.environ:
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()
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)

此页面对您有帮助吗?


您也可以在 GitHub 上留下详细反馈 GitHub.