mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
Merge ba737e1c5f into e52f05566d
This commit is contained in:
commit
b11fbb7d8e
2 changed files with 79 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import json
|
||||
from collections.abc import AsyncIterator, Iterator, Sequence
|
||||
from collections.abc import AsyncIterator, Iterator, Mapping, Sequence
|
||||
from typing import Any, Final, TypedDict, cast
|
||||
|
||||
from typing_extensions import ReadOnly
|
||||
|
|
@ -47,6 +47,7 @@ class _GenAIPart(TypedDict, total=False):
|
|||
class _GenAIFunctionDeclaration(TypedDict, total=False):
|
||||
name: ReadOnly[str]
|
||||
description: ReadOnly[str]
|
||||
parameters: ReadOnly[Mapping[str, object]]
|
||||
parametersJsonSchema: ReadOnly[dict[str, object]]
|
||||
|
||||
|
||||
|
|
@ -359,6 +360,8 @@ class GoogleGenAIAdapter:
|
|||
function_chunk["description"] = func_decl["description"]
|
||||
if "parametersJsonSchema" in func_decl:
|
||||
function_chunk["parameters"] = func_decl["parametersJsonSchema"]
|
||||
elif "parameters" in func_decl:
|
||||
function_chunk["parameters"] = func_decl["parameters"]
|
||||
|
||||
openai_tool: dict[str, object] = {"type": "function", "function": function_chunk}
|
||||
openai_tools.append(openai_tool)
|
||||
|
|
|
|||
|
|
@ -1267,3 +1267,78 @@ def test_inline_data_backward_compatibility_text_only():
|
|||
content, str
|
||||
), "Content should be a string for text-only messages (backward compatibility)"
|
||||
assert content == "Hello, how are you?"
|
||||
|
||||
|
||||
def test_tools_transformation_parameters_field():
|
||||
from litellm.google_genai.adapters.transformation import GoogleGenAIAdapter
|
||||
|
||||
adapter = GoogleGenAIAdapter()
|
||||
|
||||
schema = {
|
||||
"type": "OBJECT",
|
||||
"properties": {"city": {"type": "STRING", "description": "City name"}},
|
||||
"required": ["city"],
|
||||
}
|
||||
tools = [
|
||||
{
|
||||
"functionDeclarations": [
|
||||
{
|
||||
"name": "get_weather",
|
||||
"description": "Get the weather for a city",
|
||||
"parameters": schema,
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
completion_request = adapter.translate_generate_content_to_completion(
|
||||
model="gpt-3.5-turbo",
|
||||
contents={"role": "user", "parts": [{"text": "Weather in Hanoi?"}]},
|
||||
tools=tools,
|
||||
)
|
||||
|
||||
function = completion_request["tools"][0]["function"]
|
||||
assert function["name"] == "get_weather"
|
||||
assert function["parameters"] == {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string", "description": "City name"}},
|
||||
"required": ["city"],
|
||||
}
|
||||
|
||||
|
||||
def test_tools_transformation_prefers_parameters_json_schema():
|
||||
from litellm.google_genai.adapters.transformation import GoogleGenAIAdapter
|
||||
|
||||
adapter = GoogleGenAIAdapter()
|
||||
|
||||
tools = [
|
||||
{
|
||||
"functionDeclarations": [
|
||||
{
|
||||
"name": "get_weather",
|
||||
"parameters": {
|
||||
"type": "OBJECT",
|
||||
"properties": {"ignored": {"type": "STRING"}},
|
||||
},
|
||||
"parametersJsonSchema": {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
"required": ["city"],
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
completion_request = adapter.translate_generate_content_to_completion(
|
||||
model="gpt-3.5-turbo",
|
||||
contents={"role": "user", "parts": [{"text": "Weather in Hanoi?"}]},
|
||||
tools=tools,
|
||||
)
|
||||
|
||||
function = completion_request["tools"][0]["function"]
|
||||
assert function["parameters"] == {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
"required": ["city"],
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue