This repository has been archived on 2024-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
local-llm-server/llm_server/llm/openai/moderation.py

22 lines
704 B
Python
Raw Normal View History

2023-09-26 22:09:11 -06:00
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:
2023-10-04 10:32:11 -06:00
print('moderation failed:', response)
2023-09-26 22:09:11 -06:00
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