Skip to main content
POST
CometAPI hỗ trợ định dạng API gốc của Gemini, mang đến cho bạn toàn quyền truy cập vào các tính năng dành riêng cho Gemini như kiểm soát suy nghĩ, Google Search grounding, các modality tạo ảnh gốc và hơn thế nữa. Hãy dùng endpoint này khi bạn cần các khả năng không có sẵn thông qua endpoint chat tương thích OpenAI.
Hãy sử dụng tài liệu tham chiếu GenerateContent API chính thức của Google làm nguồn chuẩn cho các trường request đầy đủ, schema response và hành vi riêng theo model của Gemini. Trang CometAPI này giải thích cách gửi đúng cấu trúc request gốc đó thông qua CometAPI.
Các tham số request và trường response của Gemini có thể thay đổi khi Google cập nhật API gốc. Hãy kiểm tra tài liệu tạo văn bản của Gemini để có danh sách tham số đầy đủ mới nhất và hành vi riêng theo nhà cung cấp.
Cả header x-goog-api-keyAuthorization: Bearer đều được hỗ trợ để xác thực.

Bắt đầu nhanh

Để sử dụng bất kỳ Gemini SDK hoặc HTTP client nào với CometAPI, hãy thay thế base URL và API key:

Gửi đầu vào video

Gemini generateContent chấp nhận video như một phần nội dung. Hãy chọn dạng đầu vào dựa trên nơi video được lưu trữ:
Đối với các yêu cầu REST và curl, hãy sử dụng tên trường camelCase của Gemini như inlineData.mimeTypefileData.fileUri. Không gửi media URL dưới dạng file_data.file_uri.
Ví dụ này đọc một tệp MP4 cục bộ, mã hóa nó thành base64 và gửi nó trong phần thân yêu cầu:
Ví dụ này gửi một URL MP4 công khai bằng fileData.fileUri:
CometAPI không khuyến nghị một quy trình tải lên Gemini Files API riêng cho endpoint này. Hãy gửi media trực tiếp trong yêu cầu generateContent bằng inlineData hoặc fileData.fileUri.

Cấu hình thinking (reasoning)

Các model Gemini có thể thực hiện suy luận nội bộ trước khi tạo phản hồi. Cách điều khiển phụ thuộc vào thế hệ model.
Các model Gemini 3 sử dụng thinkingLevel để kiểm soát độ sâu suy luận. Các mức khả dụng: MINIMAL, LOW, MEDIUM, HIGH.Sử dụng gemini-3-flash-preview làm model ví dụ mặc định trừ khi bạn thực sự cần một biến thể Gemini 3 khác.
Việc sử dụng thinkingLevel với các model Gemini 2.5 (hoặc thinkingBudget với các model Gemini 3) có thể gây ra lỗi. Hãy dùng đúng tham số cho phiên bản model của bạn.

Stream phản hồi

Để nhận Server-Sent Events khi model đang tạo nội dung, hãy sử dụng streamGenerateContent?alt=sse làm toán tử. Mỗi sự kiện SSE chứa một dòng data: với một đối tượng JSON GenerateContentResponse.

Đặt system instructions

Để định hướng hành vi của model trong toàn bộ cuộc trò chuyện, hãy sử dụng systemInstruction:

Yêu cầu đầu ra JSON

Để buộc đầu ra JSON có cấu trúc, hãy đặt responseMimeType. Bạn cũng có thể cung cấp responseSchema để xác thực schema nghiêm ngặt:

Để bật tìm kiếm web theo thời gian thực, hãy thêm công cụ googleSearch:
Phản hồi bao gồm groundingMetadata với URL nguồn và điểm độ tin cậy.

Ví dụ phản hồi

Một phản hồi điển hình từ endpoint Gemini của CometAPI:
Trường thoughtsTokenCount trong usageMetadata cho biết mô hình đã dùng bao nhiêu token cho quá trình suy luận nội bộ, ngay cả khi phần thinking output không được đưa vào phản hồi.

So sánh với endpoint tương thích OpenAI

Ủy quyền

x-goog-api-key
string
header
bắt buộc

Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer $COMETAPI_KEY) is also supported.

Tham số đường dẫn

model
string
bắt buộc

Gemini model ID. Example: gemini-3-flash-preview, gemini-2.5-pro. See the Models page for current options.

operator
enum<string>
bắt buộc

The operation to perform. Use generateContent for synchronous responses, or streamGenerateContent?alt=sse for Server-Sent Events streaming.

Tùy chọn có sẵn:
generateContent,
streamGenerateContent?alt=sse

Nội dung

application/json
contents
object[]

Conversation content. Each entry has an optional role (user or model) and a parts array.

systemInstruction
object

System instructions that guide the model's behavior across the entire conversation. Text only.

tools
object[]

Tools the model may use to generate responses. Supports function declarations, Google Search, Google Maps, and code execution.

toolConfig
object

Configuration for tool usage, such as function calling mode.

safetySettings
object[]

Safety filter settings. Override default thresholds for specific harm categories.

generationConfig
object

Configuration for model generation behavior including temperature, output length, and response format.

cachedContent
string

The name of cached content to use as context. Format: cachedContents/{id}. See the Gemini context caching documentation for details.

Phản hồi

200 - application/json

Successful response. For streaming requests, the response is a stream of SSE events, each containing a GenerateContentResponse JSON object prefixed with data:.

candidates
object[]

The generated response candidates.

promptFeedback
object

Feedback on the prompt, including safety blocking information.

usageMetadata
object

Token usage statistics for the request.

modelVersion
string

The model version that generated this response.

createTime
string

The timestamp when this response was created (ISO 8601 format).

responseId
string

Unique identifier for this response.