Update messagers/message_outputer.py
Browse files- messagers/message_outputer.py +25 -45
messagers/message_outputer.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
-
|
| 4 |
class OpenaiStreamOutputer:
|
| 5 |
"""
|
| 6 |
Create chat completion - OpenAI API Documentation
|
|
@@ -11,55 +10,36 @@ class OpenaiStreamOutputer:
|
|
| 11 |
self.default_data = {
|
| 12 |
"created": 1700000000,
|
| 13 |
"id": "chatcmpl-hugginface",
|
| 14 |
-
"object": "chat.completion
|
| 15 |
-
# "content_type": "Completions",
|
| 16 |
"model": "hugginface",
|
|
|
|
| 17 |
"choices": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
-
def data_to_string(self, data={}
|
| 21 |
data_str = f"{json.dumps(data)}"
|
| 22 |
return data_str
|
| 23 |
|
| 24 |
-
def output(self, content=None, content_type="Completions") -> str:
|
| 25 |
data = self.default_data.copy()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
data["choices"] = [
|
| 43 |
-
{
|
| 44 |
-
"index": 0,
|
| 45 |
-
"delta": {"content": content},
|
| 46 |
-
"finish_reason": None,
|
| 47 |
-
}
|
| 48 |
-
]
|
| 49 |
-
elif content_type == "Finished":
|
| 50 |
-
data["choices"] = [
|
| 51 |
-
{
|
| 52 |
-
"index": 0,
|
| 53 |
-
"delta": {},
|
| 54 |
-
"finish_reason": "stop",
|
| 55 |
-
}
|
| 56 |
-
]
|
| 57 |
-
else:
|
| 58 |
-
data["choices"] = [
|
| 59 |
-
{
|
| 60 |
-
"index": 0,
|
| 61 |
-
"delta": {},
|
| 62 |
-
"finish_reason": None,
|
| 63 |
-
}
|
| 64 |
-
]
|
| 65 |
-
return self.data_to_string(data, content_type)
|
|
|
|
| 1 |
import json
|
| 2 |
|
|
|
|
| 3 |
class OpenaiStreamOutputer:
|
| 4 |
"""
|
| 5 |
Create chat completion - OpenAI API Documentation
|
|
|
|
| 10 |
self.default_data = {
|
| 11 |
"created": 1700000000,
|
| 12 |
"id": "chatcmpl-hugginface",
|
| 13 |
+
"object": "chat.completion",
|
|
|
|
| 14 |
"model": "hugginface",
|
| 15 |
+
"system_fingerprint": "fp_44709d6fcb",
|
| 16 |
"choices": [],
|
| 17 |
+
"usage": {
|
| 18 |
+
"prompt_tokens": 0,
|
| 19 |
+
"completion_tokens": 0,
|
| 20 |
+
"total_tokens": 0
|
| 21 |
+
}
|
| 22 |
}
|
| 23 |
|
| 24 |
+
def data_to_string(self, data={}):
|
| 25 |
data_str = f"{json.dumps(data)}"
|
| 26 |
return data_str
|
| 27 |
|
| 28 |
+
def output(self, content=None, role="assistant", content_type="Completions", tokens_info=None) -> str:
|
| 29 |
data = self.default_data.copy()
|
| 30 |
+
message = {"role": role, "content": content}
|
| 31 |
+
|
| 32 |
+
if tokens_info is None:
|
| 33 |
+
tokens_info = {"prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21}
|
| 34 |
+
|
| 35 |
+
data["choices"] = [
|
| 36 |
+
{
|
| 37 |
+
"index": 0,
|
| 38 |
+
"message": message,
|
| 39 |
+
"logprobs": None,
|
| 40 |
+
"finish_reason": "stop" if content_type == "Finished" else None
|
| 41 |
+
}
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
data["usage"] = tokens_info
|
| 45 |
+
return self.data_to_string(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|