Generate_content von Google Gemini funktioniert nicht in der Python-API mit dem Funktionsaufruf function_responsePython

Python-Programme
Guest
 Generate_content von Google Gemini funktioniert nicht in der Python-API mit dem Funktionsaufruf function_response

Post by Guest »

Ich versuche, die gleiche Anfrage zu stellen wie in CURL, was funktioniert. Aber in Python funktioniert es nicht.
Die Gemini-Dokumente:
https://ai.google.dev/gemini-api/docs/f ... ing?hl=pt- br#multi-turn-example-1
Die Curl-Anfrage:
import requests

url = "https://generativelanguage.googleapis.c ... ateContent"
api_key = "YOUR_API_KEY" # Replace with your actual API key

headers = {
'Content-Type': 'application/json'
}

data = {
"contents": [
{
"role": "user",
"parts": [
{
"text": "Which theaters in Mountain View show Barbie movie?"
}
]
},
{
"role": "model",
"parts": [
{
"functionCall": {
"name": "find_theaters",
"args": {
"location": "Mountain View, CA",
"movie": "Barbie"
}
}
}
]
},
{
"role": "function",
"parts": [
{
"functionResponse": {
"name": "find_theaters",
"response": {
"name": "find_theaters",
"content": {
"movie": "Barbie",
"theaters": [
{
"name": "AMC Mountain View 16",
"address": "2000 W El Camino Real, Mountain View, CA 94040"
},
{
"name": "Regal Edwards 14",
"address": "245 Castro St, Mountain View, CA 94040"
}
]
}
}
}
}
]
}
],
"tools": [
{
"functionDeclarations": [
{
"name": "find_movies",
"description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
"parameters": {
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
},
"description": {
"type": "STRING",
"description": "Any kind of description including category or genre, title words, attributes, etc."
}
},
"required": ["description"]
}
},
{
"name": "find_theaters",
"description": "find theaters based on location and optionally movie title which is currently playing in theaters",
"parameters": {
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
},
"movie": {
"type": "STRING",
"description": "Any movie title"
}
},
"required": ["location"]
}
},
{
"name": "get_showtimes",
"description": "Find the start times for movies playing in a specific theater",
"parameters": {
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
},
"movie": {
"type": "STRING",
"description": "Any movie title"
},
"theater": {
"type": "STRING",
"description": "Name of the theater"
},
"date": {
"type": "STRING",
"description": "Date for requested showtime"
}
},
"required": ["location", "movie", "theater", "date"]
}
}
]
}
]
}

response = requests.post(url, headers=headers, json=data, params={'key': api_key})

print(response.json())

Vertex-API verwenden:
messages = [
{
"role": "user",
"parts": [{"text": "Which theaters in Mountain View show Barbie movie?"}],
},
{
"role": "model",
"parts": [
{
"function_call": {
"name": "find_theaters",
"args": {"location": "Mountain View, CA", "movie": "Barbie"},
}
}
],
},
{
"role": "function",
"parts": [
{
"function_response": {
"name": "find_theaters",
"response": {
"name": "find_theaters",
"content": {
"movie": "Barbie",
"theaters": [
{
"name": "AMC Mountain View 16",
"address": "2000 W El Camino Real, Mountain View, CA 94040",
},
{
"name": "Regal Edwards 14",
"address": "245 Castro St, Mountain View, CA 94040",
},
],
},
},
}
}
],
},
]

# Define the tools
tools = [
{
"function_declarations": [
{
"name": "find_movies",
"description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
"description": {
"type": "string",
"description": "Any kind of description including category or genre, title words, attributes, etc.",
},
},
"required": ["description"],
},
},
{
"name": "find_theaters",
"description": "find theaters based on location and optionally movie title which is currently playing in theaters",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
"movie": {
"type": "string",
"description": "Any movie title",
},
},
"required": ["location"],
},
},
{
"name": "get_showtimes",
"description": "Find the start times for movies playing in a specific theater",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
},
"movie": {
"type": "string",
"description": "Any movie title",
},
"theater": {
"type": "string",
"description": "Name of the theater",
},
"date": {
"type": "string",
"description": "Date for requested showtime",
},
},
"required": ["location", "movie", "theater", "date"],
},
},
]
}
]

model = GenerativeModel("gemini-pro")

model.generate_content(messages, tool_config=tool_config, tools=tools)

Der Fehler:
ValueError: Protocol message Struct has no "location" field.
Bitte beachten Sie, dass ich es der Gemini-Dokumentation entnommen habe, aber nicht finden konnte, wie es in Python funktionieren sollte

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post