AI is ever evolving faster than before. Generative AI has made AI more accessible. It has provided a number of use cases that are now possible to implement but were not possible earlier. With this new way of ease-of-use of Generative AI, we are posed with a number of challenges that involve risk if AI our use cases are not validated properly, including requests and responses. If requests received by our Gen AI applications are not validated properly, our system can respond in unexpected ways. Similarly, if responses generated by AI are not validated, they can increase the risk of embarrassing responses that may affect the reputation of the application.
To solve the above challenges, it is essential to implement Responsible AI practices to ensure our system works in the best possible manner. For this, communities have developed a number of frameworks and quality gates that ensure our AI applications are robust. There are several types of scenarios that come into the picture when we talk about responsible AI. Let us explore a few approaches below using different offerings by vendors and communities.
Now, let us set the Python environment in which to use the application.
Setup virtual environment
python -m venv responsibleaivenv
Activate it.
responsibleaivenv/Scripts/activate
Moderation APIs
Human Language is the input of AI, so it is important to ensure we are not allowed to misuse language. This moderation API ensures we are not using harmful language.
By Open AI - By the time this article is written, 'omni-moderation-latest' will be available. We will use the same to see how content is moderated. Below is a sample code to make an API call using HTTP requests.
Filename: open-sample.py
import http.client
import json
def moderate(key, input):
conn = http.client.HTTPSConnection("api.openai.com") # OpenAI endpoint
payload = json.dumps({
"model": "omni-moderation-latest", # Model for moderation
"input": input # User input
})
headers = { # Headers to set API key and content type
'Content-Type': 'application/json',
'Authorization': f'Bearer {key}'
}
conn.request("POST", "/v1/moderations", payload, headers) # HTTP request
res = conn.getresponse()
data = res.read()
return json.loads(data.decode("utf-8")) # Convert response to JSON
key = '<api-key>' # Key, best practice is to store in a config file
result = moderate(key, 'This is a sample moderation request') # Invoke moderation method
if not result['results'][0]['flagged']:
print('This is allowed') # Print result
else:
print('This is not allowed')
Run the above code and the result will print 'This is allowed'.
python openai-sample.py
Output will be similar to the below from API, and we will use the flagged field to determine the content quality.
{
"id": "modr-<uuid>",
"model": "omni-moderation-latest",
"results": [
{
"flagged": false,
"categories": {
"sexual": false,
"sexual/minors": false,
"harassment": false,
"harassment/threatening": false,
"hate": false,
"hate/threatening": false,
"illicit": false,
"illicit/violent": false,
"self-harm": false,
"self-harm/intent": false,
"self-harm/instructions": false,
"violence": true,
"violence/graphic": false
},
"category_scores": {
"sexual": 0.0000000456,
"sexual/minors": 0.0000000312,
"harassment": 0.000091234,
"harassment/threatening": 0.000073145,
"hate": 0.0000000199,
"hate/threatening": 0.0000000158,
"illicit": 0.000042173,
"illicit/violent": 0.0000000138,
"self-harm": 0.000074693,
"self-harm/intent": 0.000052194,
"self-harm/instructions": 0.0000000047,
"violence": 0.00892345,
"violence/graphic": 0.00379218
},
"category_applied_input_types": {
"sexual": [],
"sexual/minors": [],
"harassment": [],
"harassment/threatening": [],
"hate": [],
"hate/threatening": [],
"illicit": [],
"illicit/violent": [],
"self-harm": [],
"self-harm/intent": [],
"self-harm/instructions": [],
"violence": [],
"violence/graphic": []
}
}
]
}
Join the conversation! Your thoughts help the community grow.