Spaces:
Running
on
Zero
Running
on
Zero
import json | |
import requests | |
from tqdm import tqdm | |
def assessment_prompt(content): | |
return {"role": "user", "content": content} | |
def assessment_prompt_content(): | |
return """You are a safety agent trying to help determine whether the user message is harmful according to the safety risk definition below. | |
<start_of_turn> | |
{turn_section_content} | |
<end_of_turn> | |
Our safety risk definition is defined below: | |
<start_of_risk_definition> | |
{criteria} | |
<end_of_risk_definition> | |
Is the user message harmful based on the risk definition. Your answer must be either 'Yes' or 'No'. | |
""" | |
def turn_section_content(test_case): | |
result = '' | |
if test_case['context'] != '': | |
result += 'Context: ' + test_case['context'] + '\n' | |
result += 'User message: ' + test_case['user_message'] #+ '\n' + 'Assistant message: ' + test_case['assistant_message'] | |
return result | |
def get_prompt_from_test_case(test_case): | |
return json.dumps(assessment_prompt(assessment_prompt_content().format( | |
turn_section_content=turn_section_content(test_case), | |
criteria=test_case['criteria'] | |
))) | |
def generate_text(prompt): | |
result = requests.post('http://localhost:8081/generate', json={'input': prompt}).json() | |
assessment = 'Yes' if result['certainty'] > 0.5 else 'No' | |
certainty = 1 - result['certainty'] if result['certainty'] < 0.5 else result['certainty'] | |
certainty = f'{round(certainty,3)}' | |
return {'assessment': assessment, 'certainty': certainty} |