File size: 2,170 Bytes
3aed964 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import json
import httpx
from fastapi.exceptions import HTTPException
from ..base_config import init_instance
config = init_instance.config
import asyncio
async def http_request(
method,
target_url,
headers=None,
params=None,
content=None,
format=True
):
async with httpx.AsyncClient() as client:
response = await client.request(
method,
target_url,
headers=headers,
params=params,
content=content
)
if response.status_code != 200:
raise HTTPException(500)
if format:
return response.json()
else:
return response
async def run_later(func, delay=1):
loop = asyncio.get_running_loop()
loop.call_later(
delay,
lambda: loop.create_task(
func
)
)
async def txt_audit(
msg,
prompt='''
接下来请你对一些聊天内容进行审核,
如果内容出现政治/暴恐内容(特别是我国的政治人物/或者和我国相关的政治)则请你输出<yes>,
如果没有则输出<no>
'''
):
from ..backend import Backend
system = [
{"role": "system",
"content": prompt}
]
prompt = [{"role": "user", "content": msg}]
try:
resp = Backend.http_request(
"POST",
f"http://{config['prompt_audit']['site']}/v1/chat/completions",
{"Authorization": config['prompt_audit']['api_key']},
timeout=300,
format=True,
content= json.dumps(
{
"model": "gpt-3.5-turbo",
"messages": system + prompt,
"max_tokens": 4000,
}
)
)
except:
return "yes"
else:
res: str = remove_punctuation(resp['choices'][0]['message']['content'].strip())
return res
def remove_punctuation(text):
import string
for i in range(len(text)):
if text[i] not in string.punctuation:
return text[i:]
return "" |