mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-12-18 11:22:22 -06:00
feat(memory)!: Uses the new MemoryEntry schema for all memory related components.
BREAKING CHANGE. This commit changes all memory related interface to using the newly introduced MemoryEntry class. PiperOrigin-RevId: 758464887
This commit is contained in:
committed by
Copybara-Service
parent
825f5d4f2e
commit
30947b48b8
@@ -12,20 +12,31 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import random
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.agents.callback_context import CallbackContext
|
||||
from google.adk.tools.load_memory_tool import load_memory_tool
|
||||
from google.adk.tools.preload_memory_tool import preload_memory_tool
|
||||
from google.genai import types
|
||||
|
||||
|
||||
def update_current_time(callback_context: CallbackContext):
|
||||
callback_context.state['_time'] = datetime.now().isoformat()
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-2.0-flash-exp',
|
||||
model='gemini-2.0-flash-001',
|
||||
name='memory_agent',
|
||||
description='agent that have access to memory tools.',
|
||||
instruction="""
|
||||
You are an agent that help user answer questions.
|
||||
""",
|
||||
tools=[load_memory_tool, preload_memory_tool],
|
||||
before_agent_callback=update_current_time,
|
||||
instruction="""\
|
||||
You are an agent that help user answer questions.
|
||||
|
||||
Current time: {_time}
|
||||
""",
|
||||
tools=[
|
||||
load_memory_tool,
|
||||
preload_memory_tool,
|
||||
],
|
||||
)
|
||||
|
||||
111
contributing/samples/memory/asyncio_run.py
Executable file
111
contributing/samples/memory/asyncio_run.py
Executable file
@@ -0,0 +1,111 @@
|
||||
# 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.
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
from typing import cast
|
||||
import warnings
|
||||
|
||||
import agent
|
||||
from dotenv import load_dotenv
|
||||
from google.adk.cli.utils import logs
|
||||
from google.adk.runners import InMemoryRunner
|
||||
from google.adk.sessions import Session
|
||||
from google.genai import types
|
||||
|
||||
load_dotenv(override=True)
|
||||
warnings.filterwarnings('ignore', category=UserWarning)
|
||||
logs.log_to_tmp_folder()
|
||||
|
||||
|
||||
async def main():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
runner = InMemoryRunner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
)
|
||||
|
||||
async def run_prompt(session: Session, new_message: str) -> Session:
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if not event.content or not event.content.parts:
|
||||
continue
|
||||
if event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
elif event.content.parts[0].function_call:
|
||||
print(
|
||||
f'** {event.author}: fc /'
|
||||
f' {event.content.parts[0].function_call.name} /'
|
||||
f' {event.content.parts[0].function_call.args}\n'
|
||||
)
|
||||
elif event.content.parts[0].function_response:
|
||||
print(
|
||||
f'** {event.author}: fr /'
|
||||
f' {event.content.parts[0].function_response.name} /'
|
||||
f' {event.content.parts[0].function_response.response}\n'
|
||||
)
|
||||
|
||||
return cast(
|
||||
Session,
|
||||
runner.session_service.get_session(
|
||||
app_name=app_name, user_id=user_id_1, session_id=session.id
|
||||
),
|
||||
)
|
||||
|
||||
session_1 = runner.session_service.create_session(
|
||||
app_name=app_name, user_id=user_id_1
|
||||
)
|
||||
|
||||
print(f'----Session to create memory: {session_1.id} ----------------------')
|
||||
session_1 = await run_prompt(session_1, 'Hi')
|
||||
session_1 = await run_prompt(session_1, 'My name is Jack')
|
||||
session_1 = await run_prompt(session_1, 'I like badminton.')
|
||||
session_1 = await run_prompt(
|
||||
session_1,
|
||||
f'I ate a burger on {(datetime.now() - timedelta(days=1)).date()}.',
|
||||
)
|
||||
session_1 = await run_prompt(
|
||||
session_1,
|
||||
f'I ate a banana on {(datetime.now() - timedelta(days=2)).date()}.',
|
||||
)
|
||||
print('Saving session to memory service...')
|
||||
if runner.memory_service:
|
||||
await runner.memory_service.add_session_to_memory(session_1)
|
||||
print('-------------------------------------------------------------------')
|
||||
|
||||
session_2 = runner.session_service.create_session(
|
||||
app_name=app_name, user_id=user_id_1
|
||||
)
|
||||
print(f'----Session to use memory: {session_2.id} ----------------------')
|
||||
session_2 = await run_prompt(session_2, 'Hi')
|
||||
session_2 = await run_prompt(session_2, 'What do I like to do?')
|
||||
# ** memory_agent: You like badminton.
|
||||
session_2 = await run_prompt(session_2, 'When did I say that?')
|
||||
# ** memory_agent: You said you liked badminton on ...
|
||||
session_2 = await run_prompt(session_2, 'What did I eat yesterday?')
|
||||
# ** memory_agent: You ate a burger yesterday...
|
||||
print('-------------------------------------------------------------------')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user