向量化器
HFTextVectorizer
class HFTextVectorizer(model='sentence-transformers/all-mpnet-base-v2', dtype='float32', *, dims=None)
基类: BaseVectorizer
HFTextVectorizer 类旨在利用 Hugging Face 的 Sentence Transformers 的强大功能来生成文本嵌入。此向量化器在需要高级自然语言处理和理解的场景中特别有用,并且非常适合在您自己的硬件上运行(免费)。
使用此向量化器需要指定 Hugging Face 庞大 Sentence Transformers 集合中的预训练模型。这些模型在各种数据集和任务上进行训练,确保在不同的文本嵌入需求中具有多功能性和强大的性能。此外,请确保使用 pip install sentence-transformers==2.2.2 安装了 sentence-transformers 库。
# Embedding a single text
vectorizer = HFTextVectorizer(model="sentence-transformers/all-mpnet-base-v2")
embedding = vectorizer.embed("Hello, world!")
# Embedding a batch of texts
embeddings = vectorizer.embed_many(["Hello, world!", "How are you?"], batch_size=2)
初始化 Hugging Face 文本向量化器。
- 参数
- model (str) – 用于嵌入的 Hugging Face Sentence Transformers 的预训练模型。默认为 ‘sentence-transformers/all-mpnet-base-v2’。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ImportError – 如果未安装 sentence-transformers 库。
- ValueError – 如果设置嵌入模型维度时出错。
- ValueError – 如果提供了无效的 dtype。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 Hugging Face Sentence Transformer 嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果为文本传入了错误的输入类型。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 Hugging Face Sentence Transformer 异步嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
OpenAITextVectorizer
class OpenAITextVectorizer(model='text-embedding-ada-002', api_config=None, dtype='float32', *, dims=None)
基类: BaseVectorizer
OpenAITextVectorizer 类利用 OpenAI 的 API 为文本数据生成嵌入。
此向量化器旨在与 OpenAI 的嵌入 API 交互,需要 API 密钥进行身份验证。密钥可以直接在 api_config 字典中提供,或通过 OPENAI_API_KEY 环境变量提供。用户必须从 OpenAI 网站 (https://api.openai.com/) 获取 API 密钥。此外,必须使用 pip install openai>=1.13.0 安装 openai python 客户端。
此向量化器支持同步和异步操作,允许批量处理文本并灵活处理预处理任务。
# Synchronous embedding of a single text
vectorizer = OpenAITextVectorizer(
model="text-embedding-ada-002",
api_config={"api_key": "your_api_key"} # OR set OPENAI_API_KEY in your env
)
embedding = vectorizer.embed("Hello, world!")
# Asynchronous batch embedding of multiple texts
embeddings = await vectorizer.aembed_many(
["Hello, world!", "How are you?"],
batch_size=2
)
初始化 OpenAI 向量化器。
- 参数
- model (str) – 用于嵌入的模型。默认为 ‘text-embedding-ada-002’。
- api_config (Optional [ Dict ] , optional) – 包含 API 密钥和任何其他 OpenAI API 选项的字典。默认为 None。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ImportError – 如果未安装 openai 库。
- ValueError – 如果未提供 OpenAI API 密钥。
- ValueError – 如果提供了无效的 dtype。
aembed(text, preprocess=None, as_buffer=False, **kwargs)
使用 OpenAI API 异步嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果为文本传入了错误的输入类型。
aembed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 OpenAI API 异步嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果为文本传入了错误的输入类型。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 OpenAI API 嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果为文本传入了错误的输入类型。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 OpenAI API 嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果为文本传入了错误的输入类型。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
AzureOpenAITextVectorizer
class AzureOpenAITextVectorizer(model='text-embedding-ada-002', api_config=None, dtype='float32', *, dims=None)
基类: BaseVectorizer
AzureOpenAITextVectorizer 类利用 Azure OpenAI 的 API 为文本数据生成嵌入。
此向量化器旨在与 Azure OpenAI 的嵌入 API 交互,需要 API 密钥、Azure OpenAI 部署终结点和 API 版本。这些值可以直接在 api_config 字典中提供,参数为 ‘azure_endpoint’、‘api_version’ 和 ‘api_key’,或通过环境变量 ‘AZURE_OPENAI_ENDPOINT’、‘OPENAI_API_VERSION’ 和 ‘AZURE_OPENAI_API_KEY’ 提供。用户必须从其 Azure OpenAI 服务的“密钥和终结点”部分获取这些值。此外,必须使用 pip install openai>=1.13.0 安装 openai python 客户端。
此向量化器支持同步和异步操作,允许批量处理文本并灵活处理预处理任务。
# Synchronous embedding of a single text
vectorizer = AzureOpenAITextVectorizer(
model="text-embedding-ada-002",
api_config={
"api_key": "your_api_key", # OR set AZURE_OPENAI_API_KEY in your env
"api_version": "your_api_version", # OR set OPENAI_API_VERSION in your env
"azure_endpoint": "your_azure_endpoint", # OR set AZURE_OPENAI_ENDPOINT in your env
}
)
embedding = vectorizer.embed("Hello, world!")
# Asynchronous batch embedding of multiple texts
embeddings = await vectorizer.aembed_many(
["Hello, world!", "How are you?"],
batch_size=2
)
初始化 Azure OpenAI 向量化器。
- 参数
- model (str) – 用于嵌入的部署。必须是“部署名称”,而不是“模型名称”。默认为 ‘text-embedding-ada-002’。
- api_config (Optional [ Dict ] , optional) – 包含 API 密钥、API 版本、Azure 终结点和任何其他 API 选项的字典。默认为 None。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ImportError – 如果未安装 openai 库。
- ValueError – 如果未提供 Azure OpenAI API 密钥、版本或终结点。
- ValueError – 如果提供了无效的 dtype。
aembed(text, preprocess=None, as_buffer=False, **kwargs)
使用 OpenAI API 异步嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
aembed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 Azure OpenAI API 异步嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 Azure OpenAI API 嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 Azure OpenAI API 嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
VertexAITextVectorizer
class VertexAITextVectorizer(model='textembedding-gecko', api_config=None, dtype='float32', *, dims=None)
基类: BaseVectorizer
VertexAITextVectorizer 使用 Google 的 Vertex AI Palm 2 嵌入模型 API 创建文本嵌入。
此向量化器专为需要与 Google Cloud Platform (GCP) 服务集成的环境而设计。
使用此向量化器需要一个活动的 GCP 项目和位置(区域),以及适当的应用凭据。这些可以通过 api_config 字典提供,或设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量。此外,必须使用 pip install google-cloud-aiplatform>=1.26 安装 vertexai python 客户端。
# Synchronous embedding of a single text
vectorizer = VertexAITextVectorizer(
model="textembedding-gecko",
api_config={
"project_id": "your_gcp_project_id", # OR set GCP_PROJECT_ID
"location": "your_gcp_location", # OR set GCP_LOCATION
})
embedding = vectorizer.embed("Hello, world!")
# Asynchronous batch embedding of multiple texts
embeddings = await vectorizer.embed_many(
["Hello, world!", "Goodbye, world!"],
batch_size=2
)
初始化 Vertex AI 向量化器。
- 参数
- model (str) – 用于嵌入的模型。默认为 ‘textembedding-gecko’。
- api_config (Optional [ Dict ] , optional) – 包含 API 配置详细信息的字典。默认为 None。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ImportError – 如果未安装 google-cloud-aiplatform 库。
- ValueError – 如果未提供 API 密钥。
- ValueError – 如果提供了无效的 dtype。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 Vertex AI 嵌入 API 嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 Vertex AI 嵌入 API 嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果为测试传入了错误的输入类型。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
CohereTextVectorizer
class CohereTextVectorizer(model='embed-english-v3.0', api_config=None, dtype='float32', *, dims=None)
基类: BaseVectorizer
CohereTextVectorizer 类利用 Cohere 的 API 为文本数据生成嵌入。
此向量化器旨在与 Cohere 的 /embed API 交互,需要 API 密钥进行身份验证。密钥可以直接在 api_config 字典中提供,或通过 COHERE_API_KEY 环境变量提供。用户必须从 Cohere 网站 (https://dashboard.cohere.com/) 获取 API 密钥。此外,必须使用 pip install cohere 安装 cohere python 客户端。
此向量化器仅支持同步操作,允许批量处理文本并灵活处理预处理任务。
from redisvl.utils.vectorize import CohereTextVectorizer
vectorizer = CohereTextVectorizer(
model="embed-english-v3.0",
api_config={"api_key": "your-cohere-api-key"} # OR set COHERE_API_KEY in your env
)
query_embedding = vectorizer.embed(
text="your input query text here",
input_type="search_query"
)
doc_embeddings = cohere.embed_many(
texts=["your document text", "more document text"],
input_type="search_document"
)
初始化 Cohere 向量化器。
访问 https://cohere.ai/embed 了解嵌入。
- 参数
- model (str) – 用于嵌入的模型。默认为 ‘embed-english-v3.0’。
- api_config (Optional [ Dict ] , optional) – 包含 API 密钥的字典。默认为 None。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。‘float32’ 将使用 Cohere 的 float 嵌入,‘int8’ 和 ‘uint8’ 将映射到 Cohere 相应的嵌入类型。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ImportError – 如果未安装 cohere 库。
- ValueError – 如果未提供 API 密钥。
- ValueError – 如果提供了无效的 dtype。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 Cohere 嵌入 API 嵌入一段文本。
必须将 embedding input_type 作为 kwarg 提供给此方法,用于指定提供给模型的输入类型。
- 支持的输入类型
search_document
: 用于存储在向量数据库中进行搜索用例的嵌入。search_query
: 用于对向量数据库运行搜索查询以查找相关文档的嵌入。classification
: 用于通过文本分类器传递的嵌入clustering
: 用于通过聚类算法运行的嵌入。
在填充 Redis 数据库时,您要搜索的文档应使用 input_type=“search_document” 嵌入,并且在查询数据库时,应将 input_type = “search query”。如果要在下游将嵌入用于分类或聚类任务,应设置 input_type = “classification” 或 “clustering”。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- input_type (str) – 指定传递给模型的输入类型。嵌入模型 v3 及更高版本必需。
- 返回
- 如果 as_buffer=True: 返回一个 bytes 对象
- 如果 as_buffer=False
- 对于 dtype=”float32”: 返回一个浮点数列表
- 对于 dtype=”int8” 或 “uint8”: 返回一个整数列表
- 返回类型: Union[List[float], List[int], bytes]
- 抛出: TypeError – 如果提供了无效的 input_type。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 Cohere 嵌入 API 嵌入多个文本块。
必须将 embedding input_type 作为 kwarg 提供给此方法,用于指定提供给模型的输入类型。
- 支持的输入类型
search_document
: 用于存储在向量数据库中进行搜索用例的嵌入。search_query
: 用于对向量数据库运行搜索查询以查找相关文档的嵌入。classification
: 用于通过文本分类器传递的嵌入clustering
: 用于通过聚类算法运行的嵌入。
在填充 Redis 数据库时,您要搜索的文档应使用 input_type=“search_document” 嵌入,并且在查询数据库时,应将 input_type = “search query”。如果要在下游将嵌入用于分类或聚类任务,应设置 input_type = “classification” 或 “clustering”。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。默认为 10。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- input_type (str) – 指定传递给模型的输入类型。嵌入模型 v3 及更高版本必需。
- 返回
- 如果 as_buffer=True: 返回 bytes 对象列表
- 如果 as_buffer=False
- 对于 dtype=”float32”: 返回浮点数列表列表
- 对于 dtype=”int8” 或 “uint8”: 返回整数列表列表
- 返回类型: Union[List[List[float]], List[List[int]], List[bytes]]
- 抛出: TypeError – 如果提供了无效的 input_type。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
BedrockTextVectorizer
class BedrockTextVectorizer(model='amazon.titan-embed-text-v2:0', api_config=None, dtype='float32', *, dims=None)
基类: BaseVectorizer
AmazonBedrockTextVectorizer 类利用 Amazon Bedrock 的 API 为文本数据生成嵌入。
此向量化器旨在与 Amazon Bedrock API 交互,需要 AWS 凭据进行身份验证。凭据可以直接在 api_config 字典中提供,或通过环境变量提供
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION (默认为 us-east-1)
此向量化器支持带有批量处理和预处理功能的同步操作。
# Initialize with explicit credentials
vectorizer = AmazonBedrockTextVectorizer(
model="amazon.titan-embed-text-v2:0",
api_config={
"aws_access_key_id": "your_access_key",
"aws_secret_access_key": "your_secret_key",
"aws_region": "us-east-1"
}
)
# Initialize using environment variables
vectorizer = AmazonBedrockTextVectorizer()
# Generate embeddings
embedding = vectorizer.embed("Hello, world!")
embeddings = vectorizer.embed_many(["Hello", "World"], batch_size=2)
初始化 AWS Bedrock 向量化器。
- 参数
- model (str) – 要使用的 Bedrock 模型 ID。默认为 amazon.titan-embed-text-v2:0
- api_config (Optional [ Dict [ str , str ] ]) – AWS 凭据和配置。可包含:aws_access_key_id、aws_secret_access_key、aws_region 如果未提供,将使用环境变量。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ValueError – 如果在配置或环境中未提供凭据。
- ImportError – 如果未安装 boto3。
- ValueError – 如果提供了无效的 dtype。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 AWS Bedrock 嵌入 API 嵌入一段文本。
- 参数
- text (str) – 要嵌入的文本。
- preprocess (Optional [ Callable ]) – 可选的预处理函数。
- as_buffer (bool) – 是否作为字节缓冲区返回。
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果文本不是字符串。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用 AWS Bedrock 嵌入 API 嵌入多个文本块。
- 参数
- texts (List [ str ]) – 要嵌入的文本列表。
- preprocess (Optional [ Callable ]) – 可选的预处理函数。
- batch_size (int) – 处理的批次大小。默认为 10。
- as_buffer (bool) – 是否作为字节缓冲区返回。
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果 texts 不是字符串列表。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
CustomTextVectorizer
class CustomTextVectorizer(embed, embed_many=None, aembed=None, aembed_many=None, dtype='float32')
基类: BaseVectorizer
CustomTextVectorizer 类包装用户定义的嵌入方法,为文本数据创建嵌入。
此向量化器旨在接受提供的可调用文本向量化器,并提供类定义以实现与 RedisVL 的兼容性。此向量化器可能支持同步和异步操作,从而可以批量处理文本,但至少需要同步嵌入来满足 ‘embed()’ 方法。
# Synchronous embedding of a single text
vectorizer = CustomTextVectorizer(
embed = my_vectorizer.generate_embedding
)
embedding = vectorizer.embed("Hello, world!")
# Asynchronous batch embedding of multiple texts
embeddings = await vectorizer.aembed_many(
["Hello, world!", "How are you?"],
batch_size=2
)
初始化自定义向量化器。
- 参数
- embed (Callable) – 一个接受字符串对象并返回浮点数列表的可调用函数。
- embed_many (Optional [ Callable) – 一个接受字符串对象列表并返回包含浮点数列表的列表的可调用函数。默认为 None。
- aembed (Optional [ Callable ]) – 一个接受字符串对象并返回浮点数列表的异步可调用函数。默认为 None。
- aembed_many (Optional [ Callable ]) – 一个接受字符串对象列表并返回包含浮点数列表的列表的异步可调用函数。默认为 None。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- 抛出: ValueError – 如果嵌入验证失败。
async aembed(*args, **kwargs)
异步嵌入一段文本。
- 参数
- text – 要嵌入的文本
- preprocess – 预处理文本的可选函数
- as_buffer – 如果为 True,返回 bytes 对象而不是列表
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
async aembed_many(*args, **kwargs)
异步嵌入多个文本块。
- 参数
- texts – 要嵌入的文本列表
- preprocess – 预处理文本的可选函数
- batch_size – 每批处理的文本数量
- as_buffer – 如果为 True,将每个嵌入作为 bytes 对象返回
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用您的同步 embed 函数为一段文本生成嵌入。
- 参数
- text (str) – 要嵌入的文本。
- preprocess (Optional [ Callable ]) – 预处理文本的可选可调用对象。
- as_buffer (bool) – 如果为 True,将嵌入作为字节缓冲区返回。
- 返回: 输入文本的嵌入。
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果输入不是字符串。
embed_many(texts, preprocess=None, batch_size=10, as_buffer=False, **kwargs)
使用您的同步 embed_many 函数批量生成多段文本的嵌入。
- 参数
- texts (List [ str ]) – 要嵌入的文本列表。
- preprocess (Optional [ Callable ]) – 对每个文本的可选预处理。
- batch_size (int) – 每批文本的数量。
- as_buffer (bool) – 如果为 True,将每个嵌入转换为字节缓冲区。
- 返回: 嵌入列表,其中每个嵌入是浮点数列表或 bytes。
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出
- TypeError – 如果输入不是字符串列表。
- NotImplementedError – 如果未提供 embed_many 函数。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。
VoyageAITextVectorizer
class VoyageAITextVectorizer(model='voyage-large-2', api_config=None, dtype='float32', *, dims=None)
基类: BaseVectorizer
VoyageAITextVectorizer 类利用 Voyage AI 的 API 为文本数据生成嵌入。
此向量化器旨在与 Voyage AI 的 /embed API 交互,需要 API 密钥进行身份验证。密钥可以直接在 api_config 字典中提供,或通过 VOYAGE_API_KEY 环境变量提供。用户必须从 Voyage AI 网站 (https://dash.voyageai.com/) 获取 API 密钥。此外,必须使用 pip install voyageai 安装 voyageai python 客户端。
此向量化器支持同步和异步操作,允许批量处理文本并灵活处理预处理任务。
from redisvl.utils.vectorize import VoyageAITextVectorizer
vectorizer = VoyageAITextVectorizer(
model="voyage-large-2",
api_config={"api_key": "your-voyageai-api-key"} # OR set VOYAGE_API_KEY in your env
)
query_embedding = vectorizer.embed(
text="your input query text here",
input_type="query"
)
doc_embeddings = vectorizer.embed_many(
texts=["your document text", "more document text"],
input_type="document"
)
初始化 Voyage AI 向量化器。
访问 https://docs.voyageai.com/docs/embeddings 了解嵌入并查看可用模型。
- 参数
- model (str) – 用于嵌入的模型。默认为 “voyage-large-2”。
- api_config (Optional [ Dict ] , optional) – 包含 API 密钥的字典。默认为 None。
- dtype (str) – 在将文本嵌入为字节数组时使用的默认数据类型。在调用 embed() 和 embed_many() 中设置 as_buffer=True 时使用。默认为 ‘float32’。
- dims (int | None)
- 抛出
- ImportError – 如果未安装 voyageai 库。
- ValueError – 如果未提供 API 密钥。
aembed(text, preprocess=None, as_buffer=False, **kwargs)
使用 Voyage AI 嵌入 API 嵌入一段文本。
可以将 embedding input_type 作为 kwarg 提供给此方法,用于指定提供给模型的输入类型。对于检索/搜索用例,建议在编码查询或文档时指定此参数以提高检索质量。使用和不使用 input_type 参数生成的嵌入是兼容的。
支持的输入类型是 document
和 query
在填充 Redis 数据库时,您要搜索的文档应使用 input_type=”document” 嵌入,并且在查询数据库时,应将 input_type=”query”。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- input_type (str) – 指定传递给模型的输入类型。
- truncation (bool) – 是否截断输入文本以适应上下文长度。请查看 https://docs.voyageai.com/docs/embeddings
- 返回: 嵌入。
- 返回类型: List[float]
- 抛出: TypeError – 如果提供了无效的 input_type。
aembed_many(texts, preprocess=None, batch_size=None, as_buffer=False, **kwargs)
使用 Voyage AI 嵌入 API 嵌入多个文本块。
可以将 embedding input_type 作为 kwarg 提供给此方法,用于指定提供给模型的输入类型。对于检索/搜索用例,建议在编码查询或文档时指定此参数以提高检索质量。使用和不使用 input_type 参数生成的嵌入是兼容的。
支持的输入类型是 document
和 query
在填充 Redis 数据库时,您要搜索的文档应使用 input_type=”document” 嵌入,并且在查询数据库时,应将 input_type=”query”。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- input_type (str) – 指定传递给模型的输入类型。
- truncation (bool) – 是否截断输入文本以适应上下文长度。请查看 https://docs.voyageai.com/docs/embeddings
- 返回: 嵌入列表。
- 返回类型: List[List[float]]
- 抛出: TypeError – 如果提供了无效的 input_type。
embed(text, preprocess=None, as_buffer=False, **kwargs)
使用 Voyage AI 嵌入 API 嵌入一段文本。
可以将 embedding input_type 作为 kwarg 提供给此方法,用于指定提供给模型的输入类型。对于检索/搜索用例,建议在编码查询或文档时指定此参数以提高检索质量。使用和不使用 input_type 参数生成的嵌入是兼容的。
支持的输入类型是 document
和 query
在填充 Redis 数据库时,您要搜索的文档应使用 input_type=”document” 嵌入,并且在查询数据库时,应将 input_type=”query”。
- 参数
- text (str) – 要嵌入的文本块。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- input_type (str) – 指定传递给模型的输入类型。
- truncation (bool) – 是否截断输入文本以适应上下文长度。请查看 https://docs.voyageai.com/docs/embeddings
- 返回: 嵌入,表示为浮点数列表,或在 as_buffer=True 时表示为 bytes 对象
- 返回类型: Union[List[float], bytes]
- 抛出: TypeError – 如果提供了无效的 input_type。
embed_many(texts, preprocess=None, batch_size=None, as_buffer=False, **kwargs)
使用 Voyage AI 嵌入 API 嵌入多个文本块。
可以将 embedding input_type 作为 kwarg 提供给此方法,用于指定提供给模型的输入类型。对于检索/搜索用例,建议在编码查询或文档时指定此参数以提高检索质量。使用和不使用 input_type 参数生成的嵌入是兼容的。
支持的输入类型是 document
和 query
在填充 Redis 数据库时,您要搜索的文档应使用 input_type=”document” 嵌入,并且在查询数据库时,应将 input_type=”query”。
- 参数
- texts (List [ str ]) – 要嵌入的文本块列表。
- preprocess (Optional [ Callable ] , optional) – 在向量化之前执行的可选预处理可调用对象。默认为 None。
- batch_size (int , optional) – 创建嵌入时使用的文本批次大小。。
- as_buffer (bool , optional) – 是否将原始嵌入转换为字节字符串。默认为 False。
- input_type (str) – 指定传递给模型的输入类型。
- truncation (bool) – 是否截断输入文本以适应上下文长度。请查看 https://docs.voyageai.com/docs/embeddings
- 返回: 嵌入列表,表示为浮点数列表列表,或在 as_buffer=True 时表示为 bytes 对象列表
- 返回类型: Union[List[List[float]], List[bytes]]
- 抛出: TypeError – 如果提供了无效的 input_type。
model_post_init(context, /)
此函数旨在像 BaseModel 方法一样初始化私有属性。
它将 context 作为参数,因为这是 pydantic-core 在调用它时传递的内容。
- 参数
- self (BaseModel) – BaseModel 实例。
- context (Any) – 上下文。
- 返回类型: None
model_config: ClassVar[ConfigDict] = {}
模型的配置,应为符合 [ConfigDict][pydantic.config.ConfigDict] 的字典。