以编程方式使用文档

Kinetica 是一个内置向量相似性搜索支持的数据库。

它支持:

  • - 精确和近似最近邻搜索
  • - L2 距离、内积和余弦距离

本笔记本展示了如何使用基于 Kinetica 向量存储的检索器(Kinetica).

请确保此连接器已安装在您的工作环境中:

pip install -qU langchain-kinetica langchain-community langchain-openai

我们想使用 OpenAIEmbeddings 所以我们必须获取 OpenAI API 密钥。

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)

# 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"

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

# create retriever from the vector store
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.