Spaces:
Sleeping
Sleeping
Update application/chat_inference.py
Browse files- application/chat_inference.py +61 -61
application/chat_inference.py
CHANGED
|
@@ -1,61 +1,61 @@
|
|
| 1 |
-
from application.utils.chat_completion_api import ChatCompletionAPI
|
| 2 |
-
from config import Response,pipeline_dict,convs_dict
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
self.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
convs_dict[user][convId]['
|
| 39 |
-
|
| 40 |
-
convs_dict[user][convId]['messages'].append({"role":"user","content":prompt})
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 1 |
+
from application.utils.chat_completion_api import ChatCompletionAPI
|
| 2 |
+
from config import Response,pipeline_dict,convs_dict
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
class ChatInference:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.chatCompletionAPI = ChatCompletionAPI()
|
| 8 |
+
|
| 9 |
+
def validate(self,data,user):
|
| 10 |
+
try:
|
| 11 |
+
pipeline = pipeline_dict['api']['models']
|
| 12 |
+
model = data['model']
|
| 13 |
+
self.headers = pipeline[model]['headers']
|
| 14 |
+
self.updateHeaders = {}
|
| 15 |
+
for header in self.headers:
|
| 16 |
+
if(header=="config"):
|
| 17 |
+
for configHeader in self.headers[header]:
|
| 18 |
+
if(configHeader=="Authorization"):
|
| 19 |
+
auth = self.headers[header][configHeader].split(' ')
|
| 20 |
+
self.updateHeaders[configHeader] = f"{auth[0]} {eval(auth[1])}"
|
| 21 |
+
elif(configHeader=="comment"):
|
| 22 |
+
pass
|
| 23 |
+
else:
|
| 24 |
+
self.updateHeaders[configHeader] = f"{eval(self.headers[header][configHeader])}"
|
| 25 |
+
else:
|
| 26 |
+
self.updateHeaders[header] = self.headers[header]
|
| 27 |
+
prompt = data['prompt']
|
| 28 |
+
max_tokens = data.get('max_token', 10020)
|
| 29 |
+
temperature = max(0, min(data.get('temperature', 0.7), 2))
|
| 30 |
+
top_p = max(0.1, min(data.get('top_p', 0.9), 1))
|
| 31 |
+
system = data.get('system_prompt','You are a helpful and harmless AI assistant. You are xylaria made by sk md saad amin. You should think step-by-step')
|
| 32 |
+
convId = data['convId']
|
| 33 |
+
|
| 34 |
+
if(len(convs_dict[user][convId]['messages'])==1):
|
| 35 |
+
if system:
|
| 36 |
+
convs_dict[user][convId]['messages'].insert(0,{"role":"system", "content": system})
|
| 37 |
+
convs_dict[user]['metadata'].insert(0,{"convId": convId, "title": prompt[:23]})
|
| 38 |
+
convs_dict[user][convId]['title'] = prompt[:30]
|
| 39 |
+
if(pipeline[model]['type'] == 'image-text-to-text'):
|
| 40 |
+
convs_dict[user][convId]['messages'].append({"role": "user", "content": [{"type":"text","text":prompt}]})
|
| 41 |
+
else:
|
| 42 |
+
convs_dict[user][convId]['messages'].append({"role":"user","content":prompt})
|
| 43 |
+
transformed = {
|
| 44 |
+
"model": model,
|
| 45 |
+
"prompt": prompt,
|
| 46 |
+
"messages": convs_dict[user][convId]['messages'],
|
| 47 |
+
"max_tokens": max_tokens,
|
| 48 |
+
"temperature": temperature,
|
| 49 |
+
"top_p": top_p,
|
| 50 |
+
"stream": True
|
| 51 |
+
}
|
| 52 |
+
data.update(transformed)
|
| 53 |
+
return data
|
| 54 |
+
except KeyError:
|
| 55 |
+
return 400
|
| 56 |
+
|
| 57 |
+
def chat(self,data,handle_stream,user):
|
| 58 |
+
data = self.validate(data=data,user=user)
|
| 59 |
+
if(data==400):
|
| 60 |
+
return "Required Parameters are Missing!", 400
|
| 61 |
+
return self.chatCompletionAPI.make_request(json=data,url=data['base_url'],handle_stream=handle_stream,messages=data['messages'], headers=self.updateHeaders, webSearch=data['webSearch'])
|