Skip to main content
Open In ColabOpen on GitHub

Typesense

Typesense 是一个开源的内存搜索引擎,你可以选择自托管或在Typesense Cloud上运行。

Typesense 通过将整个索引存储在 RAM 中(并在磁盘上备份)来专注于性能,并通过简化可用选项和设置良好的默认值来专注于提供开箱即用的开发者体验。

它还允许你将基于属性的过滤与向量查询结合起来,以获取最相关的文档。

本笔记本向您展示如何使用Typesense作为您的VectorStore。

首先安装我们的依赖项:

%pip install --upgrade --quiet  typesense openapi-schema-pydantic langchain-openai langchain-community tiktoken

我们想要使用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.document_loaders import TextLoader
from langchain_community.vectorstores import Typesense
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

让我们导入我们的测试数据集:

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()
docsearch = Typesense.from_documents(
docs,
embeddings,
typesense_client_params={
"host": "localhost", # Use xxx.a1.typesense.net for Typesense Cloud
"port": "8108", # Use 443 for Typesense Cloud
"protocol": "http", # Use https for Typesense Cloud
"typesense_api_key": "xyz",
"typesense_collection_name": "lang-chain",
},
)
query = "What did the president say about Ketanji Brown Jackson"
found_docs = docsearch.similarity_search(query)
print(found_docs[0].page_content)

Typesense 作为检索器

Typesense,与所有其他向量存储一样,是一个使用余弦相似度的LangChain检索器。

retriever = docsearch.as_retriever()
retriever
query = "What did the president say about Ketanji Brown Jackson"
retriever.invoke(query)[0]

这个页面有帮助吗?