Tạo nội dung
Sử dụng định dạng API gốc của Gemini thông qua CometAPI để tạo văn bản, đầu vào video, bản tóm tắt quá trình suy luận, grounding bằng Google Search, đầu ra JSON và Streaming.
gemini-3.8-flash.
x-goog-api-key và Authorization: Bearer đều được hỗ trợ để xác thực.Bắt đầu nhanh
Để sử dụng Google Gen AI SDK hoặc ứng dụng HTTP client với CometAPI, hãy cấu hình URL cơ sở và API key:generateContent khi thay đổi URL cơ sở.
Gửi đầu vào video
Gửi video dưới dạng một phần nội dung. Chọn cấu trúc đầu vào dựa trên nơi lưu trữ video:inlineData.mimeType và fileData.fileUri.<base64-encoded-mp4> bằng nội dung base64 của video:
Cấu hình quá trình suy luận (suy luận)
Sử dụngthinkingConfig.thinkingLevel để định hướng độ sâu suy luận. Các ví dụ dưới đây sử dụng LOW và MEDIUM.
- Mức độ suy luận
- Tóm tắt quá trình suy luận
LOW:thinkingBudget là một chế độ điều khiển bằng số dành cho các mô hình tương thích, bao gồm Gemini 2.5. Hãy sử dụng thinkingLevel trong các ví dụ về Gemini 3 và không gửi đồng thời cả hai chế độ điều khiển. Xem hướng dẫn về quá trình suy luận của Google hướng dẫn về quá trình suy luận để biết các giá trị dành riêng cho từng mô hình.Truyền trực tuyến phản hồi
Sử dụngstreamGenerateContent?alt=sse để nhận Server-Sent Events. Mỗi dòng data: chứa một đối tượng JSON GenerateContentResponse:
Đặt chỉ dẫn hệ thống
Sử dụngsystemInstruction để định hướng phản hồi. Ví dụ này yêu cầu một phương trình không kèm văn bản bổ sung:
Yêu cầu đầu ra JSON
ĐặtresponseMimeType thành application/json và cung cấp một responseSchema. Ví dụ này yêu cầu một mảng các hành tinh có tên và khoảng cách dạng số:
Ground bằng Google Search
Thêm một công cụgoogleSearch để yêu cầu grounding bằng tìm kiếm. Ví dụ này yêu cầu kết quả trận chung kết UEFA EURO 2024:
groundingMetadata của candidate để biết các truy vấn tìm kiếm, URL nguồn và các liên kết giữa nguồn với văn bản phản hồi.
Bảo toàn nội dung cuộc trò chuyện
Đối với các cuộc hội thoại nhiều lượt, hãy gửi nội dunguser và model trước đó trong contents. Các ví dụ chat của SDK sẽ duy trì lịch sử này cho bạn.
Đối với function calling, hãy trả về một functionResponse cho mỗi functionCall, với name tương ứng và mọi id được trả về. Truyền lại nguyên vẹn nội dung model trước đó, bao gồm các trường thoughtSignature. Chữ ký là opaque; không tái tạo lại từ văn bản hiển thị.
Ví dụ phản hồi
Một phản hồi văn bản bao gồm nội dung được tạo và mức sử dụng Token. Ví dụ rút gọn này bỏ qua các trường tùy chọn:thoughtsTokenCount báo cáo các Token suy luận nội bộ, ngay cả khi phản hồi không có bản tóm tắt suy luận. Hãy kiểm tra từng phần nội dung; một phản hồi có thể chứa văn bản, bản tóm tắt hoặc lệnh gọi hàm.So sánh các định dạng yêu cầu
Chọn endpoint gốc cho các trường yêu cầu và phản hồi của Gemini. Xem Chat Completions để biết định dạng tương thích OpenAI.Ủy quyền
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
Gemini model ID. These examples use gemini-3.8-flash. See the Models page for available model IDs.
Operation to perform. Use generateContent for a JSON response. For Server-Sent Events, select streamGenerateContent and set the separate alt query parameter to sse.
generateContent, streamGenerateContent Tham số truy vấn
Set to sse when the operator is streamGenerateContent. Omit this parameter for generateContent.
sse Nội dung
Conversation content. Each entry has an optional role (user or model) and a parts array. For tool results, preserve the complete preceding model content, including any thoughtSignature fields.
System instructions that guide the model's behavior across the entire conversation. Text only.
Tools available to the model during generation. Use googleSearch for search grounding.
Configuration for tool usage, such as function calling mode.
Safety filter settings. Override default thresholds for specific harm categories.
Configuration for model generation behavior including temperature, output length, and response format.
The name of cached content to use as context. Format: cachedContents/{id}. See the Gemini context caching documentation for details.
Phản hồi
Successful response. For streaming requests, the response is a stream of SSE events, each containing a GenerateContentResponse JSON object prefixed with data: .
The generated response candidates.
Feedback on the prompt, including safety blocking information.
Token usage statistics for the request.
The model version that generated this response.
The timestamp when this response was created (ISO 8601 format).
Unique identifier for this response.