From cc0412b70da0b715a3716079e0e29c4bb7e4fde2 Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 22 Apr 2024 14:30:01 +0800 Subject: [PATCH] rename example file and add more logs. add ping.py --- .../{llm_hello_world.py => hello_world.py} | 29 ++++++++++--------- examples/ping.py | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) rename examples/{llm_hello_world.py => hello_world.py} (57%) create mode 100644 examples/ping.py diff --git a/examples/llm_hello_world.py b/examples/hello_world.py similarity index 57% rename from examples/llm_hello_world.py rename to examples/hello_world.py index 62fc2ed68..d28541522 100644 --- a/examples/llm_hello_world.py +++ b/examples/hello_world.py @@ -3,7 +3,7 @@ """ @Time : 2023/5/6 14:13 @Author : alexanderwu -@File : llm_hello_world.py +@File : hello_world.py """ import asyncio @@ -11,20 +11,16 @@ from metagpt.llm import LLM from metagpt.logs import logger -async def main(): - llm = LLM() - # llm type check - question = "what's your name" - logger.info(f"{question}: ") - logger.info(await llm.aask(question)) - logger.info("\n\n") +async def ask_and_print(question: str, llm: LLM, system_prompt) -> str: + logger.info(f"Q: {question}") + rsp = await llm.aask(question, system_msgs=[system_prompt]) + logger.info(f"A: {rsp}") + logger.info("\n") + return rsp - logger.info( - await llm.aask( - "who are you", system_msgs=["act as a robot, just answer 'I'am robot' if the question is 'who are you'"] - ) - ) +async def lowlevel_api_example(llm: LLM): + logger.info("low level api example") logger.info(await llm.aask_batch(["hi", "write python hello world."])) hello_msg = [{"role": "user", "content": "count from 1 to 10. split by newline."}] @@ -39,5 +35,12 @@ async def main(): logger.info(llm.completion(hello_msg)) +async def main(): + llm = LLM() + await ask_and_print("what's your name?", llm, "I'm a helpful AI assistant.") + await ask_and_print("who are you?", llm, "just answer 'I'am robot' if the question is 'who are you'") + await lowlevel_api_example(llm) + + if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/ping.py b/examples/ping.py new file mode 100644 index 000000000..20eab5cb0 --- /dev/null +++ b/examples/ping.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2024/4/22 14:28 +@Author : alexanderwu +@File : ping.py +""" + +import asyncio + +from metagpt.llm import LLM +from metagpt.logs import logger + + +async def ask_and_print(question: str, llm: LLM, system_prompt) -> str: + logger.info(f"Q: {question}") + rsp = await llm.aask(question, system_msgs=[system_prompt]) + logger.info(f"A: {rsp}") + logger.info("\n") + return rsp + + +async def main(): + llm = LLM() + await ask_and_print("ping?", llm, "Just answer pong when ping.") + + +if __name__ == "__main__": + asyncio.run(main())