langchain_community.embeddings.huggingface.HuggingFaceInferenceAPIEmbeddings

class langchain_community.embeddings.huggingface.HuggingFaceInferenceAPIEmbeddings[source]

基类: BaseModel, Embeddings

使用 HuggingFace API 嵌入文本。

需要 HuggingFace Inference API 密钥和模型名称。

通过解析和验证来自关键字参数的输入数据来创建新模型。

如果输入数据无法解析以形成有效模型,则引发 ValidationError。

param additional_headers: Dict[str, str] = {}

如果需要,将其他标头传递给 requests 库。

param api_key: SecretStr [Required]

您的 HuggingFace Inference API 密钥。

约束
  • type = string

  • writeOnly = True

  • format = password

param api_url: Optional[str] = None

自定义推理端点 URL。 None 表示使用默认公共 URL。

param model_name: str = 'sentence-transformers/all-MiniLM-L6-v2'

用于文本嵌入的模型的名称。

async aembed_documents(texts: List[str]) List[List[float]]

异步嵌入搜索文档。

参数

texts (List[str]) – 要嵌入的文本列表。

返回值

嵌入列表。

返回类型

List[List[float]]

async aembed_query(text: str) List[float]

异步嵌入查询文本。

参数

text (str) – 要嵌入的文本。

返回值

嵌入。

返回类型

List[float]

embed_documents(texts: List[str]) List[List[float]][source]

获取文本列表的嵌入。

参数

texts (Documents) – 获取嵌入的文本列表。

返回值

嵌入的文本为 List[List[float]],其中每个内部 List[float]

对应于单个输入文本。

返回类型

List[List[float]]

示例

from langchain_community.embeddings import (
    HuggingFaceInferenceAPIEmbeddings,
)

hf_embeddings = HuggingFaceInferenceAPIEmbeddings(
    api_key="your_api_key",
    model_name="sentence-transformers/all-MiniLM-l6-v2"
)
texts = ["Hello, world!", "How are you?"]
hf_embeddings.embed_documents(texts)
embed_query(text: str) List[float][source]

使用 HuggingFace transformer 模型计算查询嵌入。

参数

text (str) – 要嵌入的文本。

返回值

文本的嵌入。

返回类型

List[float]

HuggingFaceInferenceAPIEmbeddings 的使用示例