22 lines
704 B
Python
22 lines
704 B
Python
import requests
|
|
|
|
from llm_server import opts
|
|
|
|
|
|
def check_moderation_endpoint(prompt: str):
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': f"Bearer {opts.openai_api_key}",
|
|
}
|
|
response = requests.post('https://api.openai.com/v1/moderations', headers=headers, json={"input": prompt}, timeout=10)
|
|
if response.status_code != 200:
|
|
print('moderation failed:', response)
|
|
response.raise_for_status()
|
|
response = response.json()
|
|
|
|
offending_categories = []
|
|
for k, v in response['results'][0]['categories'].items():
|
|
if v:
|
|
offending_categories.append(k)
|
|
return response['results'][0]['flagged'], offending_categories
|