Skip to main content
Kinetica 是一个内置向量相似性搜索支持的数据库。 它支持:
  • 精确和近似最近邻搜索
  • L2 距离、内积和余弦距离
本 notebook 展示如何使用基于 Kinetica 向量存储(Kinetica)的检索器。 请确保在你的工作环境中安装了此连接器:
pip install -qU langchain-kinetica langchain-community langchain-openai
我们希望使用 OpenAIEmbeddings,因此需要获取 OpenAI API 密钥。
import getpass
import os

from langchain_openai import OpenAIEmbeddings

if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

embeddings = OpenAIEmbeddings()
你必须在以下环境变量中设置数据库连接。如果使用虚拟环境,可以在项目的 .env 文件中设置:
  • KINETICA_URL:数据库连接 URL(例如 http://localhost:9191
  • KINETICA_USER:数据库用户名
  • KINETICA_PASSWD:安全密码
from gpudb import GPUdb

from langchain_kinetica import KineticaSettings, KineticaVectorstore

kdbc = GPUdb.get_connection()

k_config = KineticaSettings(kdbc=kdbc)
k_config
2026-02-02 20:58:48.261 INFO     [GPUdb] Connected to Kinetica! (host=http://localhost:19191 api=7.2.3.3 server=7.2.3.5)

KineticaSettings(kdbc=<gpudb.gpudb.GPUdb object at 0x1141e7b90>, database='langchain', table='langchain_kinetica_embeddings', metric='l2')

创建向量存储

from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter

loader = TextLoader("./state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
results = text_splitter.split_documents(documents)

# Kinetica 模块将尝试使用集合名称创建一张表。
# 因此,请确保集合名称唯一,且用户具有创建表的权限。

COLLECTION_NAME = "state_of_the_union_test"

vectorstore = KineticaVectorstore.from_documents(
    embedding=embeddings,
    documents=results,
    collection_name=COLLECTION_NAME,
    config=k_config,
    pre_delete_collection=True,
)

使用检索器搜索

from langchain_core.vectorstores import VectorStoreRetriever

# 从向量存储创建检索器
retriever: VectorStoreRetriever = vectorstore.as_retriever(search_kwargs={"k": 2})

results = retriever.invoke("What did the president say about Ketanji Brown Jackson")

print(results[0].page_content)
Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections.

Tonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.