From 1463b7d8a6eef40d8202a9f9b3f708240aa6e0a4 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Fri, 15 Sep 2023 10:02:15 +0800 Subject: [PATCH] update: moderation add asyncio func impl --- metagpt/provider/openai_api.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 624388d35..7e865f288 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -6,7 +6,7 @@ """ import asyncio import time -from typing import List, NamedTuple, Union +from typing import NamedTuple, Union import openai from openai.error import APIConnectionError @@ -296,7 +296,7 @@ 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]]): + def moderation(self, content: Union[str, list[str]]): try: if not content: logger.error("content cannot be empty!") @@ -306,6 +306,20 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): except Exception as e: logger.error(f"moderating failed:{e}") - def _moderation(self, content: Union[str]): + def _moderation(self, content: Union[str, list[str]]): rsp = self.llm.Moderation.create(input=content) return rsp + + 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.llm.Moderation.acreate(input=content) + return rsp