Gemini 文件读取接口

POST https://api.timrouter.com/v1/chat/completions 在线测试 →
Authorization

请在 Header 中加入参数 Authorization,取值为 Bearer 之后拼接令牌

范例: Authorization: Bearer ********************

给定一个提示,该模型将返回一个或多个预测的完成,并且也还能够返回每个位置的替代词元的概率。 为带来的提示和配置项新建完成 官方说明文档:https://ai.google.dev/gemini-api/docs/document-processing?hl=zh-cn

请求参数项

请求头参数
Content-Type string
必填
范例: application/json
Accept string
必填
范例: application/json
Authorization string
非必填
范例: Bearer {{YOUR_API_KEY}}
请求体参数 application/json
model string
必填
要使用的模型的 ID。有关哪些模型可与聊天 API 一起使用的详细信息,请参阅模型接口适配性表。
messages array [object]
必填
至今为止对话所包括的消息清单。Python 代码范例。
role string
非必填
content string
非必填
temperature integer
非必填
使用什么采样温度值,介于 0 和 2 之间。较高的值(如 0.8)将使产出更加随机,而较低的值(如 0.2)将使产出更加集中和确定。 我们一般推荐改变这个或top_p但不是两者。
top_p integer
非必填
一种替代温度采样的方法,称为核心采样,其中模型考虑具有 top_p 概率质量的词元的结果。因此 0.1 意味着只考虑构成前 10% 概率质量的词元。 我们一般推荐改变这个或temperature但不是两者。
n integer
非必填
默认取值为 1 为每个传入消息产出多少个对话补全选取。
stream boolean
非必填
默认取值为 false 若配置,则像在 ChatGPT 中一样会发送部分消息增量。词元将以仅数据的服务器发送事件的形式发送,这些事件在可用时,并在 data: [DONE] 消息终止流。Python 代码范例。
stop string
非必填
默认取值为 null 最多 4 个序列,API 将停止进一步产出词元。
max_tokens integer
非必填
默认取值为 inf 在对话补全中产出的最大词元数。 传入词元和产出词元的总长度受模型的上下文长度限制。计算词元的 Python 代码范例。
presence_penalty number
非必填
-2.0 和 2.0 之间的数字。正值会根据到当前为止是否出现在文本中来惩罚新词元,从而增加模型谈论新主题的可能性。 查阅有关频率和存在惩罚的更多信息。
frequency_penalty number
非必填
默认取值为 0 -2.0 到 2.0 之间的数字。正值根据文本当前的存在频率惩罚新词元,降低模型重复相同行的可能性。 有关频率和存在惩罚的更多信息。
logit_bias null
非必填
修改指定词元出现在续写中的可能性。 接受一个 JSON 对象,该对象将词元(由词元器指定的词元 ID)映射到相关的偏差值(-100 到 100)。从数学上讲,偏差在对模型进行采样之前添加到模型产出的 logit 中。确切效果因模型而异,但-1 和 1 之间的值应减少或增加相关词元的选取可能性;如-100 或 100 这样的值应导致相关词元的禁用或独占选取。
user string
非必填
代表您的最终用户的唯一标识符,能够帮助 OpenAI 监控和检测滥用行为。了解更多。
response_format object
非必填
指定模型必须产出的格式的对象。 将 { "type": "json_object" } 启用 JSON 模式,这能够确保模型产出的消息是有效的 JSON。 重要提示:使用 JSON 模式时,还必须通过系统或用户消息指示模型产出 JSON。若不这样做,模型可能会产出无休止的空白流,直到产出达到令牌限制,从而导致延迟增加和请求“卡住”的外观。另请留意,若 finish_reason="length",则消息内容可能会被部分切断,这表示产出超过了 max_tokens 或对话超过了最大上下文长度。 显示属性
seen integer
非必填
此功能处于测试阶段。若指定,我们的系统将尽最大努力确定性地进行采样,以便使用相同的种子和配置项进行重复请求应返回相同的结果。不能保证确定性,您应该参考 system_fingerprint 返回参数来监控后端的更改。
tools array[string]
必填
模型能够请求的一组工具列表。当前,只兼容作为工具的函数。使用此功能来带来模型能够为之产出 JSON 传入的函数列表。
tool_choice object
必填
控制模型请求哪个函数(若有的话)。none 表示模型不会请求函数,而是产出消息。auto 表示模型能够在产出消息和请求函数之间进行选取。通过 {"type": "function", "function": {"name": "my_function"}} 强制模型请求该函数。 若没有函数存在,默认取值为 none。若有函数存在,默认取值为 auto。 显示可能的类型 { "messages": [ { "role": "user", "content": [ {"type": "text", "text": "这张图片里有什么?请详细说明。"}, { "type": "image_url", "image_url": { "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png" } } ] } ], "model": "gemini-2.5-flash-all", "temperature": 0.5, "top_p": 1.0, "stream": true } 请求
范例
{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}

请求代码示例

curl --location --request POST 'https://api.timrouter.com/v1/chat/completions' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}'
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({
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
});

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

fetch("https://api.timrouter.com/v1/chat/completions", 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.timrouter.com/v1/chat/completions");
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 = "{
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": [
                {
                    \"type\": \"text\",
                    \"text\": \"这张图片里有什么?请详细描述。\"
                },
                {
                    \"type\": \"image_url\",
                    \"image_url\": {
                        \"url\": \"https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png\"
                    }
                }
            ]
        }
    ],
    \"model\": \"gemini-2.5-flash-all\",
    \"temperature\": 0.5,
    \"top_p\": 1,
    \"stream\": true
}";
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.timrouter.com/v1/chat/completions"
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 = "{
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": [
                {
                    \"type\": \"text\",
                    \"text\": \"这张图片里有什么?请详细描述。\"
                },
                {
                    \"type\": \"image_url\",
                    \"image_url\": {
                        \"url\": \"https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png\"
                    }
                }
            ]
        }
    ],
    \"model\": \"gemini-2.5-flash-all\",
    \"temperature\": 0.5,
    \"top_p\": 1,
    \"stream\": true
}"
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(`{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}`)
    req, _ := http.NewRequest("POST", "https://api.timrouter.com/v1/chat/completions", 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.timrouter.com/v1/chat/completions',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => '{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}',
  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.timrouter.com")
payload = json.dumps({
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
})
headers = {
   'Accept': 'application/json',
   'Authorization': 'Bearer YOUR_API_KEY',
   'Content-Type': 'application/json',
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
POST https://api.timrouter.com/v1/chat/completions HTTP/1.1
Accept: application/json
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.timrouter.com/v1/chat/completions");

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, "{
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": [
                {
                    \"type\": \"text\",
                    \"text\": \"这张图片里有什么?请详细描述。\"
                },
                {
                    \"type\": \"image_url\",
                    \"image_url\": {
                        \"url\": \"https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png\"
                    }
                }
            ]
        }
    ],
    \"model\": \"gemini-2.5-flash-all\",
    \"temperature\": 0.5,
    \"top_p\": 1,
    \"stream\": true
}");
CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://api.timrouter.com/v1/chat/completions");
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", @"{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
#import <Foundation/Foundation.h>

NSURL *url = [NSURL URLWithString:@"https://api.timrouter.com/v1/chat/completions"];
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:[@"{
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": [
                {
                    \"type\": \"text\",
                    \"text\": \"这张图片里有什么?请详细描述。\"
                },
                {
                    \"type\": \"image_url\",
                    \"image_url\": {
                        \"url\": \"https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png\"
                    }
                }
            ]
        }
    ],
    \"model\": \"gemini-2.5-flash-all\",
    \"temperature\": 0.5,
    \"top_p\": 1,
    \"stream\": true
}" 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.timrouter.com/v1/chat/completions")
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 = '{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}'
response = http.request(request)
puts response.read_body
(* Requires cohttp and lwt *)

let url = "https://api.timrouter.com/v1/chat/completions" 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 '{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}' 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({
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
});
var response = await http.post(Uri.parse("https://api.timrouter.com/v1/chat/completions"), headers: headers, body: body);
print(response.body);
library(httr)

url <- "https://api.timrouter.com/v1/chat/completions"
body <- '{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片里有什么?请详细描述。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
                    }
                }
            ]
        }
    ],
    "model": "gemini-2.5-flash-all",
    "temperature": 0.5,
    "top_p": 1,
    "stream": true
}'
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
id string
必填
object string
必填
created integer
必填
choices array [object]
必填
index integer
非必填
message object
非必填
finish_reason string
非必填
usage object
必填
prompt_tokens integer
必填
completion_tokens integer
必填
total_tokens integer
必填
{ "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "choices": [ { "index": 0, "message": { "role": "assistant", "content": "\n\nHello there, how may I assist you today?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } }
范例
{
    "id": "chatcmpl-123",
    "object": "chat.completion",
    "created": 1677652288,
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "\n\nHello there, how may I assist you today?"
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 9,
        "completion_tokens": 12,
        "total_tokens": 21
    }
}