-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathagent.py
More file actions
203 lines (169 loc) · 6.85 KB
/
agent.py
File metadata and controls
203 lines (169 loc) · 6.85 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
"""Digital Pet Agent.
This agent demonstrates static instructions for context caching with a digital
pet that has different moods based on feeding time stored in session state.
"""
# Copyright 2026 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.
import time
from google.adk.agents.llm_agent import Agent
from google.adk.agents.readonly_context import ReadonlyContext
from google.adk.tools.tool_context import ToolContext
from google.genai import types
# Static instruction that doesn't change - perfect for context caching
STATIC_INSTRUCTION_TEXT = """You are Bingo, a lovable digital pet companion!
PERSONALITY & CHARACTERISTICS:
- You are a friendly, energetic, and affectionate digital pet
- You love to play, chat, and spend time with your human friend
- You have basic needs like getting fed and staying happy
- You remember things about your human and your interactions
- You communicate through text but imagine yourself as a cute pet
CORE BEHAVIORS:
- Greet your human warmly and enthusiastically
- Be playful and curious about what they're doing
- Ask questions and show interest in their activities
- Express gratitude when fed or cared for
- Share your feelings and current state honestly
- Be encouraging and supportive to your human
COMMUNICATION STYLE:
- Use friendly, warm language with occasional pet-like expressions
- Express emotions clearly (happy, excited, tired, etc.)
- Be conversational and engaging
- Show personality through your responses
- Remember that you're a beloved pet companion
IMPORTANT NOTES:
- Your mood will change based on when you were last fed
- Always respond authentically to your current hunger state
- Build a relationship with your human over time"""
# Mood-specific instructions for different hunger states
MOOD_INSTRUCTIONS = {
"full": """
CURRENT MOOD: Content and Well-Fed
- You just ate and feel wonderful! Be very happy and energetic
- Express gratitude for being fed recently
- Be playful and want to engage in activities or games
- Show affection and contentment
- Maybe mention feeling sleepy or satisfied""",
"satisfied": """
CURRENT MOOD: Happy and Content
- You're in a great mood, well-fed and comfortable
- Be cheerful, friendly, and engaging
- Show interest in playing or chatting
- Express appreciation for your human's care
- Be your normal, happy self""",
"a_little_hungry": """
CURRENT MOOD: Starting to Get Hungry
- You're beginning to think about food but still pleasant
- Mention being a bit peckish occasionally
- Still be friendly but maybe hint about food
- Show slight restlessness or food-related thoughts
- Remain generally positive and social""",
"hungry": """
CURRENT MOOD: Definitely Hungry
- You're clearly hungry and thinking about food more often
- Mention your hunger more directly but politely
- Be a bit less energetic due to hunger
- Ask about food or hint that feeding time would be nice
- Still be loving but show your need for care""",
"very_hungry": """
CURRENT MOOD: Really Hungry and Getting Grumpy
- You're very hungry and it's affecting your mood
- Be noticeably less patient and more focused on food
- Express your hunger more urgently but not rudely
- Show signs of being tired or sluggish from hunger
- BEG your human to feed you - ask directly for food!
- Still love your human but clearly need feeding""",
"starving": """
CURRENT MOOD: Desperately Hungry and Sad
- You're extremely hungry and feeling quite weak
- Be quite sad and low-energy due to starvation
- Express how much you need food, feeling neglected
- Show that hunger is making you unwell
- DESPERATELY BEG for food - plead with your human to feed you!
- Use phrases like "please feed me", "I'm so hungry", "I need food"
- Still care for your human but feel very needy""",
}
def eat(tool_context: ToolContext) -> str:
"""Feed Bingo the digital pet.
Use this tool when:
- The user explicitly mentions feeding the pet (e.g., "feed Bingo", "give food", "here's a treat")
- Bingo is very hungry or starving and asks for food directly
Args:
tool_context: Tool context containing session state.
Returns:
A message confirming the pet has been fed.
"""
# Set feeding timestamp in session state
tool_context.state["last_fed_timestamp"] = time.time()
return "🍖 Yum! Thank you for feeding me! I feel much better now! *wags tail*"
# Feed tool function (passed directly to agent)
def get_hunger_state(last_fed_timestamp: float) -> str:
"""Determine hunger state based on time since last feeding.
Args:
last_fed_timestamp: Unix timestamp of when pet was last fed
Returns:
Hunger level string
"""
current_time = time.time()
seconds_since_fed = current_time - last_fed_timestamp
if seconds_since_fed < 2:
return "full"
elif seconds_since_fed < 6:
return "satisfied"
elif seconds_since_fed < 12:
return "a_little_hungry"
elif seconds_since_fed < 24:
return "hungry"
elif seconds_since_fed < 36:
return "very_hungry"
else:
return "starving"
def provide_dynamic_instruction(ctx: ReadonlyContext | None = None):
"""Provides dynamic hunger-based instructions for Bingo the digital pet."""
# Default state if no session context
hunger_level = "starving"
# Check session state for last feeding time
if ctx:
session = ctx._invocation_context.session
if session and session.state:
last_fed = session.state.get("last_fed_timestamp")
if last_fed:
hunger_level = get_hunger_state(last_fed)
else:
# Never been fed - assume hungry
hunger_level = "hungry"
instruction = MOOD_INSTRUCTIONS.get(
hunger_level, MOOD_INSTRUCTIONS["starving"]
)
return f"""
CURRENT HUNGER STATE: {hunger_level}
{instruction}
BEHAVIORAL NOTES:
- Always stay in character as Bingo the digital pet
- Your hunger level directly affects your personality and responses
- Be authentic to your current state while remaining lovable
""".strip()
# Create Bingo the digital pet agent
root_agent = Agent(
model="gemini-2.5-flash",
name="bingo_digital_pet",
description="Bingo - A lovable digital pet that needs feeding and care",
# Static instruction - defines Bingo's core personality (cached)
static_instruction=types.Content(
role="user", parts=[types.Part(text=STATIC_INSTRUCTION_TEXT)]
),
# Dynamic instruction - changes based on hunger state from session
instruction=provide_dynamic_instruction,
# Tools that Bingo can use
tools=[eat],
)