{"cachedContent":string,"contents":[{"role":string,"parts":[{// Union field data can be only one of the following:"text":string,"inlineData":{"mimeType":string,"data":string},"fileData":{"mimeType":string,"fileUri":string},// End of list of possible types for union field data."videoMetadata":{"startOffset":{"seconds":integer,"nanos":integer},"endOffset":{"seconds":integer,"nanos":integer},"fps":double}}]}],"systemInstruction":{"role":string,"parts":[{"text":string}]},"tools":[{"functionDeclarations":[{"name":string,"description":string,"parameters":{object(OpenAPIObjectSchema)}}]}],"safetySettings":[{"category":enum(HarmCategory),"threshold":enum(HarmBlockThreshold)}],"generationConfig":{"temperature":number,"topP":number,"topK":number,"candidateCount":integer,"maxOutputTokens":integer,"presencePenalty":float,"frequencyPenalty":float,"stopSequences":[string],"responseMimeType":string,"responseSchema":schema,"seed":integer,"responseLogprobs":boolean,"logprobs":integer,"audioTimestamp":boolean},"labels":{string:string}}
Top-P 會影響模型選取輸出符記的方式。模型會按照機率最高 (請見「Top-K」) 到最低的順序選取符記,直到所選符記的機率總和等於「Top-P」值。舉例來說,假設詞元 A、B 和 C 的可能性分別為 0.3、0.2 和 0.1,而「Top-P」值為 0.5,模型會依據 temperature 選擇 A 或 B 做為下一個詞元,並排除 C。
下列範例說明如何使用原生 Gen AI SDK for Python 或 OpenAI 程式庫,將要求傳送至模型。您可以設定 OpenAI 程式庫,使其與 Vertex AI 端點搭配運作。
程式庫
說明
用途
Python 適用的 Gen AI SDK
與 Gemini 模型互動的官方 Google 程式庫。這項服務與 Google Cloud 服務完全整合,並提供所有最新功能。
建議用於新專案,以及想充分運用 Vertex AI 功能的開發人員。
OpenAI 程式庫
這是與大型語言模型互動的熱門第三方程式庫。您可以設定這項功能,將要求傳送至 Vertex AI 端點。
如果開發人員要從 OpenAI 平台遷移現有應用程式,或是偏好使用 OpenAI API 結構,這項功能就非常實用。
文字生成
根據文字輸入生成文字回覆。
Python 適用的 Gen AI SDK
fromgoogleimportgenaifromgoogle.genai.typesimportHttpOptionsclient=genai.Client(http_options=HttpOptions(api_version="v1"))response=client.models.generate_content(model="gemini-2.5-flash",contents="How does AI work?",)print(response.text)# Example response:# Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...## Here's a simplified overview:# ...
fromgoogle.authimportdefaultimportgoogle.auth.transport.requestsimportopenai# TODO(developer): Update and un-comment below lines# project_id = "PROJECT_ID"# location = "us-central1"# Programmatically get an access tokencredentials,_=default(scopes=["https://www.googleapis.com/auth/cloud-platform"])credentials.refresh(google.auth.transport.requests.Request())# OpenAI Clientclient=openai.OpenAI(base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",api_key=credentials.token,)response=client.chat.completions.create(model="google/gemini-2.0-flash-001",messages=[{"role":"user","content":"Why is the sky blue?"}],)print(response)
Go
import("context""fmt""io""google.golang.org/genai")//generateWithTextshowshowtogeneratetextusingatextprompt.funcgenerateWithText(wio.Writer)error{ctx:=context.Background()client,err:=genai.NewClient(ctx, &genai.ClientConfig{HTTPOptions:genai.HTTPOptions{APIVersion:"v1"},})iferr!=nil{returnfmt.Errorf("failed to create genai client: %w",err)}resp,err:=client.Models.GenerateContent(ctx,"gemini-2.5-flash",genai.Text("How does AI work?"),nil,)iferr!=nil{returnfmt.Errorf("failed to generate content: %w",err)}respText:=resp.Text()fmt.Fprintln(w,respText)//Exampleresponse://That's a great question! Understanding how AI works can feel like ...//...//**1.TheFoundation:DataandAlgorithms**//...returnnil}
使用多模態提示
根據多模態輸入內容 (例如文字和圖片) 生成文字回覆。
Python 適用的 Gen AI SDK
fromgoogleimportgenaifromgoogle.genai.typesimportHttpOptions,Partclient=genai.Client(http_options=HttpOptions(api_version="v1"))response=client.models.generate_content(model="gemini-2.5-flash",contents=["What is shown in this image?",Part.from_uri(file_uri="gs://cloud-samples-data/generative-ai/image/scones.jpg",mime_type="image/jpeg",),],)print(response.text)# Example response:# The image shows a flat lay of blueberry scones arranged on parchment paper. There are ...
fromgoogle.authimportdefaultimportgoogle.auth.transport.requestsimportopenai# TODO(developer): Update and un-comment below lines# project_id = "PROJECT_ID"# location = "us-central1"# Programmatically get an access tokencredentials,_=default(scopes=["https://www.googleapis.com/auth/cloud-platform"])credentials.refresh(google.auth.transport.requests.Request())# OpenAI Clientclient=openai.OpenAI(base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",api_key=credentials.token,)response=client.chat.completions.create(model="google/gemini-2.0-flash-001",messages=[{"role":"user","content":[{"type":"text","text":"Describe the following image:"},{"type":"image_url","image_url":"gs://cloud-samples-data/generative-ai/image/scones.jpg",},],}],)print(response)
Go
import("context""fmt""io"genai"google.golang.org/genai")//generateWithTextImageshowshowtogeneratetextusingbothtextandimageinputfuncgenerateWithTextImage(wio.Writer)error{ctx:=context.Background()client,err:=genai.NewClient(ctx, &genai.ClientConfig{HTTPOptions:genai.HTTPOptions{APIVersion:"v1"},})iferr!=nil{returnfmt.Errorf("failed to create genai client: %w",err)}modelName:="gemini-2.5-flash"contents:=[]*genai.Content{{Parts:[]*genai.Part{{Text:"What is shown in this image?"},{FileData: &genai.FileData{//Imagesource:https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpgFileURI:"gs://cloud-samples-data/generative-ai/image/scones.jpg",MIMEType:"image/jpeg",}},},Role:"user"},}resp,err:=client.Models.GenerateContent(ctx,modelName,contents,nil)iferr!=nil{returnfmt.Errorf("failed to generate content: %w",err)}respText:=resp.Text()fmt.Fprintln(w,respText)//Exampleresponse://Theimageshowsanoverheadshotofarustic,artisticarrangementonasurfacethat...returnnil}
串流文字回覆
根據文字輸入內容生成串流模型回應。
Python 適用的 Gen AI SDK
fromgoogleimportgenaifromgoogle.genai.typesimportHttpOptionsclient=genai.Client(http_options=HttpOptions(api_version="v1"))forchunkinclient.models.generate_content_stream(model="gemini-2.5-flash",contents="Why is the sky blue?",):print(chunk.text,end="")# Example response:# The# sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's# a breakdown of why:# ...
fromgoogle.authimportdefaultimportgoogle.auth.transport.requestsimportopenai# TODO(developer): Update and un-comment below lines# project_id = "PROJECT_ID"# location = "us-central1"# Programmatically get an access tokencredentials,_=default(scopes=["https://www.googleapis.com/auth/cloud-platform"])credentials.refresh(google.auth.transport.requests.Request())# OpenAI Clientclient=openai.OpenAI(base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",api_key=credentials.token,)response=client.chat.completions.create(model="google/gemini-2.0-flash-001",messages=[{"role":"user","content":"Why is the sky blue?"}],stream=True,)forchunkinresponse:print(chunk)
Go
import("context""fmt""io"genai"google.golang.org/genai")//generateWithTextStreamshowshowtogeneratetextstreamusingatextprompt.funcgenerateWithTextStream(wio.Writer)error{ctx:=context.Background()client,err:=genai.NewClient(ctx, &genai.ClientConfig{HTTPOptions:genai.HTTPOptions{APIVersion:"v1"},})iferr!=nil{returnfmt.Errorf("failed to create genai client: %w",err)}modelName:="gemini-2.5-flash"contents:=genai.Text("Why is the sky blue?")forresp,err:=rangeclient.Models.GenerateContentStream(ctx,modelName,contents,nil){iferr!=nil{returnfmt.Errorf("failed to generate content: %w",err)}chunk:=resp.Text()fmt.Fprintln(w,chunk)}//Exampleresponse://The//skyisblue//becauseofaphenomenoncalled**Rayleighscattering**.Here's the breakdown://...returnnil}