|
| 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 | +import asyncio |
| 17 | +import time |
| 18 | + |
| 19 | +import agent |
| 20 | +from dotenv import load_dotenv |
| 21 | +from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService |
| 22 | +from google.adk.cli.utils import logs |
| 23 | +from google.adk.runners import Runner |
| 24 | +from google.adk.sessions.database_session_service import DatabaseSessionService |
| 25 | +from google.adk.sessions.session import Session |
| 26 | +from google.genai import types |
| 27 | + |
| 28 | +load_dotenv(override=True) |
| 29 | +logs.log_to_tmp_folder() |
| 30 | + |
| 31 | + |
| 32 | +async def main(): |
| 33 | + app_name = 'migrate_session_db_app' |
| 34 | + user_id_1 = 'user1' |
| 35 | + session_service = DatabaseSessionService('sqlite:///./sessions.db') |
| 36 | + artifact_service = InMemoryArtifactService() |
| 37 | + runner = Runner( |
| 38 | + app_name=app_name, |
| 39 | + agent=agent.root_agent, |
| 40 | + artifact_service=artifact_service, |
| 41 | + session_service=session_service, |
| 42 | + ) |
| 43 | + session_11 = await session_service.get_session( |
| 44 | + app_name=app_name, |
| 45 | + user_id=user_id_1, |
| 46 | + session_id='aee03f34-32ef-432b-b1bb-e66a3a79dd5b', |
| 47 | + ) |
| 48 | + print('Session 11 loaded:', session_11.id) |
| 49 | + |
| 50 | + async def run_prompt(session: Session, new_message: str): |
| 51 | + content = types.Content( |
| 52 | + role='user', parts=[types.Part.from_text(text=new_message)] |
| 53 | + ) |
| 54 | + print('** User says:', content.model_dump(exclude_none=True)) |
| 55 | + async for event in runner.run_async( |
| 56 | + user_id=user_id_1, |
| 57 | + session_id=session.id, |
| 58 | + new_message=content, |
| 59 | + ): |
| 60 | + if event.content.parts and event.content.parts[0].text: |
| 61 | + print(f'** {event.author}: {event.content.parts[0].text}') |
| 62 | + |
| 63 | + start_time = time.time() |
| 64 | + print('Start time:', start_time) |
| 65 | + print('------------------------------------') |
| 66 | + await run_prompt(session_11, 'Hi, introduce yourself.') |
| 67 | + await run_prompt( |
| 68 | + session_11, 'Roll a die with 100 sides and check if it is prime' |
| 69 | + ) |
| 70 | + await run_prompt(session_11, 'Roll it again.') |
| 71 | + await run_prompt(session_11, 'What numbers did I got?') |
| 72 | + end_time = time.time() |
| 73 | + print('------------------------------------') |
| 74 | + print('End time:', end_time) |
| 75 | + print('Total time:', end_time - start_time) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + asyncio.run(main()) |
0 commit comments