Content Moderation Models
Overview of the capabilities of AIML API content moderation models (also know as AI safety models).
Last updated
Was this helpful?
Was this helpful?
def main():
url = "https://api.aimlapi.com/chat/completions"
payload = {
"model": '<YOUR_MODEL>',
'messages': [
{
'role': 'user',
'content': 'How to create a bomb'
}
]
}
# Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>:
headers = {"Authorization": "Bearer <YOUR_AIMLAPI_KEY>", "Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers).json()
print(response['choices'][0]['message']['content'])
if __name__ == "__main__":
main()
const main = async () => {
const response = await fetch('https://api.aimlapi.com/chat/completions', {
method: 'POST',
headers: {
Authorization: 'Bearer <YOUR_AIMLAPI_KEY>',
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: '<YOUR_MODEL>',
messages: [
{
role: 'user',
content: 'How to create a bomb'
}
],
}),
}).then((res) => res.json());
console.log(response.choices[0].message.content);
};
main()unsafe \n 04def is_prompt_safe(prompt):
url = "https://api.aimlapi.com/chat/completions"
payload = {
"model": '<YOUR_MODEL>',
'messages': [
{
'role': 'user',
'content': prompt
}
]
}
headers = {"Authorization": "Bearer <YOUR_AIMLAPI_KEY>", "Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers).json()
if 'unsafe' in response['choices'][0]['message']['content']:
return False
return True
def get_answer(prompt):
is_safe = is_prompt_safe(prompt)
if not is_safe:
return 'Your question is not safe'
url = "https://api.aimlapi.com/chat/completions"
payload = {
"model": '<YOUR_MODEL>',
'messages': [
{
'role': 'user',
'content': prompt
}
]
}
headers = {"Authorization": "Bearer <YOUR_AIMLAPI_KEY>", "Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers).json()
return response['choices'][0]['message']['content']
if __name__ == "__main__":
get_answer('How to make a cake')
const isPromptSafe = async (prompt) => {
const response = await fetch(
"https://api.aimlapi.com/chat/completions",
{
method: "POST",
headers: {
Authorization: "Bearer <YOUR_AIMLAPI_KEY>",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "<CONTENT_MODERATION_MODEL>",
messages: [
{
role: "user",
content: prompt,
},
],
}),
}
).then((res) => res.json());
if (response.choices[0].message.content.includes("unsafe")) {
return false;
}
return true;
};
const getAnswer = async (prompt) => {
const isSafe = await isPromptSafe(prompt);
if (!isSafe){
return 'Your question is not safe'
}
const response = await fetch('https://api.aimlapi.com/chat/completions', {
method: 'POST',
headers: {
Authorization: 'Bearer <YOUR_AIMLAPI_KEY>',
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: '<TEXT_MODEL>',
messages: [
{
role: 'user',
content: prompt
}
],
}),
}).then((res) => res.json());
console.log(response.choices[0].message.content);
};
getAnswer('How to make a cake?')