google-adk 0.0.1__py3-none-any.whl → 0.0.2__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.
- google/adk/__init__.py +20 -0
- google/adk/agents/__init__.py +32 -0
- google/adk/agents/active_streaming_tool.py +38 -0
- google/adk/agents/base_agent.py +345 -0
- google/adk/agents/callback_context.py +112 -0
- google/adk/agents/invocation_context.py +181 -0
- google/adk/agents/langgraph_agent.py +140 -0
- google/adk/agents/live_request_queue.py +64 -0
- google/adk/agents/llm_agent.py +376 -0
- google/adk/agents/loop_agent.py +62 -0
- google/adk/agents/parallel_agent.py +96 -0
- google/adk/agents/readonly_context.py +46 -0
- google/adk/agents/remote_agent.py +50 -0
- google/adk/agents/run_config.py +87 -0
- google/adk/agents/sequential_agent.py +45 -0
- google/adk/agents/transcription_entry.py +34 -0
- google/adk/artifacts/__init__.py +23 -0
- google/adk/artifacts/base_artifact_service.py +128 -0
- google/adk/artifacts/gcs_artifact_service.py +195 -0
- google/adk/artifacts/in_memory_artifact_service.py +133 -0
- google/adk/auth/__init__.py +22 -0
- google/adk/auth/auth_credential.py +220 -0
- google/adk/auth/auth_handler.py +268 -0
- google/adk/auth/auth_preprocessor.py +116 -0
- google/adk/auth/auth_schemes.py +67 -0
- google/adk/auth/auth_tool.py +55 -0
- google/adk/cli/__init__.py +15 -0
- google/adk/cli/__main__.py +18 -0
- google/adk/cli/agent_graph.py +122 -0
- google/adk/cli/browser/adk_favicon.svg +17 -0
- google/adk/cli/browser/assets/audio-processor.js +51 -0
- google/adk/cli/browser/assets/config/runtime-config.json +3 -0
- google/adk/cli/browser/index.html +33 -0
- google/adk/cli/browser/main-XUU6OGCC.js +75 -0
- google/adk/cli/browser/polyfills-FFHMD2TL.js +18 -0
- google/adk/cli/browser/styles-4VDSPQ37.css +17 -0
- google/adk/cli/cli.py +181 -0
- google/adk/cli/cli_deploy.py +181 -0
- google/adk/cli/cli_eval.py +282 -0
- google/adk/cli/cli_tools_click.py +479 -0
- google/adk/cli/fast_api.py +774 -0
- google/adk/cli/media_streamer/__init__.py +19 -0
- google/adk/cli/media_streamer/index.html +228 -0
- google/adk/cli/utils/__init__.py +49 -0
- google/adk/cli/utils/envs.py +57 -0
- google/adk/cli/utils/evals.py +93 -0
- google/adk/cli/utils/logs.py +72 -0
- google/adk/code_executors/__init__.py +49 -0
- google/adk/code_executors/base_code_executor.py +97 -0
- google/adk/code_executors/code_execution_utils.py +256 -0
- google/adk/code_executors/code_executor_context.py +202 -0
- google/adk/code_executors/container_code_executor.py +196 -0
- google/adk/code_executors/unsafe_local_code_executor.py +71 -0
- google/adk/code_executors/vertex_ai_code_executor.py +234 -0
- google/adk/evaluation/__init__.py +31 -0
- google/adk/evaluation/agent_evaluator.py +329 -0
- google/adk/evaluation/evaluation_constants.py +24 -0
- google/adk/evaluation/evaluation_generator.py +270 -0
- google/adk/evaluation/response_evaluator.py +135 -0
- google/adk/evaluation/trajectory_evaluator.py +184 -0
- google/adk/events/__init__.py +21 -0
- google/adk/events/event.py +130 -0
- google/adk/events/event_actions.py +55 -0
- google/adk/examples/__init__.py +28 -0
- google/adk/examples/base_example_provider.py +35 -0
- google/adk/examples/example.py +27 -0
- google/adk/examples/example_util.py +123 -0
- google/adk/examples/vertex_ai_example_store.py +104 -0
- google/adk/flows/__init__.py +14 -0
- google/adk/flows/llm_flows/__init__.py +20 -0
- google/adk/flows/llm_flows/_base_llm_processor.py +52 -0
- google/adk/flows/llm_flows/_code_execution.py +458 -0
- google/adk/flows/llm_flows/_nl_planning.py +129 -0
- google/adk/flows/llm_flows/agent_transfer.py +132 -0
- google/adk/flows/llm_flows/audio_transcriber.py +109 -0
- google/adk/flows/llm_flows/auto_flow.py +49 -0
- google/adk/flows/llm_flows/base_llm_flow.py +559 -0
- google/adk/flows/llm_flows/basic.py +72 -0
- google/adk/flows/llm_flows/contents.py +370 -0
- google/adk/flows/llm_flows/functions.py +486 -0
- google/adk/flows/llm_flows/identity.py +47 -0
- google/adk/flows/llm_flows/instructions.py +137 -0
- google/adk/flows/llm_flows/single_flow.py +57 -0
- google/adk/memory/__init__.py +35 -0
- google/adk/memory/base_memory_service.py +74 -0
- google/adk/memory/in_memory_memory_service.py +62 -0
- google/adk/memory/vertex_ai_rag_memory_service.py +177 -0
- google/adk/models/__init__.py +31 -0
- google/adk/models/anthropic_llm.py +243 -0
- google/adk/models/base_llm.py +87 -0
- google/adk/models/base_llm_connection.py +76 -0
- google/adk/models/gemini_llm_connection.py +200 -0
- google/adk/models/google_llm.py +331 -0
- google/adk/models/lite_llm.py +673 -0
- google/adk/models/llm_request.py +98 -0
- google/adk/models/llm_response.py +111 -0
- google/adk/models/registry.py +102 -0
- google/adk/planners/__init__.py +23 -0
- google/adk/planners/base_planner.py +66 -0
- google/adk/planners/built_in_planner.py +75 -0
- google/adk/planners/plan_re_act_planner.py +208 -0
- google/adk/runners.py +456 -0
- google/adk/sessions/__init__.py +41 -0
- google/adk/sessions/base_session_service.py +133 -0
- google/adk/sessions/database_session_service.py +522 -0
- google/adk/sessions/in_memory_session_service.py +206 -0
- google/adk/sessions/session.py +54 -0
- google/adk/sessions/state.py +71 -0
- google/adk/sessions/vertex_ai_session_service.py +356 -0
- google/adk/telemetry.py +189 -0
- google/adk/tests/__init__.py +14 -0
- google/adk/tests/integration/.env.example +10 -0
- google/adk/tests/integration/__init__.py +18 -0
- google/adk/tests/integration/conftest.py +119 -0
- google/adk/tests/integration/fixture/__init__.py +14 -0
- google/adk/tests/integration/fixture/agent_with_config/__init__.py +15 -0
- google/adk/tests/integration/fixture/agent_with_config/agent.py +88 -0
- google/adk/tests/integration/fixture/callback_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/callback_agent/agent.py +105 -0
- google/adk/tests/integration/fixture/context_update_test/OWNERS +1 -0
- google/adk/tests/integration/fixture/context_update_test/__init__.py +15 -0
- google/adk/tests/integration/fixture/context_update_test/agent.py +43 -0
- google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +582 -0
- google/adk/tests/integration/fixture/context_variable_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/context_variable_agent/agent.py +115 -0
- google/adk/tests/integration/fixture/customer_support_ma/__init__.py +15 -0
- google/adk/tests/integration/fixture/customer_support_ma/agent.py +172 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +338 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +69 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +6 -0
- google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +15 -0
- google/adk/tests/integration/fixture/flow_complex_spark/agent.py +182 -0
- google/adk/tests/integration/fixture/flow_complex_spark/sample.debug.log +243 -0
- google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +190 -0
- google/adk/tests/integration/fixture/hello_world_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/hello_world_agent/agent.py +95 -0
- google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +24 -0
- google/adk/tests/integration/fixture/hello_world_agent/test_config.json +6 -0
- google/adk/tests/integration/fixture/home_automation_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/home_automation_agent/agent.py +304 -0
- google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_config.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +18 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +17 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +6 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +18 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +17 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +5 -0
- google/adk/tests/integration/fixture/tool_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/tool_agent/agent.py +218 -0
- google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
- google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/trip_planner_agent/agent.py +110 -0
- google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +13 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +5 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +13 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +5 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +7 -0
- google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +19 -0
- google/adk/tests/integration/models/__init__.py +14 -0
- google/adk/tests/integration/models/test_google_llm.py +65 -0
- google/adk/tests/integration/test_callback.py +70 -0
- google/adk/tests/integration/test_context_variable.py +67 -0
- google/adk/tests/integration/test_evalute_agent_in_fixture.py +76 -0
- google/adk/tests/integration/test_multi_agent.py +28 -0
- google/adk/tests/integration/test_multi_turn.py +42 -0
- google/adk/tests/integration/test_single_agent.py +23 -0
- google/adk/tests/integration/test_sub_agent.py +26 -0
- google/adk/tests/integration/test_system_instruction.py +177 -0
- google/adk/tests/integration/test_tools.py +287 -0
- google/adk/tests/integration/test_with_test_file.py +34 -0
- google/adk/tests/integration/tools/__init__.py +14 -0
- google/adk/tests/integration/utils/__init__.py +16 -0
- google/adk/tests/integration/utils/asserts.py +75 -0
- google/adk/tests/integration/utils/test_runner.py +97 -0
- google/adk/tests/unittests/__init__.py +14 -0
- google/adk/tests/unittests/agents/__init__.py +14 -0
- google/adk/tests/unittests/agents/test_base_agent.py +407 -0
- google/adk/tests/unittests/agents/test_langgraph_agent.py +191 -0
- google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +138 -0
- google/adk/tests/unittests/agents/test_llm_agent_fields.py +231 -0
- google/adk/tests/unittests/agents/test_loop_agent.py +136 -0
- google/adk/tests/unittests/agents/test_parallel_agent.py +92 -0
- google/adk/tests/unittests/agents/test_sequential_agent.py +114 -0
- google/adk/tests/unittests/artifacts/__init__.py +14 -0
- google/adk/tests/unittests/artifacts/test_artifact_service.py +276 -0
- google/adk/tests/unittests/auth/test_auth_handler.py +575 -0
- google/adk/tests/unittests/conftest.py +73 -0
- google/adk/tests/unittests/fast_api/__init__.py +14 -0
- google/adk/tests/unittests/fast_api/test_fast_api.py +269 -0
- google/adk/tests/unittests/flows/__init__.py +14 -0
- google/adk/tests/unittests/flows/llm_flows/__init__.py +14 -0
- google/adk/tests/unittests/flows/llm_flows/_test_examples.py +142 -0
- google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +311 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +244 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +346 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +93 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +258 -0
- google/adk/tests/unittests/flows/llm_flows/test_identity.py +66 -0
- google/adk/tests/unittests/flows/llm_flows/test_instructions.py +164 -0
- google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +142 -0
- google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +46 -0
- google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +269 -0
- google/adk/tests/unittests/models/__init__.py +14 -0
- google/adk/tests/unittests/models/test_google_llm.py +224 -0
- google/adk/tests/unittests/models/test_litellm.py +804 -0
- google/adk/tests/unittests/models/test_models.py +60 -0
- google/adk/tests/unittests/sessions/__init__.py +14 -0
- google/adk/tests/unittests/sessions/test_session_service.py +227 -0
- google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +246 -0
- google/adk/tests/unittests/streaming/__init__.py +14 -0
- google/adk/tests/unittests/streaming/test_streaming.py +50 -0
- google/adk/tests/unittests/tools/__init__.py +14 -0
- google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +499 -0
- google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +204 -0
- google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +600 -0
- google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +630 -0
- google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +345 -0
- google/adk/tests/unittests/tools/google_api_tool/__init__.py +13 -0
- google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +657 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +145 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +68 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +153 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +196 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +573 -0
- google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +436 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +1367 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +628 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +139 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +406 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +966 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +201 -0
- google/adk/tests/unittests/tools/retrieval/__init__.py +14 -0
- google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +147 -0
- google/adk/tests/unittests/tools/test_agent_tool.py +167 -0
- google/adk/tests/unittests/tools/test_base_tool.py +141 -0
- google/adk/tests/unittests/tools/test_build_function_declaration.py +277 -0
- google/adk/tests/unittests/utils.py +304 -0
- google/adk/tools/__init__.py +51 -0
- google/adk/tools/_automatic_function_calling_util.py +346 -0
- google/adk/tools/agent_tool.py +176 -0
- google/adk/tools/apihub_tool/__init__.py +19 -0
- google/adk/tools/apihub_tool/apihub_toolset.py +209 -0
- google/adk/tools/apihub_tool/clients/__init__.py +13 -0
- google/adk/tools/apihub_tool/clients/apihub_client.py +332 -0
- google/adk/tools/apihub_tool/clients/secret_client.py +115 -0
- google/adk/tools/application_integration_tool/__init__.py +19 -0
- google/adk/tools/application_integration_tool/application_integration_toolset.py +230 -0
- google/adk/tools/application_integration_tool/clients/connections_client.py +903 -0
- google/adk/tools/application_integration_tool/clients/integration_client.py +253 -0
- google/adk/tools/base_tool.py +144 -0
- google/adk/tools/built_in_code_execution_tool.py +59 -0
- google/adk/tools/crewai_tool.py +72 -0
- google/adk/tools/example_tool.py +62 -0
- google/adk/tools/exit_loop_tool.py +23 -0
- google/adk/tools/function_parameter_parse_util.py +307 -0
- google/adk/tools/function_tool.py +87 -0
- google/adk/tools/get_user_choice_tool.py +28 -0
- google/adk/tools/google_api_tool/__init__.py +14 -0
- google/adk/tools/google_api_tool/google_api_tool.py +59 -0
- google/adk/tools/google_api_tool/google_api_tool_set.py +107 -0
- google/adk/tools/google_api_tool/google_api_tool_sets.py +55 -0
- google/adk/tools/google_api_tool/googleapi_to_openapi_converter.py +521 -0
- google/adk/tools/google_search_tool.py +68 -0
- google/adk/tools/langchain_tool.py +86 -0
- google/adk/tools/load_artifacts_tool.py +113 -0
- google/adk/tools/load_memory_tool.py +58 -0
- google/adk/tools/load_web_page.py +41 -0
- google/adk/tools/long_running_tool.py +39 -0
- google/adk/tools/mcp_tool/__init__.py +42 -0
- google/adk/tools/mcp_tool/conversion_utils.py +161 -0
- google/adk/tools/mcp_tool/mcp_tool.py +113 -0
- google/adk/tools/mcp_tool/mcp_toolset.py +272 -0
- google/adk/tools/openapi_tool/__init__.py +21 -0
- google/adk/tools/openapi_tool/auth/__init__.py +19 -0
- google/adk/tools/openapi_tool/auth/auth_helpers.py +498 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/__init__.py +25 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.py +105 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/base_credential_exchanger.py +55 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.py +117 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.py +97 -0
- google/adk/tools/openapi_tool/common/__init__.py +19 -0
- google/adk/tools/openapi_tool/common/common.py +300 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/__init__.py +32 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +231 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py +144 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py +260 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py +496 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py +268 -0
- google/adk/tools/preload_memory_tool.py +72 -0
- google/adk/tools/retrieval/__init__.py +36 -0
- google/adk/tools/retrieval/base_retrieval_tool.py +37 -0
- google/adk/tools/retrieval/files_retrieval.py +33 -0
- google/adk/tools/retrieval/llama_index_retrieval.py +41 -0
- google/adk/tools/retrieval/vertex_ai_rag_retrieval.py +107 -0
- google/adk/tools/tool_context.py +90 -0
- google/adk/tools/toolbox_tool.py +46 -0
- google/adk/tools/transfer_to_agent_tool.py +21 -0
- google/adk/tools/vertex_ai_search_tool.py +96 -0
- google/adk/version.py +16 -0
- google_adk-0.0.1.dist-info/LICENSE.txt → google_adk-0.0.2.dist-info/LICENSE +32 -0
- google_adk-0.0.2.dist-info/METADATA +73 -0
- google_adk-0.0.2.dist-info/RECORD +308 -0
- {google_adk-0.0.1.dist-info → google_adk-0.0.2.dist-info}/WHEEL +1 -2
- google_adk-0.0.2.dist-info/entry_points.txt +3 -0
- agent_kit/__init__.py +0 -0
- google_adk-0.0.1.dist-info/METADATA +0 -15
- google_adk-0.0.1.dist-info/RECORD +0 -6
- google_adk-0.0.1.dist-info/top_level.txt +0 -1
@@ -0,0 +1,130 @@
|
|
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
|
+
from __future__ import annotations
|
15
|
+
|
16
|
+
from datetime import datetime
|
17
|
+
import random
|
18
|
+
import string
|
19
|
+
from typing import Optional
|
20
|
+
|
21
|
+
from google.genai import types
|
22
|
+
from pydantic import ConfigDict
|
23
|
+
from pydantic import Field
|
24
|
+
|
25
|
+
from ..models.llm_response import LlmResponse
|
26
|
+
from .event_actions import EventActions
|
27
|
+
|
28
|
+
|
29
|
+
class Event(LlmResponse):
|
30
|
+
"""Represents an event in a conversation between agents and users.
|
31
|
+
|
32
|
+
It is used to store the content of the conversation, as well as the actions
|
33
|
+
taken by the agents like function calls, etc.
|
34
|
+
|
35
|
+
Attributes:
|
36
|
+
invocation_id: The invocation ID of the event.
|
37
|
+
author: "user" or the name of the agent, indicating who appended the event
|
38
|
+
to the session.
|
39
|
+
actions: The actions taken by the agent.
|
40
|
+
long_running_tool_ids: The ids of the long running function calls.
|
41
|
+
branch: The branch of the event.
|
42
|
+
id: The unique identifier of the event.
|
43
|
+
timestamp: The timestamp of the event.
|
44
|
+
is_final_response: Whether the event is the final response of the agent.
|
45
|
+
get_function_calls: Returns the function calls in the event.
|
46
|
+
"""
|
47
|
+
|
48
|
+
model_config = ConfigDict(
|
49
|
+
extra='forbid', ser_json_bytes='base64', val_json_bytes='base64'
|
50
|
+
)
|
51
|
+
|
52
|
+
# TODO: revert to be required after spark migration
|
53
|
+
invocation_id: str = ''
|
54
|
+
"""The invocation ID of the event."""
|
55
|
+
author: str
|
56
|
+
"""'user' or the name of the agent, indicating who appended the event to the
|
57
|
+
session."""
|
58
|
+
actions: EventActions = Field(default_factory=EventActions)
|
59
|
+
"""The actions taken by the agent."""
|
60
|
+
|
61
|
+
long_running_tool_ids: Optional[set[str]] = None
|
62
|
+
"""Set of ids of the long running function calls.
|
63
|
+
Agent client will know from this field about which function call is long running.
|
64
|
+
only valid for function call event
|
65
|
+
"""
|
66
|
+
branch: Optional[str] = None
|
67
|
+
"""The branch of the event.
|
68
|
+
|
69
|
+
The format is like agent_1.agent_2.agent_3, where agent_1 is the parent of
|
70
|
+
agent_2, and agent_2 is the parent of agent_3.
|
71
|
+
|
72
|
+
Branch is used when multiple sub-agent shouldn't see their peer agents'
|
73
|
+
conversaction history.
|
74
|
+
"""
|
75
|
+
|
76
|
+
# The following are computed fields.
|
77
|
+
# Do not assign the ID. It will be assigned by the session.
|
78
|
+
id: str = ''
|
79
|
+
"""The unique identifier of the event."""
|
80
|
+
timestamp: float = Field(default_factory=lambda: datetime.now().timestamp())
|
81
|
+
"""The timestamp of the event."""
|
82
|
+
|
83
|
+
def model_post_init(self, __context):
|
84
|
+
"""Post initialization logic for the event."""
|
85
|
+
# Generates a random ID for the event.
|
86
|
+
if not self.id:
|
87
|
+
self.id = Event.new_id()
|
88
|
+
|
89
|
+
def is_final_response(self) -> bool:
|
90
|
+
"""Returns whether the event is the final response of the agent."""
|
91
|
+
if self.actions.skip_summarization or self.long_running_tool_ids:
|
92
|
+
return True
|
93
|
+
return (
|
94
|
+
not self.get_function_calls()
|
95
|
+
and not self.get_function_responses()
|
96
|
+
and not self.partial
|
97
|
+
and not self.has_trailing_code_exeuction_result()
|
98
|
+
)
|
99
|
+
|
100
|
+
def get_function_calls(self) -> list[types.FunctionCall]:
|
101
|
+
"""Returns the function calls in the event."""
|
102
|
+
func_calls = []
|
103
|
+
if self.content and self.content.parts:
|
104
|
+
for part in self.content.parts:
|
105
|
+
if part.function_call:
|
106
|
+
func_calls.append(part.function_call)
|
107
|
+
return func_calls
|
108
|
+
|
109
|
+
def get_function_responses(self) -> list[types.FunctionResponse]:
|
110
|
+
"""Returns the function responses in the event."""
|
111
|
+
func_response = []
|
112
|
+
if self.content and self.content.parts:
|
113
|
+
for part in self.content.parts:
|
114
|
+
if part.function_response:
|
115
|
+
func_response.append(part.function_response)
|
116
|
+
return func_response
|
117
|
+
|
118
|
+
def has_trailing_code_exeuction_result(
|
119
|
+
self,
|
120
|
+
) -> bool:
|
121
|
+
"""Returns whether the event has a trailing code execution result."""
|
122
|
+
if self.content:
|
123
|
+
if self.content.parts:
|
124
|
+
return self.content.parts[-1].code_execution_result is not None
|
125
|
+
return False
|
126
|
+
|
127
|
+
@staticmethod
|
128
|
+
def new_id():
|
129
|
+
characters = string.ascii_letters + string.digits
|
130
|
+
return ''.join(random.choice(characters) for _ in range(8))
|
@@ -0,0 +1,55 @@
|
|
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
|
+
from __future__ import annotations
|
16
|
+
|
17
|
+
from typing import Optional
|
18
|
+
|
19
|
+
from pydantic import BaseModel
|
20
|
+
from pydantic import ConfigDict
|
21
|
+
from pydantic import Field
|
22
|
+
|
23
|
+
from ..auth.auth_tool import AuthConfig
|
24
|
+
|
25
|
+
|
26
|
+
class EventActions(BaseModel):
|
27
|
+
"""Represents the actions attached to an event."""
|
28
|
+
|
29
|
+
model_config = ConfigDict(extra='forbid')
|
30
|
+
|
31
|
+
skip_summarization: Optional[bool] = None
|
32
|
+
"""If true, it won't call model to summarize function response.
|
33
|
+
|
34
|
+
Only used for function_response event.
|
35
|
+
"""
|
36
|
+
|
37
|
+
state_delta: dict[str, object] = Field(default_factory=dict)
|
38
|
+
"""Indicates that the event is updating the state with the given delta."""
|
39
|
+
|
40
|
+
artifact_delta: dict[str, int] = Field(default_factory=dict)
|
41
|
+
"""Indicates that the event is updating an artifact. key is the filename,
|
42
|
+
value is the version."""
|
43
|
+
|
44
|
+
transfer_to_agent: Optional[str] = None
|
45
|
+
"""If set, the event transfers to the specified agent."""
|
46
|
+
|
47
|
+
escalate: Optional[bool] = None
|
48
|
+
"""The agent is escalating to a higher level agent."""
|
49
|
+
|
50
|
+
requested_auth_configs: dict[str, AuthConfig] = Field(default_factory=dict)
|
51
|
+
"""Will only be set by a tool response indicating tool request euc.
|
52
|
+
dict key is the function call id since one function call response (from model)
|
53
|
+
could correspond to multiple function calls.
|
54
|
+
dict value is the required auth config.
|
55
|
+
"""
|
@@ -0,0 +1,28 @@
|
|
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
|
+
from .base_example_provider import BaseExampleProvider
|
16
|
+
from .example import Example
|
17
|
+
|
18
|
+
__all__ = [
|
19
|
+
'BaseExampleProvider',
|
20
|
+
'Example',
|
21
|
+
]
|
22
|
+
|
23
|
+
try:
|
24
|
+
from .vertex_ai_example_store import VertexAiExampleStore
|
25
|
+
|
26
|
+
__all__.append('VertexAiExampleStore')
|
27
|
+
except ImportError:
|
28
|
+
pass
|
@@ -0,0 +1,35 @@
|
|
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
|
+
import abc
|
16
|
+
from .example import Example
|
17
|
+
|
18
|
+
|
19
|
+
# A class that provides examples for a given query.
|
20
|
+
class BaseExampleProvider(abc.ABC):
|
21
|
+
"""Base class for example providers.
|
22
|
+
|
23
|
+
This class defines the interface for providing examples for a given query.
|
24
|
+
"""
|
25
|
+
|
26
|
+
@abc.abstractmethod
|
27
|
+
def get_examples(self, query: str) -> list[Example]:
|
28
|
+
"""Returns a list of examples for a given query.
|
29
|
+
|
30
|
+
Args:
|
31
|
+
query: The query to get examples for.
|
32
|
+
|
33
|
+
Returns:
|
34
|
+
A list of Example objects.
|
35
|
+
"""
|
@@ -0,0 +1,27 @@
|
|
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
|
+
from google.genai import types
|
16
|
+
from pydantic import BaseModel
|
17
|
+
|
18
|
+
|
19
|
+
class Example(BaseModel):
|
20
|
+
"""A few-shot example.
|
21
|
+
|
22
|
+
Attributes:
|
23
|
+
input: The input content for the example.
|
24
|
+
output: The expected output content for the example.
|
25
|
+
"""
|
26
|
+
input: types.Content
|
27
|
+
output: list[types.Content]
|
@@ -0,0 +1,123 @@
|
|
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
|
+
"""Utility functions for converting examples to a string that can be used in system instructions in the prompt."""
|
16
|
+
|
17
|
+
import logging
|
18
|
+
from typing import Optional, Union
|
19
|
+
from typing import TYPE_CHECKING
|
20
|
+
|
21
|
+
from .base_example_provider import BaseExampleProvider
|
22
|
+
from .example import Example
|
23
|
+
|
24
|
+
if TYPE_CHECKING:
|
25
|
+
from ..sessions.session import Session
|
26
|
+
|
27
|
+
logger = logging.getLogger(__name__)
|
28
|
+
|
29
|
+
# Constant parts of the example string
|
30
|
+
_EXAMPLES_INTRO = (
|
31
|
+
"<EXAMPLES>\nBegin few-shot\nThe following are examples of user queries and"
|
32
|
+
" model responses using the available tools.\n\n"
|
33
|
+
)
|
34
|
+
_EXAMPLES_END = "End few-shot\n<EXAMPLES>"
|
35
|
+
_EXAMPLE_START = "EXAMPLE {}:\nBegin example\n"
|
36
|
+
_EXAMPLE_END = "End example\n\n"
|
37
|
+
_USER_PREFIX = "[user]\n"
|
38
|
+
_MODEL_PREFIX = "[model]\n"
|
39
|
+
_FUNCTION_PREFIX = "```\n"
|
40
|
+
_FUNCTION_CALL_PREFIX = "```tool_code\n"
|
41
|
+
_FUNCTION_CALL_SUFFIX = "\n```\n"
|
42
|
+
_FUNCTION_RESPONSE_PREFIX = "```tool_outputs\n"
|
43
|
+
_FUNCTION_RESPONSE_SUFFIX = "\n```\n"
|
44
|
+
|
45
|
+
|
46
|
+
# TODO(yaojie): Add unit tests for this function.
|
47
|
+
def convert_examples_to_text(
|
48
|
+
examples: list[Example], model: Optional[str]
|
49
|
+
) -> str:
|
50
|
+
"""Converts a list of examples to a string that can be used in a system instruction."""
|
51
|
+
examples_str = ""
|
52
|
+
for example_num, example in enumerate(examples):
|
53
|
+
output = f"{_EXAMPLE_START.format(example_num + 1)}{_USER_PREFIX}"
|
54
|
+
if example.input and example.input.parts:
|
55
|
+
output += (
|
56
|
+
"\n".join(part.text for part in example.input.parts if part.text)
|
57
|
+
+ "\n"
|
58
|
+
)
|
59
|
+
|
60
|
+
gemini2 = model is None or "gemini-2" in model
|
61
|
+
previous_role = None
|
62
|
+
for content in example.output:
|
63
|
+
role = _MODEL_PREFIX if content.role == "model" else _USER_PREFIX
|
64
|
+
if role != previous_role:
|
65
|
+
output += role
|
66
|
+
previous_role = role
|
67
|
+
for part in content.parts:
|
68
|
+
if part.function_call:
|
69
|
+
args = []
|
70
|
+
# Convert function call part to python-like function call
|
71
|
+
for k, v in part.function_call.args.items():
|
72
|
+
if isinstance(v, str):
|
73
|
+
args.append(f"{k}='{v}'")
|
74
|
+
else:
|
75
|
+
args.append(f"{k}={v}")
|
76
|
+
prefix = _FUNCTION_PREFIX if gemini2 else _FUNCTION_CALL_PREFIX
|
77
|
+
output += (
|
78
|
+
f"{prefix}{part.function_call.name}({', '.join(args)}){_FUNCTION_CALL_SUFFIX}"
|
79
|
+
)
|
80
|
+
# Convert function response part to json string
|
81
|
+
elif part.function_response:
|
82
|
+
prefix = _FUNCTION_PREFIX if gemini2 else _FUNCTION_RESPONSE_PREFIX
|
83
|
+
output += f"{prefix}{part.function_response.__dict__}{_FUNCTION_RESPONSE_SUFFIX}"
|
84
|
+
elif part.text:
|
85
|
+
output += f"{part.text}\n"
|
86
|
+
|
87
|
+
output += _EXAMPLE_END
|
88
|
+
examples_str += output
|
89
|
+
|
90
|
+
return f"{_EXAMPLES_INTRO}{examples_str}{_EXAMPLES_END}"
|
91
|
+
|
92
|
+
|
93
|
+
def _get_latest_message_from_user(session: "Session") -> str:
|
94
|
+
"""Gets the latest message from the user.
|
95
|
+
|
96
|
+
Returns:
|
97
|
+
The latest message from the user. If not found, returns an empty string.
|
98
|
+
"""
|
99
|
+
events = session.events
|
100
|
+
if not events:
|
101
|
+
return ""
|
102
|
+
|
103
|
+
event = events[-1]
|
104
|
+
if event.author == "user" and not event.get_function_responses():
|
105
|
+
if event.content.parts and event.content.parts[0].text:
|
106
|
+
return event.content.parts[0].text
|
107
|
+
else:
|
108
|
+
logger.warning("No message from user for fetching example.")
|
109
|
+
|
110
|
+
return ""
|
111
|
+
|
112
|
+
|
113
|
+
def build_example_si(
|
114
|
+
examples: Union[list[Example], BaseExampleProvider],
|
115
|
+
query: str,
|
116
|
+
model: Optional[str],
|
117
|
+
) -> str:
|
118
|
+
if isinstance(examples, list):
|
119
|
+
return convert_examples_to_text(examples, model)
|
120
|
+
if isinstance(examples, BaseExampleProvider):
|
121
|
+
return convert_examples_to_text(examples.get_examples(query), model)
|
122
|
+
|
123
|
+
raise ValueError("Invalid example configuration")
|
@@ -0,0 +1,104 @@
|
|
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
|
+
from google.genai import types
|
16
|
+
from typing_extensions import override
|
17
|
+
from vertexai.preview import example_stores
|
18
|
+
|
19
|
+
from .base_example_provider import BaseExampleProvider
|
20
|
+
from .example import Example
|
21
|
+
|
22
|
+
|
23
|
+
class VertexAiExampleStore(BaseExampleProvider):
|
24
|
+
"""Provides examples from Vertex example store."""
|
25
|
+
|
26
|
+
def __init__(self, examples_store_name: str):
|
27
|
+
"""Initializes the VertexAiExampleStore.
|
28
|
+
|
29
|
+
Args:
|
30
|
+
examples_store_name: The resource name of the vertex example store, in
|
31
|
+
the format of
|
32
|
+
``projects/{project}/locations/{location}/exampleStores/{example_store}``.
|
33
|
+
"""
|
34
|
+
self.examples_store_name = examples_store_name
|
35
|
+
|
36
|
+
@override
|
37
|
+
def get_examples(self, query: str) -> list[Example]:
|
38
|
+
example_store = example_stores.ExampleStore(self.examples_store_name)
|
39
|
+
# Retrieve relevant examples.
|
40
|
+
request = {
|
41
|
+
"stored_contents_example_parameters": {
|
42
|
+
"content_search_key": {
|
43
|
+
"contents": [{"role": "user", "parts": [{"text": query}]}],
|
44
|
+
"search_key_generation_method": {"last_entry": {}},
|
45
|
+
}
|
46
|
+
},
|
47
|
+
"top_k": 10,
|
48
|
+
"example_store": self.examples_store_name,
|
49
|
+
}
|
50
|
+
response = example_store.api_client.search_examples(request)
|
51
|
+
|
52
|
+
returned_examples = []
|
53
|
+
# Convert results to genai formats
|
54
|
+
for result in response.results:
|
55
|
+
if result.similarity_score < 0.5:
|
56
|
+
continue
|
57
|
+
expected_contents = [
|
58
|
+
content.content
|
59
|
+
for content in result.example.stored_contents_example.contents_example.expected_contents
|
60
|
+
]
|
61
|
+
expected_output = []
|
62
|
+
for content in expected_contents:
|
63
|
+
expected_parts = []
|
64
|
+
for part in content.parts:
|
65
|
+
if part.text:
|
66
|
+
expected_parts.append(types.Part.from_text(text=part.text))
|
67
|
+
elif part.function_call:
|
68
|
+
expected_parts.append(
|
69
|
+
types.Part.from_function_call(
|
70
|
+
name=part.function_call.name,
|
71
|
+
args={
|
72
|
+
key: value
|
73
|
+
for key, value in part.function_call.args.items()
|
74
|
+
},
|
75
|
+
)
|
76
|
+
)
|
77
|
+
elif part.function_response:
|
78
|
+
expected_parts.append(
|
79
|
+
types.Part.from_function_response(
|
80
|
+
name=part.function_response.name,
|
81
|
+
response={
|
82
|
+
key: value
|
83
|
+
for key, value in part.function_response.response.items()
|
84
|
+
},
|
85
|
+
)
|
86
|
+
)
|
87
|
+
expected_output.append(
|
88
|
+
types.Content(role=content.role, parts=expected_parts)
|
89
|
+
)
|
90
|
+
|
91
|
+
returned_examples.append(
|
92
|
+
Example(
|
93
|
+
input=types.Content(
|
94
|
+
role="user",
|
95
|
+
parts=[
|
96
|
+
types.Part.from_text(
|
97
|
+
text=result.example.stored_contents_example.search_key
|
98
|
+
)
|
99
|
+
],
|
100
|
+
),
|
101
|
+
output=expected_output,
|
102
|
+
)
|
103
|
+
)
|
104
|
+
return returned_examples
|
@@ -0,0 +1,14 @@
|
|
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
|
+
|
@@ -0,0 +1,20 @@
|
|
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
|
+
from . import _code_execution
|
16
|
+
from . import _nl_planning
|
17
|
+
from . import contents
|
18
|
+
from . import functions
|
19
|
+
from . import identity
|
20
|
+
from . import instructions
|
@@ -0,0 +1,52 @@
|
|
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
|
+
"""Defines the processor interface used for BaseLlmFlow."""
|
16
|
+
from __future__ import annotations
|
17
|
+
|
18
|
+
from abc import ABC
|
19
|
+
from abc import abstractmethod
|
20
|
+
from typing import AsyncGenerator
|
21
|
+
from typing import TYPE_CHECKING
|
22
|
+
|
23
|
+
from ...agents.invocation_context import InvocationContext
|
24
|
+
from ...events.event import Event
|
25
|
+
|
26
|
+
if TYPE_CHECKING:
|
27
|
+
from ...models.llm_request import LlmRequest
|
28
|
+
from ...models.llm_response import LlmResponse
|
29
|
+
|
30
|
+
|
31
|
+
class BaseLlmRequestProcessor(ABC):
|
32
|
+
"""Base class for LLM request processor."""
|
33
|
+
|
34
|
+
@abstractmethod
|
35
|
+
async def run_async(
|
36
|
+
self, invocation_context: InvocationContext, llm_request: LlmRequest
|
37
|
+
) -> AsyncGenerator[Event, None]:
|
38
|
+
"""Runs the processor."""
|
39
|
+
raise NotImplementedError("Not implemented.")
|
40
|
+
yield # AsyncGenerator requires a yield in function body.
|
41
|
+
|
42
|
+
|
43
|
+
class BaseLlmResponseProcessor(ABC):
|
44
|
+
"""Base class for LLM response processor."""
|
45
|
+
|
46
|
+
@abstractmethod
|
47
|
+
async def run_async(
|
48
|
+
self, invocation_context: InvocationContext, llm_response: LlmResponse
|
49
|
+
) -> AsyncGenerator[Event, None]:
|
50
|
+
"""Processes the LLM response."""
|
51
|
+
raise NotImplementedError("Not implemented.")
|
52
|
+
yield # AsyncGenerator requires a yield in function body.
|