From d46b7c4018b107d693937a0228ec43d761d66ae0 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 21 Dec 2023 14:45:53 +0800 Subject: [PATCH] fix moderation, remove claude from LLM, refine exceptions handler --- examples/llm_hello_world.py | 9 +++------ metagpt/provider/openai_api.py | 29 +++-------------------------- metagpt/tools/moderation.py | 22 ++++++++++------------ metagpt/utils/exceptions.py | 6 ++++-- 4 files changed, 20 insertions(+), 46 deletions(-) diff --git a/examples/llm_hello_world.py b/examples/llm_hello_world.py index 677098399..76be1cc90 100644 --- a/examples/llm_hello_world.py +++ b/examples/llm_hello_world.py @@ -7,23 +7,20 @@ """ import asyncio -from metagpt.llm import LLM, Claude +from metagpt.llm import LLM from metagpt.logs import logger async def main(): llm = LLM() - claude = Claude() - logger.info(await claude.aask("你好,请进行自我介绍")) logger.info(await llm.aask("hello world")) logger.info(await llm.aask_batch(["hi", "write python hello world."])) hello_msg = [{"role": "user", "content": "count from 1 to 10. split by newline."}] logger.info(await llm.acompletion(hello_msg)) - logger.info(await llm.acompletion_batch([hello_msg])) - logger.info(await llm.acompletion_batch_text([hello_msg])) - logger.info(await llm.acompletion_text(hello_msg)) + + # streaming mode, much slower await llm.acompletion_text(hello_msg, stream=True) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index dbafa31b7..b6c1fbe55 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -34,6 +34,7 @@ from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.constant import GENERAL_FUNCTION_SCHEMA, GENERAL_TOOL_CHOICE from metagpt.provider.llm_provider_registry import register_provider from metagpt.schema import Message +from metagpt.utils.exceptions import handle_exception from metagpt.utils.singleton import Singleton from metagpt.utils.token_counter import ( TOKEN_COSTS, @@ -420,30 +421,6 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return CONFIG.max_tokens_rsp return get_max_completion_tokens(messages, self.model, CONFIG.max_tokens_rsp) - def moderation(self, content: Union[str, list[str]]): - try: - if not content: - logger.error("content cannot be empty!") - else: - rsp = self._moderation(content=content) - return rsp - except Exception as e: - logger.error(f"moderating failed:{e}") - - def _moderation(self, content: Union[str, list[str]]): - rsp = self.client.moderations.create(input=content) - return rsp - + @handle_exception async def amoderation(self, content: Union[str, list[str]]): - try: - if not content: - logger.error("content cannot be empty!") - else: - rsp = await self._amoderation(content=content) - return rsp - except Exception as e: - logger.error(f"moderating failed:{e}") - - async def _amoderation(self, content: Union[str, list[str]]): - rsp = await self.async_client.moderations.create(input=content) - return rsp + return await self.async_client.moderations.create(input=content) diff --git a/metagpt/tools/moderation.py b/metagpt/tools/moderation.py index c56a6afc4..5532e4f66 100644 --- a/metagpt/tools/moderation.py +++ b/metagpt/tools/moderation.py @@ -5,6 +5,7 @@ @Author : zhanglei @File : moderation.py """ +import asyncio from typing import Union from metagpt.llm import LLM @@ -14,16 +15,6 @@ class Moderation: def __init__(self): self.llm = LLM() - def moderation(self, content: Union[str, list[str]]): - resp = [] - if content: - moderation_results = self.llm.moderation(content=content) - results = moderation_results.results - for item in results: - resp.append(item.flagged) - - return resp - async def amoderation(self, content: Union[str, list[str]]): resp = [] if content: @@ -35,6 +26,13 @@ class Moderation: return resp -if __name__ == "__main__": +async def main(): moderation = Moderation() - print(moderation.moderation(content=["I will kill you", "The weather is really nice today", "I want to hit you"])) + rsp = await moderation.amoderation( + content=["I will kill you", "The weather is really nice today", "I want to hit you"] + ) + print(rsp) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/metagpt/utils/exceptions.py b/metagpt/utils/exceptions.py index b4b5aa590..70ed45910 100644 --- a/metagpt/utils/exceptions.py +++ b/metagpt/utils/exceptions.py @@ -21,6 +21,7 @@ def handle_exception( _func: Callable[..., ReturnType] = None, *, exception_type: Union[Type[Exception], Tuple[Type[Exception], ...]] = Exception, + exception_msg: str = "", default_return: Any = None, ) -> Callable[..., ReturnType]: """handle exception, return default value""" @@ -32,8 +33,9 @@ def handle_exception( return await func(*args, **kwargs) except exception_type as e: logger.opt(depth=1).error( - f"Calling {func.__name__} with args: {args}, kwargs: {kwargs} failed: {e}, " - f"stack: {traceback.format_exc()}" + f"{e}: {exception_msg}, " + f"\nCalling {func.__name__} with args: {args}, kwargs: {kwargs} " + f"\nStack: {traceback.format_exc()}" ) return default_return