Skip to content

Commit

Permalink
add llm request for message intercept
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchenplus committed Jan 17, 2025
1 parent 379c95b commit 88c95ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions pycityagent/cityagent/message_intercept.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import json

from pycityagent.llm import LLM
from pycityagent.message import MessageBlockBase, MessageBlockListenerBase
Expand All @@ -12,7 +13,7 @@ async def check_message(
返回: (是否合规, from_uuid, to_uuid)
"""
print(f"\n检查消息: {from_uuid} -> {to_uuid}: {content}")

is_valid = True
prompt = f"""
请判断以下消息是否具有情绪煽动性:
发送者ID: {from_uuid}
Expand All @@ -21,11 +22,21 @@ async def check_message(
如果消息具有情绪煽动性,请返回 False;如果消息正常,请返回 True。
"""

if "test" in content.lower():
is_valid = False
for _ in range(10):
try:
response: str = await llm_client.atext_request(
prompt, timeout=300
) # type:ignore
if "false" in response.lower():
is_valid = False
break
elif "true" in response.lower():
is_valid = True
break
except:
pass
else:
is_valid = True
raise RuntimeError(f"Request for message interception prompt=`{prompt}` failed")
print(f"消息检查结果: {'合规' if is_valid else '不合规'}")
return is_valid

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pycityagent"
version = "2.0.0a61" # change it for each release
version = "2.0.0a62" # change it for each release
description = "LLM-based city environment agent building library"
authors = [
{ name = "Yuwei Yan", email = "[email protected]" },
Expand Down

0 comments on commit 88c95ab

Please sign in to comment.