Gemini文本聊天
代理 Gemini API 请求。 路径格式: /v1beta/models/{model_name}:{action}
例如:
/v1beta/models/gemini-2.5-pro:generateContent/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse
POST
/v1beta/models/{model}:generateContentGemini文本聊天
代理 Gemini API 请求。 路径格式: `/v1beta/models/{model_name}:{action}` 例如: - `/v1beta/models/gemini-2.5-pro:generateContent` - `/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse`
鉴权: 使用 Bearer Token 认证。 格式: `Authorization: Bearer sk-xxxxxx`
请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 位置: path |
请求体
Content-Type: application/json
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
contents | array<object> | ||
[] | object | ||
role | string | 可选值: user, model | |
parts | array<object> | ||
[] | object | ||
text | string | ||
inlineData | object | ||
mimeType | string | ||
data | string | ||
generationConfig | object | ||
temperature | number | ||
topP | number | ||
topK | integer | ||
maxOutputTokens | integer | ||
stopSequences | array<string> | ||
[] | string | ||
safetySettings | array<object> | ||
[] | object | ||
category | string | ||
threshold | string | ||
tools | array<object> | ||
[] | object | ||
systemInstruction | object | ||
parts | array<object> | ||
[] | object |
示例代码
curl -X POST "https://api.nodeprice.com/v1beta/models/string:generateContent" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "string",
"inlineData": {
"mimeType": "string",
"data": "string"
}
}
]
}
],
"generationConfig": {
"temperature": 0,
"topP": 0,
"topK": 0,
"maxOutputTokens": 0,
"stopSequences": [
"string"
]
},
"safetySettings": [
{
"category": "string",
"threshold": "string"
}
],
"tools": [
{}
],
"systemInstruction": {
"parts": [
{}
]
}
}'const url = "https://api.nodeprice.com/v1beta/models/string:generateContent";
const body = {
"contents": [
{
"role": "user",
"parts": [
{
"text": "string",
"inlineData": {
"mimeType": "string",
"data": "string"
}
}
]
}
],
"generationConfig": {
"temperature": 0,
"topP": 0,
"topK": 0,
"maxOutputTokens": 0,
"stopSequences": [
"string"
]
},
"safetySettings": [
{
"category": "string",
"threshold": "string"
}
],
"tools": [
{}
],
"systemInstruction": {
"parts": [
{}
]
}
};
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer <token>"
},
body: JSON.stringify(body)
});
console.log(await response.text());package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.nodeprice.com/v1beta/models/string:generateContent"
body := strings.NewReader(`{
"contents": [
{
"role": "user",
"parts": [
{
"text": "string",
"inlineData": {
"mimeType": "string",
"data": "string"
}
}
]
}
],
"generationConfig": {
"temperature": 0,
"topP": 0,
"topK": 0,
"maxOutputTokens": 0,
"stopSequences": [
"string"
]
},
"safetySettings": [
{
"category": "string",
"threshold": "string"
}
],
"tools": [
{}
],
"systemInstruction": {
"parts": [
{}
]
}
}`)
req, err := http.NewRequest("POST", url, body)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer <token>")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}import requests
url = "https://api.nodeprice.com/v1beta/models/string:generateContent"
body = """{
"contents": [
{
"role": "user",
"parts": [
{
"text": "string",
"inlineData": {
"mimeType": "string",
"data": "string"
}
}
]
}
],
"generationConfig": {
"temperature": 0,
"topP": 0,
"topK": 0,
"maxOutputTokens": 0,
"stopSequences": [
"string"
]
},
"safetySettings": [
{
"category": "string",
"threshold": "string"
}
],
"tools": [
{}
],
"systemInstruction": {
"parts": [
{}
]
}
}"""
response = requests.request("POST", url, data=body, headers={
"Content-Type": "application/json",
"Authorization": "Bearer <token>"
})
print(response.text)OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n"
+ " \"contents\": [\n"
+ " {\n"
+ " \"role\": \"user\",\n"
+ " \"parts\": [\n"
+ " {\n"
+ " \"text\": \"string\",\n"
+ " \"inlineData\": {\n"
+ " \"mimeType\": \"string\",\n"
+ " \"data\": \"string\"\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ],\n"
+ " \"generationConfig\": {\n"
+ " \"temperature\": 0,\n"
+ " \"topP\": 0,\n"
+ " \"topK\": 0,\n"
+ " \"maxOutputTokens\": 0,\n"
+ " \"stopSequences\": [\n"
+ " \"string\"\n"
+ " ]\n"
+ " },\n"
+ " \"safetySettings\": [\n"
+ " {\n"
+ " \"category\": \"string\",\n"
+ " \"threshold\": \"string\"\n"
+ " }\n"
+ " ],\n"
+ " \"tools\": [\n"
+ " {}\n"
+ " ],\n"
+ " \"systemInstruction\": {\n"
+ " \"parts\": [\n"
+ " {}\n"
+ " ]\n"
+ " }\n"
+ "}");
Request request = new Request.Builder()
.url("https://api.nodeprice.com/v1beta/models/string:generateContent")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer <token>")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());#include <stdio.h>
#include <curl/curl.h>
int main(void) {
CURL *curl = curl_easy_init();
if (curl == NULL) {
return 1;
}
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer <token>");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.nodeprice.com/v1beta/models/string:generateContent");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\n"
+ " \"contents\": [\n"
+ " {\n"
+ " \"role\": \"user\",\n"
+ " \"parts\": [\n"
+ " {\n"
+ " \"text\": \"string\",\n"
+ " \"inlineData\": {\n"
+ " \"mimeType\": \"string\",\n"
+ " \"data\": \"string\"\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ],\n"
+ " \"generationConfig\": {\n"
+ " \"temperature\": 0,\n"
+ " \"topP\": 0,\n"
+ " \"topK\": 0,\n"
+ " \"maxOutputTokens\": 0,\n"
+ " \"stopSequences\": [\n"
+ " \"string\"\n"
+ " ]\n"
+ " },\n"
+ " \"safetySettings\": [\n"
+ " {\n"
+ " \"category\": \"string\",\n"
+ " \"threshold\": \"string\"\n"
+ " }\n"
+ " ],\n"
+ " \"tools\": [\n"
+ " {}\n"
+ " ],\n"
+ " \"systemInstruction\": {\n"
+ " \"parts\": [\n"
+ " {}\n"
+ " ]\n"
+ " }\n"
+ "}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
CURLcode code = curl_easy_perform(curl);
if (code != CURLE_OK) {
fprintf(stderr, "%s\n", curl_easy_strerror(code));
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return 0;
}using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.nodeprice.com/v1beta/models/string:generateContent");
request.Headers.Add("Authorization", "Bearer <token>");
request.Content = new StringContent("{\n"
+ " \"contents\": [\n"
+ " {\n"
+ " \"role\": \"user\",\n"
+ " \"parts\": [\n"
+ " {\n"
+ " \"text\": \"string\",\n"
+ " \"inlineData\": {\n"
+ " \"mimeType\": \"string\",\n"
+ " \"data\": \"string\"\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ],\n"
+ " \"generationConfig\": {\n"
+ " \"temperature\": 0,\n"
+ " \"topP\": 0,\n"
+ " \"topK\": 0,\n"
+ " \"maxOutputTokens\": 0,\n"
+ " \"stopSequences\": [\n"
+ " \"string\"\n"
+ " ]\n"
+ " },\n"
+ " \"safetySettings\": [\n"
+ " {\n"
+ " \"category\": \"string\",\n"
+ " \"threshold\": \"string\"\n"
+ " }\n"
+ " ],\n"
+ " \"tools\": [\n"
+ " {}\n"
+ " ],\n"
+ " \"systemInstruction\": {\n"
+ " \"parts\": [\n"
+ " {}\n"
+ " ]\n"
+ " }\n"
+ "}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}响应
200 成功
Content-Type: application/json
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
candidates | array<object> | ||
[] | object | ||
content | object | ||
role | string | ||
parts | array<object> | ||
[] | object | ||
finishReason | string | ||
safetyRatings | array<object> | ||
[] | object | ||
usageMetadata | object | ||
promptTokenCount | integer | ||
candidatesTokenCount | integer | ||
totalTokenCount | integer |