Skip to content

文本嵌入

以下是 SiliconFlow 创建嵌入请求接口的详细参数说明及示例,供你撰写文档参考:

一、接口请求体(Body)参数详解

1. input(必需)

  • 类型:字符串或字符串数组
  • 描述:要嵌入的输入文本,可以是字符串形式的文本,或者是预先处理好的 token 数组。如果需要在单次请求中嵌入多个输入,可以传递一个字符串数组或 token 数组数组。需要注意的是,输入文本不能超过模型的最大输入 token 数(除 BAAI/bge-m3 模型为 8192 token 外,其他模型均为 512 token),也不能是空字符串。
  • 示例值
  • 单个输入:"Silicon flow embedding online: fast, affordable, and high-quality embedding services. come try it out!"
  • 多个输入:["text1 to embed", "text2 to embed"]

二、使用示例

1. 基础嵌入请求示例

import requests
import json

# 设置接口URL
url = "https://api.siliconflow.cn/v1/embeddings"

# 设置请求头,包含认证信息和内容类型
headers = {
    "Authorization": "Bearer YOUR_API_KEY",  # 替换为你的API密钥
    "Content-Type": "application/json"       # 指定发送的数据格式为JSON
}

# 设置请求体
data = {
    "input": "Silicon flow embedding online: fast, affordable, and high-quality embedding services. come try it out!"
}

# 发送POST请求
response = requests.post(url, headers=headers, json=data)

# 打印返回结果
print(response.json())

2. 批量嵌入请求示例

import requests
import json

url = "https://api.siliconflow.cn/v1/embeddings"

headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

# 批量输入多个文本进行嵌入
data = {
    "input": ["text1 to embed", "text2 to embed"]
}

response = requests.post(url, headers=headers, json=data)

print(response.json())