Spaces:
Runtime error
Runtime error
:gem: [Feature] New MessageOutputer: Support Openai Stream format
Browse files
messagers/message_outputer.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 data_to_string(self, data={}, content_type=""):
|
11 |
+
data_str = f"{json.dumps(data)}"
|
12 |
+
|
13 |
+
return data_str
|
14 |
+
|
15 |
+
def output(self, content=None, content_type="Completions") -> str:
|
16 |
+
data = {
|
17 |
+
"created": 1700000000,
|
18 |
+
"id": "chatcmpl-hugginface",
|
19 |
+
"object": "chat.completion.chunk",
|
20 |
+
# "content_type": content_type,
|
21 |
+
"model": "hugginface",
|
22 |
+
"choices": [],
|
23 |
+
}
|
24 |
+
if content_type == "Role":
|
25 |
+
data["choices"] = [
|
26 |
+
{
|
27 |
+
"index": 0,
|
28 |
+
"delta": {"role": "assistant"},
|
29 |
+
"finish_reason": None,
|
30 |
+
}
|
31 |
+
]
|
32 |
+
elif content_type in [
|
33 |
+
"Completions",
|
34 |
+
"InternalSearchQuery",
|
35 |
+
"InternalSearchResult",
|
36 |
+
"SuggestedResponses",
|
37 |
+
]:
|
38 |
+
if content_type in ["InternalSearchQuery", "InternalSearchResult"]:
|
39 |
+
content += "\n"
|
40 |
+
data["choices"] = [
|
41 |
+
{
|
42 |
+
"index": 0,
|
43 |
+
"delta": {"content": content},
|
44 |
+
"finish_reason": None,
|
45 |
+
}
|
46 |
+
]
|
47 |
+
elif content_type == "Finished":
|
48 |
+
data["choices"] = [
|
49 |
+
{
|
50 |
+
"index": 0,
|
51 |
+
"delta": {},
|
52 |
+
"finish_reason": "stop",
|
53 |
+
}
|
54 |
+
]
|
55 |
+
else:
|
56 |
+
data["choices"] = [
|
57 |
+
{
|
58 |
+
"index": 0,
|
59 |
+
"delta": {},
|
60 |
+
"finish_reason": None,
|
61 |
+
}
|
62 |
+
]
|
63 |
+
return self.data_to_string(data, content_type)
|