Gemini 文本向量(embedContent)
curl --request POST \
--url https://api.leapx-hub.com/v1/models/{model}:embedContent \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"content": {},
"outputDimensionality": 123,
"taskType": "<string>"
}
'import requests
url = "https://api.leapx-hub.com/v1/models/{model}:embedContent"
payload = {
"content": {},
"outputDimensionality": 123,
"taskType": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({content: {}, outputDimensionality: 123, taskType: '<string>'})
};
fetch('https://api.leapx-hub.com/v1/models/{model}:embedContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.leapx-hub.com/v1/models/{model}:embedContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'content' => [
],
'outputDimensionality' => 123,
'taskType' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.leapx-hub.com/v1/models/{model}:embedContent"
payload := strings.NewReader("{\n \"content\": {},\n \"outputDimensionality\": 123,\n \"taskType\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.leapx-hub.com/v1/models/{model}:embedContent")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"content\": {},\n \"outputDimensionality\": 123,\n \"taskType\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.leapx-hub.com/v1/models/{model}:embedContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"content\": {},\n \"outputDimensionality\": 123,\n \"taskType\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"embedding": {
"values": [0.0023064255, -0.009327292, 0.015797347, ...]
},
"metadata": {
"usage": {
"prompt_tokens": 6,
"total_tokens": 6
}
}
}
文本向量系列
Gemini 文本向量(embedContent)
POST
/
v1
/
models
/
{model}
:embedContent
Gemini 文本向量(embedContent)
curl --request POST \
--url https://api.leapx-hub.com/v1/models/{model}:embedContent \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"content": {},
"outputDimensionality": 123,
"taskType": "<string>"
}
'import requests
url = "https://api.leapx-hub.com/v1/models/{model}:embedContent"
payload = {
"content": {},
"outputDimensionality": 123,
"taskType": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({content: {}, outputDimensionality: 123, taskType: '<string>'})
};
fetch('https://api.leapx-hub.com/v1/models/{model}:embedContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.leapx-hub.com/v1/models/{model}:embedContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'content' => [
],
'outputDimensionality' => 123,
'taskType' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.leapx-hub.com/v1/models/{model}:embedContent"
payload := strings.NewReader("{\n \"content\": {},\n \"outputDimensionality\": 123,\n \"taskType\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.leapx-hub.com/v1/models/{model}:embedContent")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"content\": {},\n \"outputDimensionality\": 123,\n \"taskType\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.leapx-hub.com/v1/models/{model}:embedContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"content\": {},\n \"outputDimensionality\": 123,\n \"taskType\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"embedding": {
"values": [0.0023064255, -0.009327292, 0.015797347, ...]
},
"metadata": {
"usage": {
"prompt_tokens": 6,
"total_tokens": 6
}
}
}
简介
使用 Gemini 原生接口将文本转换为向量嵌入。模型由 URL 路径 指定(如gemini-embedding-001),适用于需要 Google 嵌入模型或与 Gemini API 对齐的场景。
与 文本向量化(Embedding) 的 OpenAI 格式互为补充:本文档为 Gemini 原生路径;同一能力也可通过
POST /v1/embeddings 调用。认证
string
必填
Bearer Token,如
Bearer sk-xxxxxxxxxx路径参数
string
必填
嵌入模型名称,如
gemini-embedding-001。请求参数
object
必填
待嵌入内容。须包含
parts 数组,每项为 { "text": "文本内容" }。integer
输出向量维度(仅部分模型支持,如
gemini-embedding-001、text-embedding-004)。string
任务类型,如
RETRIEVAL_DOCUMENT、RETRIEVAL_QUERY(可选)。cURL 示例
curl -X POST "https://api.leapx-hub.com/v1/models/gemini-embedding-001:embedContent" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-XyLy**************************mIqSt" \
-d '{
"content": {
"parts": [
{ "text": "要嵌入的文本内容" }
]
}
}'
curl -X POST "https://api.leapx-hub.com/v1/models/gemini-embedding-001:embedContent" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-XyLy**************************mIqSt" \
-d '{
"content": {
"parts": [
{ "text": "要嵌入的文本内容" }
]
},
"outputDimensionality": 768
}'
Python 示例
import requests
url = "https://api.leapx-hub.com/v1/models/gemini-embedding-001:embedContent"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-XyLy**************************mIqSt"
}
payload = {
"content": {
"parts": [
{ "text": "要嵌入的文本内容" }
]
}
}
response = requests.post(url, json=payload, headers=headers)
data = response.json()
embedding = data["embedding"]["values"]
print(f"向量维度:{len(embedding)}")
{
"embedding": {
"values": [0.0023064255, -0.009327292, 0.015797347, ...]
},
"metadata": {
"usage": {
"prompt_tokens": 6,
"total_tokens": 6
}
}
}
批量接口(batchEmbedContents)
批量嵌入请使用:POST /v1/models/{model}:batchEmbedContents,请求体为 requests 数组,每项结构同单条(含 content.parts),且不要在每项中带 model 字段。
curl -X POST "https://api.leapx-hub.com/v1/models/gemini-embedding-001:batchEmbedContents" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-XyLy**************************mIqSt" \
-d '{
"requests": [
{ "content": { "parts": [{ "text": "第一段文本" }] } },
{ "content": { "parts": [{ "text": "第二段文本" }] } }
]
}'
支持的模型
| 模型 | 说明 |
|---|---|
| gemini-embedding-001 | 通用嵌入模型,支持 outputDimensionality |
| text-embedding-004 | 高精度嵌入模型 |
注意事项
- 模型由 URL 路径指定,请求体不要包含
model字段 content.parts必填,至少一个text非空- 用量信息在响应的
metadata.usage中(prompt_tokens、total_tokens)
⌘I
