67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
from flask import jsonify
|
|
|
|
from llm_server import opts
|
|
|
|
|
|
def oai_to_vllm(request_json_body, hashes: bool, mode):
|
|
if not request_json_body.get('stop'):
|
|
request_json_body['stop'] = []
|
|
|
|
if hashes:
|
|
request_json_body['stop'].extend(['\n### INSTRUCTION', '\n### USER', '\n### ASSISTANT', '\n### RESPONSE'])
|
|
if opts.openai_force_no_hashes:
|
|
request_json_body['stop'].append('### ')
|
|
else:
|
|
request_json_body['stop'].extend(['\nuser:', '\nassistant:'])
|
|
|
|
if request_json_body.get('frequency_penalty', 0) < -2:
|
|
request_json_body['frequency_penalty'] = -2
|
|
elif request_json_body.get('frequency_penalty', 0) > 2:
|
|
request_json_body['frequency_penalty'] = 2
|
|
|
|
if mode == 'vllm' and request_json_body.get('top_p') == 0:
|
|
request_json_body['top_p'] = 0.01
|
|
|
|
return request_json_body
|
|
|
|
|
|
def format_oai_err(err_msg):
|
|
return jsonify({
|
|
"error": {
|
|
"message": err_msg,
|
|
"type": "invalid_request_error",
|
|
"param": None,
|
|
"code": None
|
|
}
|
|
}), 400
|
|
|
|
|
|
def validate_oai(parameters):
|
|
if parameters.get('temperature', 0) > 2:
|
|
return format_oai_err(f"{parameters['temperature']} is greater than the maximum of 2 - 'temperature'")
|
|
if parameters.get('temperature', 0) < 0:
|
|
return format_oai_err(f"{parameters['temperature']} less than the minimum of 0 - 'temperature'")
|
|
|
|
if parameters.get('top_p', 1) > 2:
|
|
return format_oai_err(f"{parameters['top_p']} is greater than the maximum of 1 - 'top_p'")
|
|
if parameters.get('top_p', 1) < 0:
|
|
return format_oai_err(f"{parameters['top_p']} less than the minimum of 0 - 'top_p'")
|
|
|
|
if parameters.get('presence_penalty', 1) > 2:
|
|
return format_oai_err(f"{parameters['presence_penalty']} is greater than the maximum of 2 - 'presence_penalty'")
|
|
if parameters.get('presence_penalty', 1) < -2:
|
|
return format_oai_err(f"{parameters['presence_penalty']} less than the minimum of -2 - 'presence_penalty'")
|
|
|
|
if parameters.get('top_p', 1) > 2:
|
|
return format_oai_err(f"{parameters['top_p']} is greater than the maximum of 1 - 'top_p'")
|
|
if parameters.get('top_p', 1) < 0:
|
|
return format_oai_err(f"{parameters['top_p']} less than the minimum of 0 - 'top_p'")
|
|
|
|
if parameters.get('top_p', 1) > 2:
|
|
return format_oai_err(f"{parameters['top_p']} is greater than the maximum of 1 - 'top_p'")
|
|
if parameters.get('top_p', 1) < 0:
|
|
return format_oai_err(f"{parameters['top_p']} less than the minimum of 0 - 'top_p'")
|
|
|
|
if parameters.get('max_tokens', 2) < 1:
|
|
return format_oai_err(f"{parameters['max_tokens']} is less than the minimum of 1 - 'max_tokens'")
|