forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_global_instruction_plugin.py
More file actions
208 lines (160 loc) · 6.8 KB
/
test_global_instruction_plugin.py
File metadata and controls
208 lines (160 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import Mock
from google.adk.agents.callback_context import CallbackContext
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import Agent
from google.adk.agents.readonly_context import ReadonlyContext
from google.adk.models.llm_request import LlmRequest
from google.adk.plugins.global_instruction_plugin import GlobalInstructionPlugin
from google.adk.sessions.session import Session
from google.genai import types
import pytest
@pytest.mark.asyncio
async def test_global_instruction_plugin_with_string():
"""Test GlobalInstructionPlugin with a string global instruction."""
plugin = GlobalInstructionPlugin(
global_instruction=(
"You are a helpful assistant with a friendly personality."
)
)
# Create mock objects
mock_session = Session(
app_name="test_app", user_id="test_user", id="test_session", state={}
)
mock_invocation_context = Mock(spec=InvocationContext)
mock_invocation_context.session = mock_session
mock_callback_context = Mock(spec=CallbackContext)
mock_callback_context._invocation_context = mock_invocation_context
llm_request = LlmRequest(
model="gemini-1.5-flash",
config=types.GenerateContentConfig(system_instruction=""),
)
# Execute the plugin's before_model_callback
result = await plugin.before_model_callback(
callback_context=mock_callback_context, llm_request=llm_request
)
# Plugin should return None to allow normal processing
assert result is None
# System instruction should now contain the global instruction
assert (
"You are a helpful assistant with a friendly personality."
in llm_request.config.system_instruction
)
@pytest.mark.asyncio
async def test_global_instruction_plugin_with_instruction_provider():
"""Test GlobalInstructionPlugin with an InstructionProvider function."""
async def build_global_instruction(readonly_context: ReadonlyContext) -> str:
return f"You are assistant for user {readonly_context.session.user_id}."
plugin = GlobalInstructionPlugin(global_instruction=build_global_instruction)
# Create mock objects
mock_session = Session(
app_name="test_app", user_id="alice", id="test_session", state={}
)
mock_invocation_context = Mock(spec=InvocationContext)
mock_callback_context = Mock(spec=CallbackContext)
mock_callback_context._invocation_context = mock_invocation_context
mock_callback_context.session = mock_session
llm_request = LlmRequest(
model="gemini-1.5-flash",
config=types.GenerateContentConfig(system_instruction=""),
)
# Execute the plugin's before_model_callback
result = await plugin.before_model_callback(
callback_context=mock_callback_context, llm_request=llm_request
)
# Plugin should return None to allow normal processing
assert result is None
# System instruction should contain the dynamically generated instruction
assert (
"You are assistant for user alice."
in llm_request.config.system_instruction
)
@pytest.mark.asyncio
async def test_global_instruction_plugin_empty_instruction():
"""Test GlobalInstructionPlugin with empty global instruction."""
plugin = GlobalInstructionPlugin(global_instruction="")
# Create mock objects
mock_session = Session(
app_name="test_app", user_id="test_user", id="test_session", state={}
)
mock_invocation_context = Mock(spec=InvocationContext)
mock_invocation_context.session = mock_session
mock_callback_context = Mock(spec=CallbackContext)
mock_callback_context._invocation_context = mock_invocation_context
llm_request = LlmRequest(
model="gemini-1.5-flash",
config=types.GenerateContentConfig(
system_instruction="Original instruction"
),
)
# Execute the plugin's before_model_callback
result = await plugin.before_model_callback(
callback_context=mock_callback_context, llm_request=llm_request
)
# Plugin should return None to allow normal processing
assert result is None
# System instruction should remain unchanged
assert llm_request.config.system_instruction == "Original instruction"
@pytest.mark.asyncio
async def test_global_instruction_plugin_leads_existing():
"""Test that GlobalInstructionPlugin prepends global instructions."""
plugin = GlobalInstructionPlugin(
global_instruction="You are a helpful assistant."
)
# Create mock objects
mock_session = Session(
app_name="test_app", user_id="test_user", id="test_session", state={}
)
mock_invocation_context = Mock(spec=InvocationContext)
mock_invocation_context.session = mock_session
mock_callback_context = Mock(spec=CallbackContext)
mock_callback_context._invocation_context = mock_invocation_context
llm_request = LlmRequest(
model="gemini-1.5-flash",
config=types.GenerateContentConfig(
system_instruction="Existing instructions."
),
)
# Execute the plugin's before_model_callback
result = await plugin.before_model_callback(
callback_context=mock_callback_context, llm_request=llm_request
)
# Plugin should return None to allow normal processing
assert result is None
# System instruction should contain global instruction before existing ones
expected = "You are a helpful assistant.\n\nExisting instructions."
assert llm_request.config.system_instruction == expected
@pytest.mark.asyncio
async def test_global_instruction_plugin_prepends_to_list():
"""Test GlobalInstructionPlugin prepends to a list of instructions."""
plugin = GlobalInstructionPlugin(global_instruction="Global instruction.")
mock_session = Session(
app_name="test_app", user_id="test_user", id="test_session", state={}
)
mock_invocation_context = Mock(spec=InvocationContext)
mock_invocation_context.session = mock_session
mock_callback_context = Mock(spec=CallbackContext)
mock_callback_context._invocation_context = mock_invocation_context
llm_request = LlmRequest(
model="gemini-1.5-flash",
config=types.GenerateContentConfig(
system_instruction=["Existing instruction."]
),
)
await plugin.before_model_callback(
callback_context=mock_callback_context, llm_request=llm_request
)
expected = ["Global instruction.", "Existing instruction."]
assert llm_request.config.system_instruction == expected