基于 Kinetica 向量存储的检索器
Kinetica 是一个具有集成向量相似性搜索支持的数据库。
它支持
- 精确和近似最近邻搜索
- L2 距离、内积和余弦距离
此笔记本演示了如何使用基于 Kinetica 向量存储 (Kinetica
) 的检索器。
# Please ensure that this connector is installed in your working environment.
%pip install gpudb==7.2.0.9
我们希望使用 OpenAIEmbeddings
,因此我们必须获取 OpenAI API 密钥。
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
## Loading Environment Variables
from dotenv import load_dotenv
load_dotenv()
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import (
Kinetica,
KineticaSettings,
)
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
API 参考:TextLoader | Kinetica | KineticaSettings | Document | OpenAIEmbeddings | CharacterTextSplitter
# Kinetica needs the connection to the database.
# This is how to set it up.
HOST = os.getenv("KINETICA_HOST", "http://127.0.0.1:9191")
USERNAME = os.getenv("KINETICA_USERNAME", "")
PASSWORD = os.getenv("KINETICA_PASSWORD", "")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
def create_config() -> KineticaSettings:
return KineticaSettings(host=HOST, username=USERNAME, password=PASSWORD)
从向量存储创建检索器
loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
# The Kinetica Module will try to create a table with the name of the collection.
# So, make sure that the collection name is unique and the user has the permission to create a table.
COLLECTION_NAME = "state_of_the_union_test"
connection = create_config()
db = Kinetica.from_documents(
embedding=embeddings,
documents=docs,
collection_name=COLLECTION_NAME,
config=connection,
)
# create retriever from the vector store
retriever = db.as_retriever(search_kwargs={"k": 2})
使用检索器搜索
result = retriever.get_relevant_documents(
"What did the president say about Ketanji Brown Jackson"
)
print(docs[0].page_content)