Skip to main content
Open In ColabOpen on GitHub

腾讯云向量数据库

Tencent Cloud VectorDB 是一个完全托管的、自主研发的、企业级分布式数据库服务,专为存储、检索和分析多维向量数据而设计。

在演练中,我们将使用腾讯云VectorDB演示SelfQueryRetriever

创建一个TencentVectorDB实例

首先,我们需要创建一个TencentVectorDB并用一些数据填充它。我们已经创建了一个包含电影摘要的小型演示文档集。

注意: 自查询检索器要求您安装 larkpip install lark)以及特定于集成的需求。

%pip install --upgrade --quiet tcvectordb langchain-openai tiktoken lark

[notice] A new release of pip is available: 23.2.1 -> 24.0
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.

我们想要使用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:")

创建一个TencentVectorDB实例并用一些数据填充它:

from langchain_community.vectorstores.tencentvectordb import (
ConnectionParams,
MetaField,
TencentVectorDB,
)
from langchain_core.documents import Document
from tcvectordb.model.enum import FieldType

meta_fields = [
MetaField(name="year", data_type="uint64", index=True),
MetaField(name="rating", data_type="string", index=False),
MetaField(name="genre", data_type=FieldType.String, index=True),
MetaField(name="director", data_type=FieldType.String, index=True),
]

docs = [
Document(
page_content="The Shawshank Redemption is a 1994 American drama film written and directed by Frank Darabont.",
metadata={
"year": 1994,
"rating": "9.3",
"genre": "drama",
"director": "Frank Darabont",
},
),
Document(
page_content="The Godfather is a 1972 American crime film directed by Francis Ford Coppola.",
metadata={
"year": 1972,
"rating": "9.2",
"genre": "crime",
"director": "Francis Ford Coppola",
},
),
Document(
page_content="The Dark Knight is a 2008 superhero film directed by Christopher Nolan.",
metadata={
"year": 2008,
"rating": "9.0",
"genre": "science fiction",
"director": "Christopher Nolan",
},
),
Document(
page_content="Inception is a 2010 science fiction action film written and directed by Christopher Nolan.",
metadata={
"year": 2010,
"rating": "8.8",
"genre": "science fiction",
"director": "Christopher Nolan",
},
),
Document(
page_content="The Avengers is a 2012 American superhero film based on the Marvel Comics superhero team of the same name.",
metadata={
"year": 2012,
"rating": "8.0",
"genre": "science fiction",
"director": "Joss Whedon",
},
),
Document(
page_content="Black Panther is a 2018 American superhero film based on the Marvel Comics character of the same name.",
metadata={
"year": 2018,
"rating": "7.3",
"genre": "science fiction",
"director": "Ryan Coogler",
},
),
]

vector_db = TencentVectorDB.from_documents(
docs,
None,
connection_params=ConnectionParams(
url="http://10.0.X.X",
key="eC4bLRy2va******************************",
username="root",
timeout=20,
),
collection_name="self_query_movies",
meta_fields=meta_fields,
drop_old=True,
)

创建我们的自查询检索器

现在我们可以实例化我们的检索器。为此,我们需要提前提供一些关于我们的文档支持的元数据字段的信息以及文档内容的简短描述。

from langchain.chains.query_constructor.schema import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain_openai import ChatOpenAI

metadata_field_info = [
AttributeInfo(
name="genre",
description="The genre of the movie",
type="string",
),
AttributeInfo(
name="year",
description="The year the movie was released",
type="integer",
),
AttributeInfo(
name="director",
description="The name of the movie director",
type="string",
),
AttributeInfo(
name="rating", description="A 1-10 rating for the movie", type="string"
),
]
document_content_description = "Brief summary of a movie"
llm = ChatOpenAI(temperature=0, model="gpt-4", max_tokens=4069)
retriever = SelfQueryRetriever.from_llm(
llm, vector_db, document_content_description, metadata_field_info, verbose=True
)

测试一下

现在我们可以尝试实际使用我们的检索器了!

# This example only specifies a relevant query
retriever.invoke("movies about a superhero")
[Document(page_content='The Dark Knight is a 2008 superhero film directed by Christopher Nolan.', metadata={'year': 2008, 'rating': '9.0', 'genre': 'science fiction', 'director': 'Christopher Nolan'}),
Document(page_content='The Avengers is a 2012 American superhero film based on the Marvel Comics superhero team of the same name.', metadata={'year': 2012, 'rating': '8.0', 'genre': 'science fiction', 'director': 'Joss Whedon'}),
Document(page_content='Black Panther is a 2018 American superhero film based on the Marvel Comics character of the same name.', metadata={'year': 2018, 'rating': '7.3', 'genre': 'science fiction', 'director': 'Ryan Coogler'}),
Document(page_content='The Godfather is a 1972 American crime film directed by Francis Ford Coppola.', metadata={'year': 1972, 'rating': '9.2', 'genre': 'crime', 'director': 'Francis Ford Coppola'})]
# This example only specifies a filter
retriever.invoke("movies that were released after 2010")
[Document(page_content='The Avengers is a 2012 American superhero film based on the Marvel Comics superhero team of the same name.', metadata={'year': 2012, 'rating': '8.0', 'genre': 'science fiction', 'director': 'Joss Whedon'}),
Document(page_content='Black Panther is a 2018 American superhero film based on the Marvel Comics character of the same name.', metadata={'year': 2018, 'rating': '7.3', 'genre': 'science fiction', 'director': 'Ryan Coogler'})]
# This example specifies both a relevant query and a filter
retriever.invoke("movies about a superhero which were released after 2010")
[Document(page_content='The Avengers is a 2012 American superhero film based on the Marvel Comics superhero team of the same name.', metadata={'year': 2012, 'rating': '8.0', 'genre': 'science fiction', 'director': 'Joss Whedon'}),
Document(page_content='Black Panther is a 2018 American superhero film based on the Marvel Comics character of the same name.', metadata={'year': 2018, 'rating': '7.3', 'genre': 'science fiction', 'director': 'Ryan Coogler'})]

筛选 k

我们也可以使用自我查询检索器来指定k:要获取的文档数量。

我们可以通过将enable_limit=True传递给构造函数来实现这一点。

retriever = SelfQueryRetriever.from_llm(
llm,
vector_db,
document_content_description,
metadata_field_info,
verbose=True,
enable_limit=True,
)
retriever.invoke("what are two movies about a superhero")
[Document(page_content='The Dark Knight is a 2008 superhero film directed by Christopher Nolan.', metadata={'year': 2008, 'rating': '9.0', 'genre': 'science fiction', 'director': 'Christopher Nolan'}),
Document(page_content='The Avengers is a 2012 American superhero film based on the Marvel Comics superhero team of the same name.', metadata={'year': 2012, 'rating': '8.0', 'genre': 'science fiction', 'director': 'Joss Whedon'})]

这个页面有帮助吗?