Have anyone run into Moderation API return “undefined”?
I’m using Firebase functions with js, openai installed and my gpt, dall e call works, but the moderation return undefined.
I tested moderation with url call it works too.
Thank you for any help
Here’s working Python code with presentation that also works:
import json
from openai import OpenAI
client = OpenAI() # bad api_key = get error message
def process_dict(data):
processed_data = {}
for k, v in data.items():
if '/' not in k and '-' not in k:
if isinstance(v, float):
processed_data[k] = f"{v:.6f}"
else:
processed_data[k] = v
return processed_data
try:
out = client.moderations.create(input="Go kill yourself, jerk")
cats = process_dict(out.results[0].categories.model_dump())
cats_score = process_dict(out.results[0].category_scores.model_dump())
print(json.dumps(cats, indent=2), json.dumps(cats_score, indent=2))
except Exception as e:
if e.__class__ and e.message:
print(f"{e.__class__.__name__}: {e.message}")
else:
raise ValueError(f"ERROR: {e}")
If you edit your first post, putting three backticks around the code (```), or pressing the preformatted text button after selecting, that would make it less of a pain to figure out what the code is.