У меня возникла проблема с отправкой модели готового ответа API для форматирования пользователю.
Как только объявлены необходимые функции для вызовов базы данных:
Код: Выделить всё
list_tables_func = vertexai.generative_models.FunctionDeclaration.from_func(list_tables)
describe_table_func = vertexai.generative_models.FunctionDeclaration.from_func(describe_table)
execute_query_func = vertexai.generative_models.FunctionDeclaration.from_func(execute_query)
Код: Выделить всё
db_tool = vertexai.generative_models.Tool(
function_declarations = [list_tables_func, describe_table_func, execute_query_func]
)
instruction = ...
vertexai.init(project=..., location=...)
model = vertexai.generative_models.GenerativeModel(
"gemini-1.5-flash-001",
tools = [db_tool],
system_instruction = instruction)
chat = model.start_chat()
Код: Выделить всё
query = "What is the most expensive product?"
resp = chat.send_message(content=query,
tools=[db_tool])
function_calls = resp.candidates[0].function_calls
func = function_calls[-1] # the last query is a clue to retrieve user's answer
Код: Выделить всё
name: "execute_query"
args {
fields {
key: "sql"
value {
string_value: "SELECT name FROM products ORDER BY price DESC LIMIT 1"
}
}
}
Код: Выделить всё
sql_query = func.args["sql"].replace("name", "product_name")
Код: Выделить всё
api_response = {"text": "; ".join([", ".join(items) for items in sql_query])}
user_response = chat.send_message(
Content(
role = "user",
parts = [vertexai.generative_models.Part.from_function_response(
name = func.name,
response = {"content": api_response})]
)
)
Код: Выделить всё
Подробнее здесь: [url]https://stackoverflow.com/questions/79188754/vertex-ai-function-calling-using-response[/url]
Мобильная версия