Skip to content
Cloudflare Docs
非官方翻译 - 此文档为非官方中文翻译版本,仅供参考。如有疑问请以 英文官方文档 为准。

Google AI Studio

Google AI Studio 帮助您使用 Google Gemini 模型快速构建。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-ai-studio

前提条件

在向 Google AI Studio 发出请求时,您需要:

  • 您的 AI 网关账户 ID。
  • 您的 AI 网关网关名称。
  • 一个有效的 Google AI Studio API 令牌。
  • 您要使用的 Google AI Studio 模型的名称。

URL 结构

您的新基础 URL 将使用上述数据的结构:https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-ai-studio/

然后您可以附加您要访问的端点,例如:v1/models/{model}:{generative_ai_rest_resource}

因此您的最终 URL 将组合为:https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-ai-studio/v1/models/{model}:{generative_ai_rest_resource}

示例

cURL

示例获取请求
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models/gemini-1.0-pro:generateContent" \
--header 'content-type: application/json' \
--header 'x-goog-api-key: {google_studio_api_key}' \
--data '{
"contents": [
{
"role":"user",
"parts": [
{"text":"What is Cloudflare?"}
]
}
]
}'

在 JavaScript 中使用 @google/generative-ai

如果您使用 @google/generative-ai 包,您可以这样设置您的端点:

JavaScript 示例
import { GoogleGenerativeAI } from "@google/generative-ai";
const api_token = env.GOOGLE_AI_STUDIO_TOKEN;
const account_id = "";
const gateway_name = "";
const genAI = new GoogleGenerativeAI(api_token);
const model = genAI.getGenerativeModel(
{ model: "gemini-1.5-flash" },
{
baseUrl: `https://gateway.ai.cloudflare.com/v1/${account_id}/${gateway_name}/google-ai-studio`,
},
);
await model.generateContent(["What is Cloudflare?"]);

OpenAI-Compatible Endpoint

You can also use the OpenAI-compatible endpoint (/ai-gateway/chat-completion/) to access Google AI Studio models using the OpenAI API schema. To do so, send your requests to:

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions

Specify:

{
"model": "google-ai-studio/{model}"
}