注册

Gemini 原生文本嵌入接口 - 向量嵌入生成

POST https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent 在线调试 →
Authorization

在 Header 添加参数 Authorization,其值为 Bearer 之后拼接 Token

示例: Authorization: Bearer ********************

请求参数

Header 参数
Content-Type string
可选
示例: application/json
Body 参数 application/json
content object
可选
要嵌入的内容。 必需
parts array [object]
可选
output_dimensionality integer
可选
输出嵌入的可选降维。
taskType string
可选
嵌入将用于的可选任务类型。 { "content": { "parts": [ { "text": "What is the meaning of life?" } ] }, "output_dimensionality": 768, "taskType": "SEMANTIC_SIMILARITY" } 请求
示例
{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}

请求示例代码

{
  "model": "models/gemini-embedding-001",
  "content": {
    "parts": [
      {
        "text": "Hello world"
      }
    ]
  }
}
curl --location --request POST 'https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}'
var myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "Bearer YOUR_API_KEY");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
});

var requestOptions = {
   method: 'POST',
   headers: myHeaders,
   body: raw,
   redirect: 'follow'
};

fetch("https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent", requestOptions)
   .then(response => response.text())
   .then(result => console.log(result))
   .catch(error => console.log('error', error));
import java.io.*;
import java.net.*;
import java.util.*;

URL url = new URL("https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
String jsonInputString = "{
    \"content\": {
        \"parts\": [
            {
                \"text\": \"What is the meaning of life?\"
            }
        ]
    },
    \"output_dimensionality\": 768,
    \"taskType\": \"SEMANTIC_SIMILARITY\"
}";
try(OutputStream os = conn.getOutputStream()) {
    byte[] input = jsonInputString.getBytes("utf-8");
    os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
import Foundation

let urlString = "https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent"
guard let url = URL(string: urlString) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("Bearer YOUR_API_KEY", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let httpBody = "{
    \"content\": {
        \"parts\": [
            {
                \"text\": \"What is the meaning of life?\"
            }
        ]
    },
    \"output_dimensionality\": 768,
    \"taskType\": \"SEMANTIC_SIMILARITY\"
}"
request.httpBody = httpBody.data(using: .utf8)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let data = data {
        print(String(data: data, encoding: .utf8)!)
    }
}
task.resume()
package main

import (
    "fmt"
    "io"
    "net/http"
)

func main() {
    body := strings.NewReader(`{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}`)
    req, _ := http.NewRequest("POST", "https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent", body)
    req.Header.Set("Accept", "application/json")
    req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
    req.Header.Set("Content-Type", "application/json")
    client := &http.Client{}
    resp, _ := client.Do(req)
    defer resp.Body.Close()
    bodyBytes, _ := io.ReadAll(resp.Body)
    fmt.Println(string(bodyBytes))
}
<?php

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => '{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}',
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Authorization: Bearer YOUR_API_KEY",
    "Content-Type: application/json",
  ),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import http.client
import json

conn = http.client.HTTPSConnection("api.quickrouter.ai")
payload = json.dumps({
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
})
headers = {
   'Accept': 'application/json',
   'Authorization': 'Bearer YOUR_API_KEY',
   'Content-Type': 'application/json',
}
conn.request("POST", "/v1beta/models/gemini-embedding-001:embedContent", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
POST https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent HTTP/1.1
Accept: application/json
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer YOUR_API_KEY");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{
    \"content\": {
        \"parts\": [
            {
                \"text\": \"What is the meaning of life?\"
            }
        ]
    },
    \"output_dimensionality\": 768,
    \"taskType\": \"SEMANTIC_SIMILARITY\"
}");
CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent");
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Authorization", "Bearer YOUR_API_KEY");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", @"{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
#import <Foundation/Foundation.h>

NSURL *url = [NSURL URLWithString:@"https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Bearer YOUR_API_KEY" forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[@"{
    \"content\": {
        \"parts\": [
            {
                \"text\": \"What is the meaning of life?\"
            }
        ]
    },
    \"output_dimensionality\": 768,
    \"taskType\": \"SEMANTIC_SIMILARITY\"
}" dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
require "uri"
require "net/http"
require "json"

url = URI("https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer YOUR_API_KEY"
request["Content-Type"] = "application/json"
request.body = '{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}'
response = http.request(request)
puts response.read_body
(* Requires cohttp and lwt *)

let url = "https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent" in
let headers = Cohttp.Header.of_list [
  ("Accept", "application/json");
  ("Authorization", "Bearer YOUR_API_KEY");
  ("Content-Type", "application/json");
] in
let body = Cohttp_lwt.Body.of_string '{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}' in
Lwt_main.run (
  Cohttp_lwt_unix.Client.request ?body:(Some body) ~method_:`POST ~headers (Uri.of_string url)
  >>= fun (resp, body) ->
  Cohttp_lwt.Body.to_string body >|= fun s -> print_endline s
)
import 'package:http/http.dart' as http;
import 'dart:convert';

var headers = {
  "Accept": "application/json",
  "Authorization": "Bearer YOUR_API_KEY",
  "Content-Type": "application/json",
};
var body = json.encode({
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
});
var response = await http.post(Uri.parse("https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent"), headers: headers, body: body);
print(response.body);
library(httr)

url <- "https://api.quickrouter.ai/v1beta/models/gemini-embedding-001:embedContent"
body <- '{
    "content": {
        "parts": [
            {
                "text": "What is the meaning of life?"
            }
        ]
    },
    "output_dimensionality": 768,
    "taskType": "SEMANTIC_SIMILARITY"
}'
response <- post(url, body = body, add_headers("Accept" = "application/json", "Authorization" = "Bearer YOUR_API_KEY", "Content-Type" = "application/json"))
content(response, "text", encoding = "UTF-8")

返回响应

响应参数 🟢 200 OK · application/json
embeddings object
可选
{ "embedding": { "values": [ -0.0296398, -0.010636787, 0.012426536, -0.043735605, -0.012861362, 0.0025766853, -0.0056927837, 0.013363464, 0.042344756, -0.00600363, 0.016946154, -0.016573662, -0.0242951, 0.040273175, 0.13305295, 0.006534976, -0.0066952384, 0.011369802, 0.014375867, -0.0056447736, 0.007932834, 0.003440911, 0.0015993431, -0.012414881, -0.014631236, -0.0057829414, 0.019290049, 0.0005005244, 0.014184347, -0.0065879934, -0.003565929, 0.019397464, 0.005518749, 0.030157084, -0.009677098, -0.0029866593, 0.017299578, 0.006338976, -9.322887e-05, 0.021375261, -0.024926692, -0.0003635663, 0.004625881, 0.0027941137, 0.032529075, -0.0025162857, 0.006721118, -0.027300382, -0.0030568605, 0.020879114, -0.01518175, 0.024195358, -0.0022586272, -0.16242279, -0.0031008113, 0.011878735, -0.0065687853, -0.0078121293, -0.0031238683, -0.0036403642, -0.02131096, -0.0039914316, -0.018723665, -0.045217782, 0.006739287, -0.010009519, 0.0018316646, -0.0018083674, -0.012085223, -0.0014903563, -0.015677448, 0.00876675, -0.011975094, 0.005202495, -0.0013395232, -0.02117135, 0.02085695, 0.009407144, 0.023169326, -0.0061100135, 0.011164493, -0.0015549492, -0.027464285, -0.0123647, -9.947967e-05, -0.010167568, -0.015949652, 0.0012945095, 0.004713989, -0.009797795, -0.006914863, 0.01999175, 0.009941107, 0.022365669, -0.011112252, 0.0018841166, -0.0140149705, -0.009466361, -0.0051897927, 0.0017415871, -0.023722459, -0.013767081, 0.0009987081, 0.0025969457, 0.011067786, -0.0008395377, 0.02449344, 0.0065714344, -0.008802859, 0.01478423, -0.009419866, 0.0024454435, -0.014272332, 0.002841706, 0.0014935558, -0.1668367, 0.0071803983, 0.0002759486, -0.0005119746, 0.0138186235, 0.00035195384, 0.0021816439, 0.013897939, 0.022451976, -0.0046094316, -0.016414018, 0.029705513, 0.0013502334, -0.029205326, -0.00223862, 0.008688559, -0.0017488803, -0.0017071241, 0.02811523, 0.0003582492, 0.013387385, 0.0020523916, -0.008380343, -0.008700947, 0.01313421, 0.024904193, 0.0033890235, -0.0067567667, 0.006474785, -0.009726371, -0.013789963, -0.024971183, 0.027432237, 0.021531988, 0.00085505226, -0.007530849, 0.017413057, -0.019567914, -0.0029013238, 0.02718425, -0.048679482, -0.025425717, -0.003643478, 0.046605345, 0.015131438, 0.017564094, -0.024245195, 0.009929572, 0.010758676, 0.027922388, -0.0046666698, 0.012225224, -0.0040972205, 0.009128703, 0.0119820675, -0.0018588518, -0.000116790565, 0.008043984, 0.028858813, -0.015383414, 0.0047377315, -0.014935567, -0.0070394077, 0.026544005, 0.008124204, 0.011230377, -0.011875552, -0.018568655, -0.014651568, -0.010986834, -0.0070081796, -0.030712977, -0.016584154, 0.043207396, -0.010555375, 0.010056673, -0.004625643, -0.00096580194, -0.005962162, -0.0044048144, 0.013608296, -0.0038451874, 0.02661319, -0.009673978, 0.0073342496, -0.024697386, 0.024071904, 0.007120086, -0.004817186, 0.021704704, 0.014507516, 0.019033715, -0.021459393, 0.0009360375, 0.0034508815, 0.008515678, 0.013316997, 0.02246404, -0.0017898967, 0.015261492, -0.009148077, -0.016117442, 0.01113729, -0.0074887434, -0.0021146906, 0.0347106, -0.015456998, -0.01071589, 0.023878252, -0.0054925852, 0.0022822723, -0.008937607, -0.008547459, 0.0027762372, 0.02171912, -0.0024024074, 0.036017273, -0.0020466154, 0.004725955, 0.0016764981, 0.00070662843, 0.009837389, 0.0019250698, 0.010220786, -0.006446121, -0.0055151423, -0.02765927, 0.025683768, 0.02911067, -0.011834867, -0.0018890229, -0.0028985268, 0.020689119, -0.032568946, -0.01900007, 0.012674699, 0.02096