Spaces:
Sleeping
Sleeping
Update application/chat_inference.py
Browse files- application/chat_inference.py +79 -56
application/chat_inference.py
CHANGED
|
@@ -1,70 +1,93 @@
|
|
|
|
|
|
|
|
| 1 |
from application.utils.chat_completion_api import ChatCompletionAPI
|
| 2 |
-
from config import Response,pipeline_dict,convs_dict
|
| 3 |
import os
|
| 4 |
from application.utils.image_captioning import ImageCaptioning
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class ChatInference:
|
| 7 |
def __init__(self):
|
| 8 |
self.chatCompletionAPI = ChatCompletionAPI()
|
| 9 |
self.image_captioning = ImageCaptioning()
|
|
|
|
| 10 |
|
| 11 |
-
def validate(self,data,user):
|
| 12 |
-
try:
|
| 13 |
-
pipeline = pipeline_dict['api']['models']
|
| 14 |
-
model = data['model']
|
| 15 |
-
self.headers = pipeline[model]['headers']
|
| 16 |
-
self.updateHeaders = {}
|
| 17 |
-
for header in self.headers:
|
| 18 |
-
if(header=="config"):
|
| 19 |
-
for configHeader in self.headers[header]:
|
| 20 |
-
if(configHeader=="Authorization"):
|
| 21 |
-
auth = self.headers[header][configHeader].split(' ')
|
| 22 |
-
self.updateHeaders[configHeader] = f"{auth[0]} {eval(auth[1])}"
|
| 23 |
-
elif(configHeader=="comment"):
|
| 24 |
-
pass
|
| 25 |
-
else:
|
| 26 |
-
self.updateHeaders[configHeader] = f"{eval(self.headers[header][configHeader])}"
|
| 27 |
-
else:
|
| 28 |
-
self.updateHeaders[header] = self.headers[header]
|
| 29 |
-
prompt = data['prompt']
|
| 30 |
-
max_tokens = data.get('max_token', 10020)
|
| 31 |
-
temperature = max(0, min(data.get('temperature', 0.7), 2))
|
| 32 |
-
top_p = max(0.1, min(data.get('top_p', 0.9), 1))
|
| 33 |
-
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')
|
| 34 |
-
convId = data['convId']
|
| 35 |
-
image = data.get('image')
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
if(pipeline[model]['type'] == 'image-text-to-text'):
|
| 49 |
-
convs_dict[user][convId]['messages'].append({"role": "user", "content": [{"type":"text","text":prompt}]})
|
| 50 |
-
else:
|
| 51 |
-
convs_dict[user][convId]['messages'].append({"role":"user","content":prompt})
|
| 52 |
-
transformed = {
|
| 53 |
-
"model": model,
|
| 54 |
-
"prompt": prompt,
|
| 55 |
-
"messages": convs_dict[user][convId]['messages'],
|
| 56 |
-
"max_tokens": max_tokens,
|
| 57 |
-
"temperature": temperature,
|
| 58 |
-
"top_p": top_p,
|
| 59 |
-
"stream": True
|
| 60 |
-
}
|
| 61 |
-
data.update(transformed)
|
| 62 |
-
return data
|
| 63 |
-
except KeyError:
|
| 64 |
-
return 400
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# application/chat_inference.py
|
| 2 |
+
import time # Import the time module
|
| 3 |
from application.utils.chat_completion_api import ChatCompletionAPI
|
| 4 |
+
from config import Response, pipeline_dict, convs_dict
|
| 5 |
import os
|
| 6 |
from application.utils.image_captioning import ImageCaptioning
|
| 7 |
+
from application.utils.web_search import WebScarper
|
| 8 |
+
from application.utils.image_generation import generate_image # Import
|
| 9 |
+
|
| 10 |
|
| 11 |
class ChatInference:
|
| 12 |
def __init__(self):
|
| 13 |
self.chatCompletionAPI = ChatCompletionAPI()
|
| 14 |
self.image_captioning = ImageCaptioning()
|
| 15 |
+
self.web_scraper = WebScarper()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def validate(self, data, user):
|
| 19 |
+
try:
|
| 20 |
+
pipeline = pipeline_dict['api']['models']
|
| 21 |
+
model = data['model']
|
| 22 |
+
self.headers = pipeline[model]['headers']
|
| 23 |
+
self.updateHeaders = {}
|
| 24 |
+
for header in self.headers:
|
| 25 |
+
if(header=="config"):
|
| 26 |
+
for configHeader in self.headers[header]:
|
| 27 |
+
if(configHeader=="Authorization"):
|
| 28 |
+
auth = self.headers[header][configHeader].split(' ')
|
| 29 |
+
self.updateHeaders[configHeader] = f"{auth[0]} {eval(auth[1])}" # Directly evaluate
|
| 30 |
+
elif(configHeader=="comment"):
|
| 31 |
+
pass
|
| 32 |
+
else:
|
| 33 |
+
self.updateHeaders[configHeader] = f"{eval(self.headers[header][configHeader])}" # Directly evaluate
|
| 34 |
+
else:
|
| 35 |
+
self.updateHeaders[header] = self.headers[header]
|
| 36 |
+
prompt = data['prompt']
|
| 37 |
+
max_tokens = data.get('max_token', 10020)
|
| 38 |
+
temperature = max(0, min(data.get('temperature', 0.7), 2))
|
| 39 |
+
top_p = max(0.1, min(data.get('top_p', 0.9), 1))
|
| 40 |
+
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')
|
| 41 |
+
convId = data['convId']
|
| 42 |
+
image = data.get('image')
|
| 43 |
|
| 44 |
+
if(len(convs_dict[user][convId]['messages'])==1):
|
| 45 |
+
if system:
|
| 46 |
+
# Include user memory in the system prompt
|
| 47 |
+
system_prompt = f"{system}\n\nMemory: {convs_dict[user]['memory']}"
|
| 48 |
+
convs_dict[user][convId]['messages'][0]['content'] = system_prompt # Update existing system message
|
| 49 |
|
| 50 |
+
convs_dict[user]['metadata'].insert(0,{"convId": convId, "title": prompt[:23]})
|
| 51 |
+
convs_dict[user][convId]['title'] = prompt[:30]
|
| 52 |
+
if image:
|
| 53 |
+
caption = self.image_captioning.generate_caption(image)
|
| 54 |
+
prompt = f"{caption}\n\n{prompt}"
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
if(pipeline[model]['type'] == 'image-text-to-text'):
|
| 58 |
+
convs_dict[user][convId]['messages'].append({"role": "user", "content": [{"type":"text","text":prompt}]})
|
| 59 |
+
else:
|
| 60 |
+
convs_dict[user][convId]['messages'].append({"role":"user","content":prompt}) # Append user message
|
| 61 |
+
transformed = {
|
| 62 |
+
"model": model,
|
| 63 |
+
"prompt": prompt,
|
| 64 |
+
"messages": convs_dict[user][convId]['messages'],
|
| 65 |
+
"max_tokens": max_tokens,
|
| 66 |
+
"temperature": temperature,
|
| 67 |
+
"top_p": top_p,
|
| 68 |
+
"stream": True
|
| 69 |
+
}
|
| 70 |
+
data.update(transformed)
|
| 71 |
+
return data
|
| 72 |
+
except KeyError as e:
|
| 73 |
+
print(f"KeyError: {e}") # Debugging
|
| 74 |
+
return 400
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"An unexpected error occurred: {e}") # Debugging
|
| 77 |
+
return 500
|
| 78 |
+
|
| 79 |
+
def chat(self, data, handle_stream, user):
|
| 80 |
+
start_time = time.time() # Capture start time
|
| 81 |
+
data = self.validate(data=data, user=user)
|
| 82 |
+
if isinstance(data, int): # Check for error codes
|
| 83 |
+
return "Required Parameters are Missing!", data
|
| 84 |
+
|
| 85 |
+
return self.chatCompletionAPI.make_request(
|
| 86 |
+
json=data,
|
| 87 |
+
url=data['base_url'],
|
| 88 |
+
handle_stream=handle_stream,
|
| 89 |
+
messages=data['messages'],
|
| 90 |
+
headers=self.updateHeaders,
|
| 91 |
+
webSearch=data['webSearch'],
|
| 92 |
+
start_time = start_time
|
| 93 |
+
)
|