Chat Completions API 充当与 OpenAI 兼容的端点,旨在让您能够更轻松地使用 Python 版和 REST 版 OpenAI 库与 Vertex AI 上的 Gemini 进行交互。如果您已经在使用 OpenAI 库,则可以使用此 API 以低成本的方式在调用 OpenAI 模型和 Vertex AI 托管模型之间切换,以比较输出、成本和可伸缩性,而无需更改现有代码。如果您尚未使用 OpenAI 库,我们建议您使用 Google Gen AI SDK。
支持的模型
Chat Completions API 同时支持 Gemini 模型和来自 Model Garden 的部分自行部署模型。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Using OpenAI libraries with Vertex AI\n\n| To see an example of using the Chat Completions API,\n| run the \"Call Gemini with the OpenAI Library\" notebook in one of the following\n| environments:\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/chat-completions/intro_chat_completions_api.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fchat-completions%2Fintro_chat_completions_api.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fchat-completions%2Fintro_chat_completions_api.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/chat-completions/intro_chat_completions_api.ipynb)\n\nThe Chat Completions API works as an Open AI-compatible endpoint, designed to\nmake it easier to interface with Gemini on Vertex AI by\nusing the OpenAI libraries for Python and REST. If you're already using the\nOpenAI libraries, you can use this API as a low-cost way to switch between\ncalling OpenAI models and Vertex AI hosted models to compare\noutput, cost, and scalability, without changing your existing code.\nIf you aren't already using the OpenAI libraries, we recommend that you\n[use the Google Gen AI SDK](/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal).\n\nSupported models\n----------------\n\nThe Chat Completions API supports both Gemini models and select\nself-deployed models from Model Garden.\n\n### Gemini models\n\nThe following models provide support for the Chat Completions API:\n\n- [Gemini 2.5 Pro](/vertex-ai/generative-ai/docs/models/gemini/2-5-pro)\n- [Gemini 2.5 Flash](/vertex-ai/generative-ai/docs/models/gemini/2-5-flash)\n- [Gemini 2.0 Flash](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash)\n- [Gemini 2.0 Flash-Lite](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash-lite)\n\n### Self-deployed models from Model Garden\n\nThe\n[Hugging Face Text Generation Interface (HF TGI)](https://huggingface.co/docs/text-generation-inference/en/index)\nand\n[Vertex AI Model Garden prebuilt vLLM](http://us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve)\ncontainers support the Chat Completions API. However,\nnot every model deployed to these containers supports the Chat Completions API.\nThe following table includes the most popular supported models by container:\n\nSupported parameters\n--------------------\n\nFor Google models, the Chat Completions API supports the following OpenAI\nparameters. For a description of each parameter, see OpenAI's documentation on\n[Creating chat completions](https://platform.openai.com/docs/api-reference/chat/create).\nParameter support for third-party models varies by model. To see which parameters\nare supported, consult the model's documentation.\n\nIf you pass any unsupported parameter, it is ignored.\n\n### Multimodal input parameters\n\nThe Chat Completions API supports select multimodal inputs.\n\nIn general, the `data` parameter can be a URI or a combination of MIME type and\nbase64 encoded bytes in the form `\"data:\u003cMIME-TYPE\u003e;base64,\u003cBASE64-ENCODED-BYTES\u003e\"`.\nFor a full list of MIME types, see [`GenerateContent`](/vertex-ai/generative-ai/docs/model-reference/inference#blob).\nFor more information on OpenAI's base64 encoding, see [their documentation](https://platform.openai.com/docs/guides/images-vision#giving-a-model-images-as-input).\n\nFor usage, see our [multimodal input examples](/vertex-ai/generative-ai/docs/migrate/openai/examples#multimodal_input_examples).\n\n### Gemini-specific parameters\n\nThere are several features supported by Gemini that are not available in OpenAI models.\nThese features can still be passed in as parameters, but must be contained within an\n`extra_content` or `extra_body` or they will be ignored.\n\n### `extra_body` features\n\nInclude a `google` field to contain any Gemini-specific\n`extra_body` features. \n\n {\n ...,\n \"extra_body\": {\n \"google\": {\n ...,\n // Add extra_body features here.\n }\n }\n }\n\n### `extra_part` features\n\n`extra_part` lets you specify additional settings at a per-`Part` level.\n\nInclude a `google` field to contain any Gemini-specific\n`extra_part` features. \n\n {\n ...,\n \"extra_part\": {\n \"google\": {\n ...,\n // Add extra_part features here.\n }\n }\n }\n\nWhat's next\n-----------\n\n- Learn more about [authentication and credentialing](/vertex-ai/generative-ai/docs/migrate/openai/auth-and-credentials) with the OpenAI-compatible syntax.\n- See examples of calling the [Chat Completions API](/vertex-ai/generative-ai/docs/migrate/openai/examples) with the OpenAI-compatible syntax.\n- See examples of calling the [Inference API](/vertex-ai/generative-ai/docs/model-reference/inference#examples) with the OpenAI-compatible syntax.\n- See examples of calling the [Function Calling API](/vertex-ai/generative-ai/docs/model-reference/function-calling#examples) with OpenAI-compatible syntax.\n- Learn more about the [Gemini API](/vertex-ai/generative-ai/docs/overview).\n- Learn more about [migrating from Azure OpenAI to the Gemini API](/vertex-ai/generative-ai/docs/migrate/migrate-from-azure-to-gemini)."]]