Google Geminigenerate_content не работает в API Python с использованием вызова функции function_responsePython

Программы на Python
Anonymous
Google Geminigenerate_content не работает в API Python с использованием вызова функции function_response

Сообщение Anonymous »

Я пытаюсь сделать тот же запрос, что и в CURL, и это работает. Но в Python это не работает.
Документация Gemini:
https://ai.google.dev/gemini-api/docs/f ... ing?hl=pt- br#multi-turn-example-1
Запрос на скручивание:

Код: Выделить всё

import requests

url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
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())
Использование API вершин:

Код: Выделить всё

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)
Ошибка:

Код: Выделить всё

ValueError: Protocol message Struct has no "location" field.
Обратите внимание, что я взял это из документации Gemini, но не смог найти, как это должно работать в Python

Подробнее здесь: https://stackoverflow.com/questions/784 ... tion-calli

Вернуться в «Python»