Testen Sie Gemini 1.5-Modelle, unsere neuesten multimodalen Modelle in Vertex AI, und erfahren Sie, was Sie mit einem 1 Mio. Tokenkontextfenster erstellen können.
Tesen Sie Gemini 1.5-Modelle, unsere neuesten multimodalen Modelle in Vertex AI, und erfahren Sie, was Sie mit einem 1 Mio. Tokenkontextfenster erstellen können.
Textantworten mit der Gemini API mit externen Funktionsaufrufen in einem Chatszenario generieren
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Generieren Sie Textantworten mithilfe der Gemini API mit externen Funktionsaufrufen. Dieses Beispiel zeigt ein Chatszenario mit zwei Funktionen und zwei aufeinanderfolgenden Prompts.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Python
Bevor Sie dieses Beispiel anwenden, folgen Sie den Python-Einrichtungsschritten in der Vertex AI-Kurzanleitung zur Verwendung von Clientbibliotheken.
Weitere Informationen finden Sie in der Referenzdokumentation zur Vertex AI Python API.
Richten Sie zur Authentifizierung bei Vertex AI Standardanmeldedaten für Anwendungen ein.
Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.
import vertexai
from vertexai.generative_models import (
FunctionDeclaration,
GenerationConfig,
GenerativeModel,
Part,
Tool,
)
def generate_function_call_chat(project_id: str, location: str) -> tuple:
prompts = []
summaries = []
# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# Specify a function declaration and parameters for an API request
get_product_info_func = FunctionDeclaration(
name="get_product_sku",
description="Get the SKU for a product",
# Function parameters are specified in OpenAPI JSON schema format
parameters={
"type": "object",
"properties": {
"product_name": {"type": "string", "description": "Product name"}
},
},
)
# Specify another function declaration and parameters for an API request
get_store_location_func = FunctionDeclaration(
name="get_store_location",
description="Get the location of the closest store",
# Function parameters are specified in OpenAPI JSON schema format
parameters={
"type": "object",
"properties": {"location": {"type": "string", "description": "Location"}},
},
)
# Define a tool that includes the above functions
retail_tool = Tool(
function_declarations=[
get_product_info_func,
get_store_location_func,
],
)
# Initialize Gemini model
model = GenerativeModel(
"gemini-1.0-pro-001",
generation_config=GenerationConfig(temperature=0),
tools=[retail_tool],
)
# Start a chat session
chat = model.start_chat()
# Send a prompt for the first conversation turn that should invoke the get_product_sku function
prompt = "Do you have the Pixel 8 Pro in stock?"
response = chat.send_message(prompt)
prompts.append(prompt)
# Check the function name that the model responded with, and make an API call to an external system
if response.candidates[0].content.parts[0].function_call.name == "get_product_sku":
# Extract the arguments to use in your API call
product_name = (
response.candidates[0].content.parts[0].function_call.args["product_name"]
)
product_name
# Here you can use your preferred method to make an API request to retrieve the product SKU, as in:
# api_response = requests.post(product_api_url, data={"product_name": product_name})
# In this example, we'll use synthetic data to simulate a response payload from an external API
api_response = {"sku": "GA04834-US", "in_stock": "yes"}
# Return the API response to Gemini so it can generate a model response or request another function call
response = chat.send_message(
Part.from_function_response(
name="get_product_sku",
response={
"content": api_response,
},
),
)
# Extract the text from the summary response
summary = response.candidates[0].content.parts[0].text
summaries.append(summary)
# Send a prompt for the second conversation turn that should invoke the get_store_location function
prompt = "Is there a store in Mountain View, CA that I can visit to try it out?"
response = chat.send_message(prompt)
prompts.append(prompt)
# Check the function name that the model responded with, and make an API call to an external system
if (
response.candidates[0].content.parts[0].function_call.name
== "get_store_location"
):
# Extract the arguments to use in your API call
location = (
response.candidates[0].content.parts[0].function_call.args["location"]
)
location
# Here you can use your preferred method to make an API request to retrieve store location closest to the user, as in:
# api_response = requests.post(store_api_url, data={"location": location})
# In this example, we'll use synthetic data to simulate a response payload from an external API
api_response = {"store": "2000 N Shoreline Blvd, Mountain View, CA 94043, US"}
# Return the API response to Gemini so it can generate a model response or request another function call
response = chat.send_message(
Part.from_function_response(
name="get_store_location",
response={
"content": api_response,
},
),
)
# Extract the text from the summary response
summary = response.candidates[0].content.parts[0].text
summaries.append(summary)
return prompts, summaries
Nächste Schritte
Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"Problem mit der Übersetzung"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Sonstiges"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Leicht verständlich"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Mein Problem wurde gelöst"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Sonstiges"
}]