25 lines
798 B
Python
25 lines
798 B
Python
import requests
|
|
|
|
from llm_server import opts
|
|
from llm_server.logging import create_logger
|
|
|
|
_logger = create_logger('moderation')
|
|
|
|
|
|
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:
|
|
_logger.error(f'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
|