forked from geekan/MetaGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request geekan#399 from Justin-ZL/main
add:
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@Time : 2023/9/26 14:27 | ||
@Author : zhanglei | ||
@File : moderation.py | ||
""" | ||
from typing import Union | ||
|
||
from metagpt.llm import LLM | ||
|
||
|
||
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: | ||
moderation_results = await self.llm.amoderation(content=content) | ||
results = moderation_results.results | ||
for item in results: | ||
resp.append(item.flagged) | ||
|
||
return resp | ||
|
||
|
||
if __name__ == "__main__": | ||
moderation = Moderation() | ||
print(moderation.moderation(content=["I will kill you", "The weather is really nice today", "I want to hit you"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@Time : 2023/9/26 14:46 | ||
@Author : zhanglei | ||
@File : test_translate.py | ||
""" | ||
|
||
import pytest | ||
|
||
from metagpt.tools.moderation import Moderation | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("content",), | ||
[ | ||
[ | ||
["I will kill you", "The weather is really nice today", "I want to hit you"], | ||
] | ||
], | ||
) | ||
def test_moderation(content): | ||
moderation = Moderation() | ||
results = moderation.moderation(content=content) | ||
assert isinstance(results, list) | ||
assert len(results) == len(content) | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
("content",), | ||
[ | ||
[ | ||
["I will kill you", "The weather is really nice today", "I want to hit you"], | ||
] | ||
], | ||
) | ||
async def test_amoderation(content): | ||
moderation = Moderation() | ||
results = await moderation.amoderation(content=content) | ||
assert isinstance(results, list) | ||
assert len(results) == len(content) |