Update messagers/message_outputer.py
Browse files- messagers/message_outputer.py +55 -34
messagers/message_outputer.py
CHANGED
@@ -1,44 +1,65 @@
|
|
1 |
import json
|
2 |
-
|
3 |
|
4 |
class OpenaiStreamOutputer:
|
|
|
|
|
|
|
|
|
|
|
5 |
def __init__(self):
|
6 |
self.default_data = {
|
7 |
-
"
|
8 |
-
"
|
9 |
-
"
|
10 |
-
"
|
11 |
-
"
|
12 |
"choices": [],
|
13 |
-
"usage": {
|
14 |
-
"prompt_tokens": 0,
|
15 |
-
"completion_tokens": 0,
|
16 |
-
"total_tokens": 0
|
17 |
-
}
|
18 |
}
|
19 |
|
20 |
-
def data_to_string(self, data):
|
21 |
-
|
|
|
22 |
|
23 |
-
def output(self, content=None,
|
24 |
data = self.default_data.copy()
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
"
|
36 |
-
"
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
+
|
3 |
|
4 |
class OpenaiStreamOutputer:
|
5 |
+
"""
|
6 |
+
Create chat completion - OpenAI API Documentation
|
7 |
+
* https://platform.openai.com/docs/api-reference/chat/create
|
8 |
+
"""
|
9 |
+
|
10 |
def __init__(self):
|
11 |
self.default_data = {
|
12 |
+
"created": 1700000000,
|
13 |
+
"id": "chatcmpl-hugginface",
|
14 |
+
"object": "chat.completion.chunk",
|
15 |
+
# "content_type": "Completions",
|
16 |
+
"model": "hugginface",
|
17 |
"choices": [],
|
|
|
|
|
|
|
|
|
|
|
18 |
}
|
19 |
|
20 |
+
def data_to_string(self, data={}, content_type=""):
|
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 |
+
if content_type == "Role":
|
27 |
+
data["choices"] = [
|
28 |
+
{
|
29 |
+
"index": 0,
|
30 |
+
"delta": {"role": "assistant"},
|
31 |
+
"finish_reason": None,
|
32 |
+
}
|
33 |
+
]
|
34 |
+
elif content_type in [
|
35 |
+
"Completions",
|
36 |
+
"InternalSearchQuery",
|
37 |
+
"InternalSearchResult",
|
38 |
+
"SuggestedResponses",
|
39 |
+
]:
|
40 |
+
if content_type in ["InternalSearchQuery", "InternalSearchResult"]:
|
41 |
+
content += "\n"
|
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)
|