From f364d10fb126fca1d2fe4b51d9ba059a5d615dd6 Mon Sep 17 00:00:00 2001 From: huhao0926 Date: Sun, 13 Jul 2025 16:58:46 +0800 Subject: [PATCH] fix(examples): update client usage to prefer await aclose() in async context" (#2839) --- examples/client/app_crud_example.py | 8 ++++++-- examples/client/client_chat_example.py | 8 ++++++-- examples/client/client_evaluation.py | 8 ++++++-- examples/client/datasource_crud_example.py | 8 ++++++-- examples/client/flow_crud_example.py | 8 ++++++-- examples/client/knowledge_crud_example.py | 24 +++++++++++++--------- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/examples/client/app_crud_example.py b/examples/client/app_crud_example.py index 4ac614717..712c090e3 100644 --- a/examples/client/app_crud_example.py +++ b/examples/client/app_crud_example.py @@ -22,8 +22,12 @@ async def main(): # initialize client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) - res = await list_app(client) - print(res) + try: + res = await list_app(client) + print(res) + finally: + # explicitly close client to avoid event loop closed error + await client.aclose() if __name__ == "__main__": diff --git a/examples/client/client_chat_example.py b/examples/client/client_chat_example.py index 61b6ea106..867ba6352 100644 --- a/examples/client/client_chat_example.py +++ b/examples/client/client_chat_example.py @@ -57,12 +57,16 @@ async def main(): # initialize client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) - data = await client.chat(model="Qwen2.5-72B-Instruct", messages="hello") + try: + data = await client.chat(model="Qwen2.5-72B-Instruct", messages="hello") + print(data) + finally: + # explicitly close client to avoid event loop closed error + await client.aclose() # async for data in client.chat_stream( # model="chatgpt_proxyllm", # messages="hello", # ): - print(data) # res = await client.chat(model="chatgpt_proxyllm" ,messages="hello") # print(res) diff --git a/examples/client/client_evaluation.py b/examples/client/client_evaluation.py index 6598aaa18..94f115bfd 100644 --- a/examples/client/client_evaluation.py +++ b/examples/client/client_evaluation.py @@ -83,8 +83,12 @@ async def main(): } ], ) - data = await run_evaluation(client, request=request) - print(data) + try: + data = await run_evaluation(client, request=request) + print(data) + finally: + # explicitly close client to avoid event loop closed error + await client.aclose() if __name__ == "__main__": diff --git a/examples/client/datasource_crud_example.py b/examples/client/datasource_crud_example.py index fbe3bd6d0..500d9e53f 100644 --- a/examples/client/datasource_crud_example.py +++ b/examples/client/datasource_crud_example.py @@ -55,8 +55,12 @@ async def main(): # initialize client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) - res = await list_datasource(client) - print(res) + try: + res = await list_datasource(client) + print(res) + finally: + # explicitly close client to avoid event loop closed error + await client.aclose() if __name__ == "__main__": diff --git a/examples/client/flow_crud_example.py b/examples/client/flow_crud_example.py index c406bec38..4c5c6e796 100644 --- a/examples/client/flow_crud_example.py +++ b/examples/client/flow_crud_example.py @@ -37,8 +37,12 @@ async def main(): # initialize client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) - res = await list_flow(client) - print(res) + try: + res = await list_flow(client) + print(res) + finally: + # explicitly close client to avoid event loop closed error + await client.aclose() if __name__ == "__main__": diff --git a/examples/client/knowledge_crud_example.py b/examples/client/knowledge_crud_example.py index 7c1a73ff0..9e753401a 100644 --- a/examples/client/knowledge_crud_example.py +++ b/examples/client/knowledge_crud_example.py @@ -75,16 +75,20 @@ async def main(): DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) - res = await create_space( - client, - SpaceModel( - name="test_space_1", - vector_type="Chroma", - desc="for client space desc", - owner="dbgpt", - ), - ) - print(res) + try: + res = await create_space( + client, + SpaceModel( + name="test_space_1", + vector_type="Chroma", + desc="for client space desc", + owner="dbgpt", + ), + ) + print(res) + finally: + # explicitly close client to avoid event loop closed error + await client.aclose() # list all spaces # res = await list_space(client)