Skip to main content

嵌入模型

快速开始

from litellm import embedding
import os
os.environ['OPENAI_API_KEY'] = ""
response = embedding(model='text-embedding-ada-002', input=["good morning from litellm"])

代理使用

注意 对于 vertex_ai

export GOOGLE_APPLICATION_CREDENTIALS="absolute/path/to/service_account.json"

将模型添加到配置中

model_list:
- model_name: textembedding-gecko
litellm_params:
model: vertex_ai/textembedding-gecko

general_settings:
master_key: sk-1234

启动代理

litellm --config /path/to/config.yaml 

# 运行在 http://0.0.0.0:4000

测试

curl --location 'http://0.0.0.0:4000/embeddings' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"input": ["Academia.edu uses"], "model": "textembedding-gecko", "encoding_format": "base64"}'

litellm.embedding() 的输入参数

info

任何非 OpenAI 参数将被视为特定于提供者的参数,并在请求体中作为 kwargs 发送到提供者。

查看保留参数

查看示例

必需字段

  • model: 字符串 - 要使用的模型 ID。model='text-embedding-ada-002'

  • input: 字符串或数组 - 要嵌入的输入文本,编码为字符串或标记数组。要在单个请求中嵌入多个输入,请传递字符串数组或标记数组数组。输入不能超过模型的最大输入标记数(text-embedding-ada-002 为 8192 个标记),不能为空字符串,并且任何数组必须为 2048 维或更少。

input=["good morning from litellm"]

可选的 LiteLLM 字段

  • user: 字符串(可选) 代表您的最终用户的唯一标识符,

  • dimensions: 整数(可选) 结果输出嵌入应具有的维度数。仅在 OpenAI/Azure 的 text-embedding-3 及更高版本模型中受支持。

  • encoding_format: 字符串(可选) 返回嵌入的格式。可以是 "float""base64"。默认为 encoding_format="float"

  • timeout: 整数(可选) - 等待 API 响应的最大时间(以秒为单位)。默认为 600 秒(10 分钟)。

  • api_base: 字符串(可选) - 要调用模型的 API 端点

  • api_version: 字符串(可选) - (特定于 Azure)调用的 API 版本

  • api_key: 字符串(可选) - 用于身份验证和授权请求的 API 密钥。如果未提供,则使用默认 API 密钥。

  • api_type: 字符串(可选) - 要使用的 API 类型。

litellm.embedding() 的输出

{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [
-0.0022326677571982145,
0.010749882087111473,
...
...
...

]
}
],
"model": "text-embedding-ada-002-v2",
"usage": {
"prompt_tokens": 10,
"total_tokens": 10
}
}

OpenAI 嵌入模型

使用方法

from litellm import embedding
import os
os.environ['OPENAI_API_KEY'] = ""
response = embedding(
model="text-embedding-3-small",
input=["good morning from litellm", "this is another item"],
metadata={"anything": "good day"},
dimensions=5 # 仅在 text-embedding-3 及更高版本模型中受支持。
)
模型名称函数调用必需的操作系统变量
text-embedding-3-smallembedding('text-embedding-3-small', input)os.environ['OPENAI_API_KEY']
text-embedding-3-largeembedding('text-embedding-3-large', input)os.environ['OPENAI_API_KEY']
text-embedding-ada-002embedding('text-embedding-ada-002', input)os.environ['OPENAI_API_KEY']

Azure OpenAI 嵌入模型

API 密钥

这可以设置为环境变量或作为 params 传递给 litellm.embedding()

import os
os.environ['AZURE_API_KEY'] =
os.environ['AZURE_API_BASE'] =
os.environ['AZURE_API_VERSION'] =

使用方法

from litellm import embedding
response = embedding(
model="azure/<your deployment name>",
input=["good morning from litellm"],
api_key=api_key,
api_base=api_base,
api_version=api_version,
)
print(response)
模型名称函数调用
text-embedding-ada-002embedding(model="azure/<your deployment name>", input=input)

感谢 Mikko 提供此集成

兼容 OpenAI 的嵌入模型

使用此方法调用兼容 OpenAI 服务器的 /embedding 端点,例如 https://github.com/xorbitsai/inference

注意:为模型添加 openai/ 前缀,以便 LiteLLM 知道路由到 OpenAI

使用方法

from litellm import embedding
response = embedding(
model = "openai/<your-llm-name>", # 添加 `openai/` 前缀以便 LiteLLM 知道路由到 OpenAI
api_base="http://0.0.0.0:4000/" # 设置自定义 OpenAI 端点的 API Base
input=["good morning from litellm"]
)

Bedrock 嵌入

API 密钥

可以设置为环境变量或作为 参数传递给 litellm.embedding()

import os
os.environ["AWS_ACCESS_KEY_ID"] = "" # 访问密钥
os.environ["AWS_SECRET_ACCESS_KEY"] = "" # 秘密访问密钥
os.environ["AWS_REGION_NAME"] = "" # us-east-1, us-east-2, us-west-1, us-west-2

使用方法

from litellm import embedding
response = embedding(
model="amazon.titan-embed-text-v1",
input=["good morning from litellm"],
)
print(response)
模型名称函数调用
Titan Embeddings - G1embedding(model="amazon.titan-embed-text-v1", input=input)
Cohere Embeddings - Englishembedding(model="cohere.embed-english-v3", input=input)
Cohere Embeddings - Multilingualembedding(model="cohere.embed-multilingual-v3", input=input)

Cohere 嵌入模型

https://docs.cohere.com/reference/embed

使用方法

from litellm import embedding
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere 调用
response = embedding(
model="embed-english-v3.0",
input=["good morning from litellm", "this is another item"],
input_type="search_document" # v3 llms 的可选参数
)
模型名称函数调用
embed-english-v3.0embedding(model="embed-english-v3.0", input=["good morning from litellm", "this is another item"])
embed-english-light-v3.0embedding(model="embed-english-light-v3.0", input=["good morning from litellm", "this is another item"])
embed-multilingual-v3.0embedding(model="embed-multilingual-v3.0", input=["good morning from litellm", "this is another item"])
embed-multilingual-light-v3.0embedding(model="embed-multilingual-light-v3.0", input=["good morning from litellm", "this is another item"])
embed-english-v2.0embedding(model="embed-english-v2.0", input=["good morning from litellm", "this is another item"])
embed-english-light-v2.0embedding(model="embed-english-light-v2.0", input=["good morning from litellm", "this is another item"])
embed-multilingual-v2.0embedding(model="embed-multilingual-v2.0", input=["good morning from litellm", "this is another item"])

HuggingFace 嵌入模型

LiteLLM 支持所有特征提取 + 句子相似性嵌入模型:https://huggingface.co/models?pipeline_tag=feature-extraction

使用方法

from litellm import embedding
import os
os.environ['HUGGINGFACE_API_KEY'] = ""
response = embedding(
model='huggingface/microsoft/codebert-base',
input=["good morning from litellm"]
)

使用方法 - 设置 input_type

LiteLLM 通过向 API Base 发起 GET 请求来推断输入类型(特征提取或句子相似性)。

通过设置 input_type 来覆盖此行为。

from litellm import embedding
import os
os.environ['HUGGINGFACE_API_KEY'] = ""
response = embedding(
model='huggingface/microsoft/codebert-base',
input=["good morning from litellm", "you are a good bot"],
api_base = "https://p69xlsj6rpno5drq.us-east-1.aws.endpoints.huggingface.cloud",
input_type="sentence-similarity"
)

使用方法 - 自定义 API Base

from litellm import embedding
import os
os.environ['HUGGINGFACE_API_KEY'] = ""
response = embedding(
model='huggingface/microsoft/codebert-base',
input=["good morning from litellm"],
api_base = "https://p69xlsj6rpno5drq.us-east-1.aws.endpoints.huggingface.cloud"
)
模型名称函数调用必需的环境变量
microsoft/codebert-baseembedding('huggingface/microsoft/codebert-base', input=input)os.environ['HUGGINGFACE_API_KEY']
BAAI/bge-large-zhembedding('huggingface/BAAI/bge-large-zh', input=input)os.environ['HUGGINGFACE_API_KEY']
any-hf-embedding-modelembedding('huggingface/hf-embedding-model', input=input)os.environ['HUGGINGFACE_API_KEY']

Mistral AI 嵌入模型

此处列出的所有模型均受支持:https://docs.mistral.ai/platform/endpoints

使用方法

from litellm import embedding
import os

os.environ['MISTRAL_API_KEY'] = ""
response = embedding(
model="mistral/mistral-embed",
input=["good morning from litellm"],
)
print(response)
模型名称函数调用
mistral-embedembedding(model="mistral/mistral-embed", input)

Vertex AI 嵌入模型

使用方法 - 嵌入

import litellm
from litellm import embedding
litellm.vertex_project = "hardy-device-38811" # 您的项目ID
litellm.vertex_location = "us-central1" # 项目位置

response = embedding(
model="vertex_ai/textembedding-gecko",
input=["good morning from litellm"],
)
print(response)

支持的模型

所有在此列出的模型均受支持:此处

模型名称函数调用
textembedding-geckoembedding(model="vertex_ai/textembedding-gecko", input)
textembedding-gecko-multilingualembedding(model="vertex_ai/textembedding-gecko-multilingual", input)
textembedding-gecko-multilingual@001embedding(model="vertex_ai/textembedding-gecko-multilingual@001", input)
textembedding-gecko@001embedding(model="vertex_ai/textembedding-gecko@001", input)
textembedding-gecko@003embedding(model="vertex_ai/textembedding-gecko@003", input)
text-embedding-preview-0409embedding(model="vertex_ai/text-embedding-preview-0409", input)
text-multilingual-embedding-preview-0409embedding(model="vertex_ai/text-multilingual-embedding-preview-0409", input)

Voyage AI 嵌入模型

使用方法 - 嵌入

from litellm import embedding
import os

os.environ['VOYAGE_API_KEY'] = ""
response = embedding(
model="voyage/voyage-01",
input=["good morning from litellm"],
)
print(response)

支持的模型

此处列出的所有模型均受支持:https://docs.voyageai.com/embeddings/#models-and-specifics

模型名称函数调用
voyage-01embedding(model="voyage/voyage-01", input)
voyage-lite-01embedding(model="voyage/voyage-lite-01", input)
voyage-lite-01-instructembedding(model="voyage/voyage-lite-01-instruct", input)

特定供应商的参数

info

任何非 OpenAI 的参数将被视为特定供应商的参数,并作为 kwargs 发送到请求体中。

查看保留参数

示例

Cohere v3 模型有一个必需参数:input_type,它可以是以下四个值之一:

  • input_type="search_document": (默认)用于您想要存储在向量数据库中的文本(文档)
  • input_type="search_query": 用于搜索查询以在向量数据库中查找最相关的文档
  • input_type="classification": 如果您将嵌入用作分类系统的输入,请使用此参数
  • input_type="clustering": 如果您将嵌入用于文本聚类,请使用此参数 https://txt.cohere.com/introducing-embed-v3/
from litellm import embedding
os.environ["COHERE_API_KEY"] = "cohere key"

# cohere call
response = embedding(
model="embed-english-v3.0",
input=["good morning from litellm", "this is another item"],
input_type="search_document" # 👈 PROVIDER-SPECIFIC PARAM
)
优云智算