google-adk 0.0.3__py3-none-any.whl → 0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. google/adk/agents/run_config.py +4 -0
  2. google/adk/auth/auth_preprocessor.py +19 -16
  3. google/adk/cli/browser/index.html +1 -1
  4. google/adk/cli/browser/{main-SY2WYYGV.js → main-SLIAU2JL.js} +46 -30
  5. google/adk/cli/cli.py +8 -8
  6. google/adk/cli/cli_deploy.py +2 -4
  7. google/adk/cli/cli_tools_click.py +6 -6
  8. google/adk/flows/llm_flows/base_llm_flow.py +1 -1
  9. google/adk/flows/llm_flows/contents.py +21 -1
  10. google/adk/flows/llm_flows/functions.py +3 -1
  11. google/adk/models/google_llm.py +0 -1
  12. google/adk/runners.py +13 -2
  13. google/adk/version.py +1 -1
  14. {google_adk-0.0.3.dist-info → google_adk-0.0.5.dist-info}/METADATA +4 -2
  15. google_adk-0.0.5.dist-info/RECORD +175 -0
  16. {google_adk-0.0.3.dist-info → google_adk-0.0.5.dist-info}/WHEEL +1 -1
  17. google_adk-0.0.5.dist-info/licenses/LICENSE +202 -0
  18. google/adk/._version.py +0 -0
  19. google/adk/docs/Makefile +0 -20
  20. google/adk/docs/build/doctrees/google-adk.doctree +0 -0
  21. google/adk/docs/build/html/_sources/google-adk.rst.txt +0 -98
  22. google/adk/docs/build/html/_sources/index.rst.txt +0 -7
  23. google/adk/docs/build/html/_static/autodoc_pydantic.css +0 -27
  24. google/adk/docs/build/html/_static/basic.css +0 -925
  25. google/adk/docs/build/html/_static/debug.css +0 -85
  26. google/adk/docs/build/html/_static/doctools.js +0 -156
  27. google/adk/docs/build/html/_static/documentation_options.js +0 -29
  28. google/adk/docs/build/html/_static/file.png +0 -0
  29. google/adk/docs/build/html/_static/language_data.js +0 -199
  30. google/adk/docs/build/html/_static/minus.png +0 -0
  31. google/adk/docs/build/html/_static/plus.png +0 -0
  32. google/adk/docs/build/html/_static/pygments.css +0 -274
  33. google/adk/docs/build/html/_static/scripts/furo-extensions.js +0 -16
  34. google/adk/docs/build/html/_static/scripts/furo.js +0 -19
  35. google/adk/docs/build/html/_static/scripts/furo.js.LICENSE.txt +0 -7
  36. google/adk/docs/build/html/_static/scripts/furo.js.map +0 -1
  37. google/adk/docs/build/html/_static/searchtools.js +0 -620
  38. google/adk/docs/build/html/_static/skeleton.css +0 -312
  39. google/adk/docs/build/html/_static/sphinx_highlight.js +0 -170
  40. google/adk/docs/build/html/_static/styles/furo-extensions.css +0 -18
  41. google/adk/docs/build/html/_static/styles/furo-extensions.css.map +0 -1
  42. google/adk/docs/build/html/_static/styles/furo.css +0 -18
  43. google/adk/docs/build/html/_static/styles/furo.css.map +0 -1
  44. google/adk/docs/build/html/genindex.html +0 -861
  45. google/adk/docs/build/html/google-adk.html +0 -5461
  46. google/adk/docs/build/html/index.html +0 -567
  47. google/adk/docs/build/html/objects.inv +0 -0
  48. google/adk/docs/build/html/py-modindex.html +0 -373
  49. google/adk/docs/build/html/search.html +0 -333
  50. google/adk/docs/build/html/searchindex.js +0 -17
  51. google/adk/docs/source/conf.py +0 -133
  52. google/adk/docs/source/google-adk.rst +0 -98
  53. google/adk/docs/source/index.rst +0 -7
  54. google/adk/tests/__init__.py +0 -14
  55. google/adk/tests/integration/.env.example +0 -10
  56. google/adk/tests/integration/__init__.py +0 -18
  57. google/adk/tests/integration/conftest.py +0 -119
  58. google/adk/tests/integration/fixture/__init__.py +0 -14
  59. google/adk/tests/integration/fixture/agent_with_config/__init__.py +0 -15
  60. google/adk/tests/integration/fixture/agent_with_config/agent.py +0 -88
  61. google/adk/tests/integration/fixture/callback_agent/__init__.py +0 -15
  62. google/adk/tests/integration/fixture/callback_agent/agent.py +0 -105
  63. google/adk/tests/integration/fixture/context_update_test/OWNERS +0 -1
  64. google/adk/tests/integration/fixture/context_update_test/__init__.py +0 -15
  65. google/adk/tests/integration/fixture/context_update_test/agent.py +0 -43
  66. google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +0 -582
  67. google/adk/tests/integration/fixture/context_variable_agent/__init__.py +0 -15
  68. google/adk/tests/integration/fixture/context_variable_agent/agent.py +0 -115
  69. google/adk/tests/integration/fixture/customer_support_ma/__init__.py +0 -15
  70. google/adk/tests/integration/fixture/customer_support_ma/agent.py +0 -172
  71. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +0 -15
  72. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +0 -338
  73. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +0 -69
  74. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +0 -6
  75. google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +0 -15
  76. google/adk/tests/integration/fixture/flow_complex_spark/agent.py +0 -182
  77. google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +0 -190
  78. google/adk/tests/integration/fixture/hello_world_agent/__init__.py +0 -15
  79. google/adk/tests/integration/fixture/hello_world_agent/agent.py +0 -95
  80. google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +0 -24
  81. google/adk/tests/integration/fixture/hello_world_agent/test_config.json +0 -6
  82. google/adk/tests/integration/fixture/home_automation_agent/__init__.py +0 -15
  83. google/adk/tests/integration/fixture/home_automation_agent/agent.py +0 -304
  84. google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +0 -5
  85. google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +0 -5
  86. google/adk/tests/integration/fixture/home_automation_agent/test_config.json +0 -5
  87. google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +0 -18
  88. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +0 -17
  89. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +0 -6
  90. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +0 -18
  91. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +0 -17
  92. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +0 -5
  93. google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +0 -5
  94. google/adk/tests/integration/fixture/tool_agent/__init__.py +0 -15
  95. google/adk/tests/integration/fixture/tool_agent/agent.py +0 -218
  96. google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
  97. google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +0 -15
  98. google/adk/tests/integration/fixture/trip_planner_agent/agent.py +0 -110
  99. google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +0 -13
  100. google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +0 -5
  101. google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +0 -13
  102. google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +0 -5
  103. google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +0 -7
  104. google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +0 -19
  105. google/adk/tests/integration/models/__init__.py +0 -14
  106. google/adk/tests/integration/models/test_google_llm.py +0 -65
  107. google/adk/tests/integration/test_callback.py +0 -70
  108. google/adk/tests/integration/test_context_variable.py +0 -67
  109. google/adk/tests/integration/test_evalute_agent_in_fixture.py +0 -76
  110. google/adk/tests/integration/test_multi_agent.py +0 -28
  111. google/adk/tests/integration/test_multi_turn.py +0 -42
  112. google/adk/tests/integration/test_single_agent.py +0 -23
  113. google/adk/tests/integration/test_sub_agent.py +0 -26
  114. google/adk/tests/integration/test_system_instruction.py +0 -177
  115. google/adk/tests/integration/test_tools.py +0 -287
  116. google/adk/tests/integration/test_with_test_file.py +0 -34
  117. google/adk/tests/integration/tools/__init__.py +0 -14
  118. google/adk/tests/integration/utils/__init__.py +0 -16
  119. google/adk/tests/integration/utils/asserts.py +0 -75
  120. google/adk/tests/integration/utils/test_runner.py +0 -97
  121. google/adk/tests/unittests/__init__.py +0 -14
  122. google/adk/tests/unittests/agents/__init__.py +0 -14
  123. google/adk/tests/unittests/agents/test_base_agent.py +0 -407
  124. google/adk/tests/unittests/agents/test_langgraph_agent.py +0 -191
  125. google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +0 -138
  126. google/adk/tests/unittests/agents/test_llm_agent_fields.py +0 -231
  127. google/adk/tests/unittests/agents/test_loop_agent.py +0 -136
  128. google/adk/tests/unittests/agents/test_parallel_agent.py +0 -92
  129. google/adk/tests/unittests/agents/test_sequential_agent.py +0 -114
  130. google/adk/tests/unittests/artifacts/__init__.py +0 -14
  131. google/adk/tests/unittests/artifacts/test_artifact_service.py +0 -276
  132. google/adk/tests/unittests/auth/test_auth_handler.py +0 -575
  133. google/adk/tests/unittests/conftest.py +0 -73
  134. google/adk/tests/unittests/fast_api/__init__.py +0 -14
  135. google/adk/tests/unittests/fast_api/test_fast_api.py +0 -269
  136. google/adk/tests/unittests/flows/__init__.py +0 -14
  137. google/adk/tests/unittests/flows/llm_flows/__init__.py +0 -14
  138. google/adk/tests/unittests/flows/llm_flows/_test_examples.py +0 -142
  139. google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +0 -311
  140. google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +0 -244
  141. google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +0 -346
  142. google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +0 -93
  143. google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +0 -258
  144. google/adk/tests/unittests/flows/llm_flows/test_identity.py +0 -66
  145. google/adk/tests/unittests/flows/llm_flows/test_instructions.py +0 -164
  146. google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +0 -142
  147. google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +0 -46
  148. google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +0 -269
  149. google/adk/tests/unittests/models/__init__.py +0 -14
  150. google/adk/tests/unittests/models/test_google_llm.py +0 -224
  151. google/adk/tests/unittests/models/test_litellm.py +0 -804
  152. google/adk/tests/unittests/models/test_models.py +0 -60
  153. google/adk/tests/unittests/sessions/__init__.py +0 -14
  154. google/adk/tests/unittests/sessions/test_session_service.py +0 -227
  155. google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +0 -246
  156. google/adk/tests/unittests/streaming/__init__.py +0 -14
  157. google/adk/tests/unittests/streaming/test_streaming.py +0 -50
  158. google/adk/tests/unittests/tools/__init__.py +0 -14
  159. google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +0 -499
  160. google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +0 -204
  161. google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +0 -600
  162. google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +0 -630
  163. google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +0 -345
  164. google/adk/tests/unittests/tools/google_api_tool/__init__.py +0 -13
  165. google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +0 -657
  166. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +0 -145
  167. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +0 -68
  168. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +0 -153
  169. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +0 -196
  170. google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +0 -573
  171. google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +0 -436
  172. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +0 -1367
  173. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +0 -628
  174. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +0 -139
  175. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +0 -406
  176. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +0 -966
  177. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +0 -201
  178. google/adk/tests/unittests/tools/retrieval/__init__.py +0 -14
  179. google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +0 -147
  180. google/adk/tests/unittests/tools/test_agent_tool.py +0 -167
  181. google/adk/tests/unittests/tools/test_base_tool.py +0 -141
  182. google/adk/tests/unittests/tools/test_build_function_declaration.py +0 -277
  183. google/adk/tests/unittests/utils.py +0 -304
  184. google_adk-0.0.3.dist-info/RECORD +0 -340
  185. {google_adk-0.0.3.dist-info → google_adk-0.0.5.dist-info}/entry_points.txt +0 -0
@@ -1,804 +0,0 @@
1
- # Copyright 2025 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- from unittest.mock import AsyncMock
17
- from unittest.mock import Mock
18
- from google.adk.models.lite_llm import _content_to_message_param
19
- from google.adk.models.lite_llm import _function_declaration_to_tool_param
20
- from google.adk.models.lite_llm import _get_content
21
- from google.adk.models.lite_llm import _message_to_generate_content_response
22
- from google.adk.models.lite_llm import _model_response_to_chunk
23
- from google.adk.models.lite_llm import _to_litellm_role
24
- from google.adk.models.lite_llm import FunctionChunk
25
- from google.adk.models.lite_llm import LiteLlm
26
- from google.adk.models.lite_llm import LiteLLMClient
27
- from google.adk.models.lite_llm import TextChunk
28
- from google.adk.models.llm_request import LlmRequest
29
- from google.genai import types
30
- from litellm import ChatCompletionAssistantMessage
31
- from litellm import ChatCompletionMessageToolCall
32
- from litellm import Function
33
- from litellm.types.utils import ChatCompletionDeltaToolCall
34
- from litellm.types.utils import Choices
35
- from litellm.types.utils import Delta
36
- from litellm.types.utils import ModelResponse
37
- from litellm.types.utils import StreamingChoices
38
- import pytest
39
-
40
- LLM_REQUEST_WITH_FUNCTION_DECLARATION = LlmRequest(
41
- contents=[
42
- types.Content(
43
- role="user", parts=[types.Part.from_text(text="Test prompt")]
44
- )
45
- ],
46
- config=types.GenerateContentConfig(
47
- tools=[
48
- types.Tool(
49
- function_declarations=[
50
- types.FunctionDeclaration(
51
- name="test_function",
52
- description="Test function description",
53
- parameters=types.Schema(
54
- type=types.Type.OBJECT,
55
- properties={
56
- "test_arg": types.Schema(
57
- type=types.Type.STRING
58
- ),
59
- "array_arg": types.Schema(
60
- type=types.Type.ARRAY,
61
- items={
62
- "type": types.Type.STRING,
63
- },
64
- ),
65
- "nested_arg": types.Schema(
66
- type=types.Type.OBJECT,
67
- properties={
68
- "nested_key1": types.Schema(
69
- type=types.Type.STRING
70
- ),
71
- "nested_key2": types.Schema(
72
- type=types.Type.STRING
73
- ),
74
- },
75
- ),
76
- },
77
- ),
78
- )
79
- ]
80
- )
81
- ],
82
- ),
83
- )
84
-
85
-
86
- STREAMING_MODEL_RESPONSE = [
87
- ModelResponse(
88
- choices=[
89
- StreamingChoices(
90
- finish_reason=None,
91
- delta=Delta(
92
- role="assistant",
93
- content="zero, ",
94
- ),
95
- )
96
- ]
97
- ),
98
- ModelResponse(
99
- choices=[
100
- StreamingChoices(
101
- finish_reason=None,
102
- delta=Delta(
103
- role="assistant",
104
- content="one, ",
105
- ),
106
- )
107
- ]
108
- ),
109
- ModelResponse(
110
- choices=[
111
- StreamingChoices(
112
- finish_reason=None,
113
- delta=Delta(
114
- role="assistant",
115
- content="two:",
116
- ),
117
- )
118
- ]
119
- ),
120
- ModelResponse(
121
- choices=[
122
- StreamingChoices(
123
- finish_reason=None,
124
- delta=Delta(
125
- role="assistant",
126
- tool_calls=[
127
- ChatCompletionDeltaToolCall(
128
- type="function",
129
- id="test_tool_call_id",
130
- function=Function(
131
- name="test_function",
132
- arguments='{"test_arg": "test_',
133
- ),
134
- index=0,
135
- )
136
- ],
137
- ),
138
- )
139
- ]
140
- ),
141
- ModelResponse(
142
- choices=[
143
- StreamingChoices(
144
- finish_reason=None,
145
- delta=Delta(
146
- role="assistant",
147
- tool_calls=[
148
- ChatCompletionDeltaToolCall(
149
- type="function",
150
- id=None,
151
- function=Function(
152
- name=None,
153
- arguments='value"}',
154
- ),
155
- index=0,
156
- )
157
- ],
158
- ),
159
- )
160
- ]
161
- ),
162
- ModelResponse(
163
- choices=[
164
- StreamingChoices(
165
- finish_reason="tool_use",
166
- )
167
- ]
168
- ),
169
- ]
170
-
171
- @pytest.fixture
172
- def mock_response():
173
- return ModelResponse(
174
- choices=[
175
- Choices(
176
- message=ChatCompletionAssistantMessage(
177
- role="assistant",
178
- content="Test response",
179
- tool_calls=[
180
- ChatCompletionMessageToolCall(
181
- type="function",
182
- id="test_tool_call_id",
183
- function=Function(
184
- name="test_function",
185
- arguments='{"test_arg": "test_value"}',
186
- ),
187
- )
188
- ],
189
- )
190
- )
191
- ]
192
- )
193
-
194
-
195
- @pytest.fixture
196
- def mock_acompletion(mock_response):
197
- return AsyncMock(return_value=mock_response)
198
-
199
-
200
- @pytest.fixture
201
- def mock_completion(mock_response):
202
- return Mock(return_value=mock_response)
203
-
204
-
205
- @pytest.fixture
206
- def mock_client(mock_acompletion, mock_completion):
207
- return MockLLMClient(mock_acompletion, mock_completion)
208
-
209
-
210
- @pytest.fixture
211
- def lite_llm_instance(mock_client):
212
- return LiteLlm(model="test_model", llm_client=mock_client)
213
-
214
-
215
- class MockLLMClient(LiteLLMClient):
216
-
217
- def __init__(self, acompletion_mock, completion_mock):
218
- self.acompletion_mock = acompletion_mock
219
- self.completion_mock = completion_mock
220
-
221
- async def acompletion(self, model, messages, tools, **kwargs):
222
- return await self.acompletion_mock(
223
- model=model, messages=messages, tools=tools, **kwargs
224
- )
225
-
226
- def completion(self, model, messages, tools, stream, **kwargs):
227
- return self.completion_mock(
228
- model=model, messages=messages, tools=tools, stream=stream, **kwargs
229
- )
230
-
231
-
232
- @pytest.mark.asyncio
233
- async def test_generate_content_async(mock_acompletion, lite_llm_instance):
234
-
235
- async for response in lite_llm_instance.generate_content_async(
236
- LLM_REQUEST_WITH_FUNCTION_DECLARATION
237
- ):
238
- assert response.content.role == "model"
239
- assert response.content.parts[0].text == "Test response"
240
- assert response.content.parts[1].function_call.name == "test_function"
241
- assert response.content.parts[1].function_call.args == {
242
- "test_arg": "test_value"
243
- }
244
- assert response.content.parts[1].function_call.id == "test_tool_call_id"
245
-
246
- mock_acompletion.assert_called_once()
247
-
248
- _, kwargs = mock_acompletion.call_args
249
- assert kwargs["model"] == "test_model"
250
- assert kwargs["messages"][0]["role"] == "user"
251
- assert kwargs["messages"][0]["content"] == "Test prompt"
252
- assert kwargs["tools"][0]["function"]["name"] == "test_function"
253
- assert (
254
- kwargs["tools"][0]["function"]["description"]
255
- == "Test function description"
256
- )
257
- assert (
258
- kwargs["tools"][0]["function"]["parameters"]["properties"]["test_arg"][
259
- "type"
260
- ]
261
- == "string"
262
- )
263
-
264
-
265
- function_declaration_test_cases = [
266
- (
267
- "simple_function",
268
- types.FunctionDeclaration(
269
- name="test_function",
270
- description="Test function description",
271
- parameters=types.Schema(
272
- type=types.Type.OBJECT,
273
- properties={
274
- "test_arg": types.Schema(type=types.Type.STRING),
275
- "array_arg": types.Schema(
276
- type=types.Type.ARRAY,
277
- items=types.Schema(
278
- type=types.Type.STRING,
279
- ),
280
- ),
281
- "nested_arg": types.Schema(
282
- type=types.Type.OBJECT,
283
- properties={
284
- "nested_key1": types.Schema(type=types.Type.STRING),
285
- "nested_key2": types.Schema(type=types.Type.STRING),
286
- },
287
- ),
288
- },
289
- ),
290
- ),
291
- {
292
- "type": "function",
293
- "function": {
294
- "name": "test_function",
295
- "description": "Test function description",
296
- "parameters": {
297
- "type": "object",
298
- "properties": {
299
- "test_arg": {"type": "string"},
300
- "array_arg": {
301
- "items": {"type": "string"},
302
- "type": "array",
303
- },
304
- "nested_arg": {
305
- "properties": {
306
- "nested_key1": {"type": "string"},
307
- "nested_key2": {"type": "string"},
308
- },
309
- "type": "object",
310
- },
311
- },
312
- },
313
- },
314
- },
315
- ),
316
- (
317
- "no_description",
318
- types.FunctionDeclaration(
319
- name="test_function_no_description",
320
- parameters=types.Schema(
321
- type=types.Type.OBJECT,
322
- properties={
323
- "test_arg": types.Schema(type=types.Type.STRING),
324
- },
325
- ),
326
- ),
327
- {
328
- "type": "function",
329
- "function": {
330
- "name": "test_function_no_description",
331
- "description": "",
332
- "parameters": {
333
- "type": "object",
334
- "properties": {
335
- "test_arg": {"type": "string"},
336
- },
337
- },
338
- },
339
- },
340
- ),
341
- (
342
- "empty_parameters",
343
- types.FunctionDeclaration(
344
- name="test_function_empty_params",
345
- parameters=types.Schema(type=types.Type.OBJECT, properties={}),
346
- ),
347
- {
348
- "type": "function",
349
- "function": {
350
- "name": "test_function_empty_params",
351
- "description": "",
352
- "parameters": {
353
- "type": "object",
354
- "properties": {},
355
- },
356
- },
357
- },
358
- ),
359
- (
360
- "nested_array",
361
- types.FunctionDeclaration(
362
- name="test_function_nested_array",
363
- parameters=types.Schema(
364
- type=types.Type.OBJECT,
365
- properties={
366
- "array_arg": types.Schema(
367
- type=types.Type.ARRAY,
368
- items=types.Schema(
369
- type=types.Type.OBJECT,
370
- properties={
371
- "nested_key": types.Schema(
372
- type=types.Type.STRING
373
- )
374
- },
375
- ),
376
- ),
377
- },
378
- ),
379
- ),
380
- {
381
- "type": "function",
382
- "function": {
383
- "name": "test_function_nested_array",
384
- "description": "",
385
- "parameters": {
386
- "type": "object",
387
- "properties": {
388
- "array_arg": {
389
- "items": {
390
- "properties": {
391
- "nested_key": {"type": "string"}
392
- },
393
- "type": "object",
394
- },
395
- "type": "array",
396
- },
397
- },
398
- },
399
- },
400
- },
401
- ),
402
- ]
403
-
404
-
405
- @pytest.mark.parametrize(
406
- "_, function_declaration, expected_output",
407
- function_declaration_test_cases,
408
- ids=[case[0] for case in function_declaration_test_cases],
409
- )
410
- def test_function_declaration_to_tool_param(
411
- _, function_declaration, expected_output
412
- ):
413
- assert (
414
- _function_declaration_to_tool_param(function_declaration)
415
- == expected_output
416
- )
417
-
418
-
419
- @pytest.mark.asyncio
420
- async def test_generate_content_async_with_system_instruction(
421
- lite_llm_instance, mock_acompletion
422
- ):
423
- mock_response_with_system_instruction = ModelResponse(
424
- choices=[
425
- Choices(
426
- message=ChatCompletionAssistantMessage(
427
- role="assistant",
428
- content="Test response",
429
- )
430
- )
431
- ]
432
- )
433
- mock_acompletion.return_value = mock_response_with_system_instruction
434
-
435
- llm_request = LlmRequest(
436
- contents=[
437
- types.Content(
438
- role="user", parts=[types.Part.from_text(text="Test prompt")]
439
- )
440
- ],
441
- config=types.GenerateContentConfig(
442
- system_instruction="Test system instruction"
443
- ),
444
- )
445
-
446
- async for response in lite_llm_instance.generate_content_async(llm_request):
447
- assert response.content.role == "model"
448
- assert response.content.parts[0].text == "Test response"
449
-
450
- mock_acompletion.assert_called_once()
451
-
452
- _, kwargs = mock_acompletion.call_args
453
- assert kwargs["model"] == "test_model"
454
- assert kwargs["messages"][0]["role"] == "developer"
455
- assert kwargs["messages"][0]["content"] == "Test system instruction"
456
- assert kwargs["messages"][1]["role"] == "user"
457
- assert kwargs["messages"][1]["content"] == "Test prompt"
458
-
459
-
460
- @pytest.mark.asyncio
461
- async def test_generate_content_async_with_tool_response(
462
- lite_llm_instance, mock_acompletion
463
- ):
464
- mock_response_with_tool_response = ModelResponse(
465
- choices=[
466
- Choices(
467
- message=ChatCompletionAssistantMessage(
468
- role="tool",
469
- content='{"result": "test_result"}',
470
- tool_call_id="test_tool_call_id",
471
- )
472
- )
473
- ]
474
- )
475
- mock_acompletion.return_value = mock_response_with_tool_response
476
-
477
- llm_request = LlmRequest(
478
- contents=[
479
- types.Content(
480
- role="user", parts=[types.Part.from_text(text="Test prompt")]
481
- ),
482
- types.Content(
483
- role="tool",
484
- parts=[
485
- types.Part.from_function_response(
486
- name="test_function",
487
- response={"result": "test_result"},
488
- )
489
- ],
490
- ),
491
- ],
492
- config=types.GenerateContentConfig(
493
- system_instruction="test instruction",
494
- ),
495
- )
496
- async for response in lite_llm_instance.generate_content_async(llm_request):
497
- assert response.content.role == "model"
498
- assert response.content.parts[0].text == '{"result": "test_result"}'
499
-
500
- mock_acompletion.assert_called_once()
501
-
502
- _, kwargs = mock_acompletion.call_args
503
- assert kwargs["model"] == "test_model"
504
-
505
- assert kwargs["messages"][2]["role"] == "tool"
506
- assert kwargs["messages"][2]["content"] == '{"result": "test_result"}'
507
-
508
-
509
- def test_content_to_message_param_user_message():
510
- content = types.Content(
511
- role="user", parts=[types.Part.from_text(text="Test prompt")]
512
- )
513
- message = _content_to_message_param(content)
514
- assert message["role"] == "user"
515
- assert message["content"] == "Test prompt"
516
-
517
-
518
- def test_content_to_message_param_assistant_message():
519
- content = types.Content(
520
- role="assistant", parts=[types.Part.from_text(text="Test response")]
521
- )
522
- message = _content_to_message_param(content)
523
- assert message["role"] == "assistant"
524
- assert message["content"] == "Test response"
525
-
526
-
527
- def test_content_to_message_param_function_call():
528
- content = types.Content(
529
- role="assistant",
530
- parts=[
531
- types.Part.from_function_call(
532
- name="test_function", args={"test_arg": "test_value"}
533
- )
534
- ],
535
- )
536
- content.parts[0].function_call.id = "test_tool_call_id"
537
- message = _content_to_message_param(content)
538
- assert message["role"] == "assistant"
539
- assert message["content"] == []
540
- assert message["tool_calls"][0].type == "function"
541
- assert message["tool_calls"][0].id == "test_tool_call_id"
542
- assert message["tool_calls"][0].function.name == "test_function"
543
- assert (
544
- message["tool_calls"][0].function.arguments
545
- == '{"test_arg": "test_value"}'
546
- )
547
-
548
-
549
- def test_message_to_generate_content_response_text():
550
- message = ChatCompletionAssistantMessage(
551
- role="assistant",
552
- content="Test response",
553
- )
554
- response = _message_to_generate_content_response(message)
555
- assert response.content.role == "model"
556
- assert response.content.parts[0].text == "Test response"
557
-
558
-
559
- def test_message_to_generate_content_response_tool_call():
560
- message = ChatCompletionAssistantMessage(
561
- role="assistant",
562
- content=None,
563
- tool_calls=[
564
- ChatCompletionMessageToolCall(
565
- type="function",
566
- id="test_tool_call_id",
567
- function=Function(
568
- name="test_function",
569
- arguments='{"test_arg": "test_value"}',
570
- ),
571
- )
572
- ],
573
- )
574
-
575
- response = _message_to_generate_content_response(message)
576
- assert response.content.role == "model"
577
- assert response.content.parts[0].function_call.name == "test_function"
578
- assert response.content.parts[0].function_call.args == {
579
- "test_arg": "test_value"
580
- }
581
- assert response.content.parts[0].function_call.id == "test_tool_call_id"
582
-
583
-
584
- def test_get_content_text():
585
- parts = [types.Part.from_text(text="Test text")]
586
- content = _get_content(parts)
587
- assert content == "Test text"
588
-
589
-
590
- def test_get_content_image():
591
- parts = [
592
- types.Part.from_bytes(data=b"test_image_data", mime_type="image/png")
593
- ]
594
- content = _get_content(parts)
595
- assert content[0]["type"] == "image_url"
596
- assert content[0]["image_url"] == "data:image/png;base64,dGVzdF9pbWFnZV9kYXRh"
597
-
598
-
599
- def test_get_content_video():
600
- parts = [
601
- types.Part.from_bytes(data=b"test_video_data", mime_type="video/mp4")
602
- ]
603
- content = _get_content(parts)
604
- assert content[0]["type"] == "video_url"
605
- assert content[0]["video_url"] == "data:video/mp4;base64,dGVzdF92aWRlb19kYXRh"
606
-
607
-
608
- def test_to_litellm_role():
609
- assert _to_litellm_role("model") == "assistant"
610
- assert _to_litellm_role("assistant") == "assistant"
611
- assert _to_litellm_role("user") == "user"
612
- assert _to_litellm_role(None) == "user"
613
-
614
-
615
- @pytest.mark.parametrize(
616
- "response, expected_chunk, expected_finished",
617
- [
618
- (
619
- ModelResponse(
620
- choices=[
621
- {
622
- "message": {
623
- "content": "this is a test",
624
- }
625
- }
626
- ]
627
- ),
628
- TextChunk(text="this is a test"),
629
- "stop",
630
- ),
631
- (
632
- ModelResponse(
633
- choices=[
634
- StreamingChoices(
635
- finish_reason=None,
636
- delta=Delta(
637
- role="assistant",
638
- tool_calls=[
639
- ChatCompletionDeltaToolCall(
640
- type="function",
641
- id="1",
642
- function=Function(
643
- name="test_function",
644
- arguments='{"key": "va',
645
- ),
646
- index=0,
647
- )
648
- ],
649
- ),
650
- )
651
- ]
652
- ),
653
- FunctionChunk(id="1", name="test_function", args='{"key": "va'),
654
- None,
655
- ),
656
- (
657
- ModelResponse(choices=[{"finish_reason": "tool_calls"}]),
658
- None,
659
- "tool_calls",
660
- ),
661
- (ModelResponse(choices=[{}]), None, "stop"),
662
- ],
663
- )
664
- def test_model_response_to_chunk(response, expected_chunk, expected_finished):
665
- result = list(_model_response_to_chunk(response))
666
- assert len(result) == 1
667
- chunk, finished = result[0]
668
- if expected_chunk:
669
- assert isinstance(chunk, type(expected_chunk))
670
- assert chunk == expected_chunk
671
- else:
672
- assert chunk is None
673
- assert finished == expected_finished
674
-
675
-
676
- @pytest.mark.asyncio
677
- async def test_acompletion_additional_args(mock_acompletion, mock_client):
678
- lite_llm_instance = LiteLlm(
679
- # valid args
680
- model="test_model",
681
- llm_client=mock_client,
682
- api_key="test_key",
683
- api_base="some://url",
684
- api_version="2024-09-12",
685
- # invalid args (ignored)
686
- stream=True,
687
- messages=[{"role": "invalid", "content": "invalid"}],
688
- tools=[{
689
- "type": "function",
690
- "function": {
691
- "name": "invalid",
692
- },
693
- }],
694
- )
695
-
696
- async for response in lite_llm_instance.generate_content_async(
697
- LLM_REQUEST_WITH_FUNCTION_DECLARATION
698
- ):
699
- assert response.content.role == "model"
700
- assert response.content.parts[0].text == "Test response"
701
- assert response.content.parts[1].function_call.name == "test_function"
702
- assert response.content.parts[1].function_call.args == {
703
- "test_arg": "test_value"
704
- }
705
- assert response.content.parts[1].function_call.id == "test_tool_call_id"
706
-
707
- mock_acompletion.assert_called_once()
708
-
709
- _, kwargs = mock_acompletion.call_args
710
-
711
- assert kwargs["model"] == "test_model"
712
- assert kwargs["messages"][0]["role"] == "user"
713
- assert kwargs["messages"][0]["content"] == "Test prompt"
714
- assert kwargs["tools"][0]["function"]["name"] == "test_function"
715
- assert "stream" not in kwargs
716
- assert "llm_client" not in kwargs
717
- assert kwargs["api_base"] == "some://url"
718
-
719
-
720
- @pytest.mark.asyncio
721
- async def test_completion_additional_args(mock_completion, mock_client):
722
- lite_llm_instance = LiteLlm(
723
- # valid args
724
- model="test_model",
725
- llm_client=mock_client,
726
- api_key="test_key",
727
- api_base="some://url",
728
- api_version="2024-09-12",
729
- # invalid args (ignored)
730
- stream=False,
731
- messages=[{"role": "invalid", "content": "invalid"}],
732
- tools=[{
733
- "type": "function",
734
- "function": {
735
- "name": "invalid",
736
- },
737
- }],
738
- )
739
-
740
- mock_completion.return_value = iter(STREAMING_MODEL_RESPONSE)
741
-
742
- responses = [
743
- response
744
- async for response in lite_llm_instance.generate_content_async(
745
- LLM_REQUEST_WITH_FUNCTION_DECLARATION, stream=True
746
- )
747
- ]
748
- assert len(responses) == 4
749
- mock_completion.assert_called_once()
750
-
751
- _, kwargs = mock_completion.call_args
752
-
753
- assert kwargs["model"] == "test_model"
754
- assert kwargs["messages"][0]["role"] == "user"
755
- assert kwargs["messages"][0]["content"] == "Test prompt"
756
- assert kwargs["tools"][0]["function"]["name"] == "test_function"
757
- assert kwargs["stream"]
758
- assert "llm_client" not in kwargs
759
- assert kwargs["api_base"] == "some://url"
760
-
761
-
762
- @pytest.mark.asyncio
763
- async def test_generate_content_async_stream(
764
- mock_completion, lite_llm_instance
765
- ):
766
-
767
- mock_completion.return_value = iter(STREAMING_MODEL_RESPONSE)
768
-
769
- responses = [
770
- response
771
- async for response in lite_llm_instance.generate_content_async(
772
- LLM_REQUEST_WITH_FUNCTION_DECLARATION, stream=True
773
- )
774
- ]
775
- assert len(responses) == 4
776
- assert responses[0].content.role == "model"
777
- assert responses[0].content.parts[0].text == "zero, "
778
- assert responses[1].content.role == "model"
779
- assert responses[1].content.parts[0].text == "one, "
780
- assert responses[2].content.role == "model"
781
- assert responses[2].content.parts[0].text == "two:"
782
- assert responses[3].content.role == "model"
783
- assert responses[3].content.parts[0].function_call.name == "test_function"
784
- assert responses[3].content.parts[0].function_call.args == {
785
- "test_arg": "test_value"
786
- }
787
- assert responses[3].content.parts[0].function_call.id == "test_tool_call_id"
788
- mock_completion.assert_called_once()
789
-
790
- _, kwargs = mock_completion.call_args
791
- assert kwargs["model"] == "test_model"
792
- assert kwargs["messages"][0]["role"] == "user"
793
- assert kwargs["messages"][0]["content"] == "Test prompt"
794
- assert kwargs["tools"][0]["function"]["name"] == "test_function"
795
- assert (
796
- kwargs["tools"][0]["function"]["description"]
797
- == "Test function description"
798
- )
799
- assert (
800
- kwargs["tools"][0]["function"]["parameters"]["properties"]["test_arg"][
801
- "type"
802
- ]
803
- == "string"
804
- )