Update messagers/message_outputer.py
Browse files- messagers/message_outputer.py +34 -55
messagers/message_outputer.py
CHANGED
@@ -1,65 +1,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 |
-
"
|
13 |
-
"
|
14 |
-
"
|
15 |
-
|
16 |
-
"
|
17 |
"choices": [],
|
|
|
|
|
|
|
|
|
|
|
18 |
}
|
19 |
|
20 |
-
def data_to_string(self, data
|
21 |
-
|
22 |
-
return data_str
|
23 |
|
24 |
-
def output(self, content=None,
|
25 |
data = self.default_data.copy()
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
"
|
37 |
-
"
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
+
import time
|
3 |
|
4 |
class OpenaiStreamOutputer:
|
|
|
|
|
|
|
|
|
|
|
5 |
def __init__(self):
|
6 |
self.default_data = {
|
7 |
+
"id": "chatcmpl-123",
|
8 |
+
"object": "chat.completion",
|
9 |
+
"created": None, # Will be set to current time in output
|
10 |
+
"model": "gpt-3.5-turbo-0613",
|
11 |
+
"system_fingerprint": "fp_44709d6fcb",
|
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 |
+
return json.dumps(data, indent=2)
|
|
|
22 |
|
23 |
+
def output(self, content=None, role="assistant", prompt_tokens=0, completion_tokens=0):
|
24 |
data = self.default_data.copy()
|
25 |
+
|
26 |
+
# Set the current Unix timestamp
|
27 |
+
data["created"] = int(time.time())
|
28 |
+
|
29 |
+
data["choices"] = [{
|
30 |
+
"index": 0,
|
31 |
+
"message": {
|
32 |
+
"role": role,
|
33 |
+
"content": content,
|
34 |
+
},
|
35 |
+
"logprobs": None,
|
36 |
+
"finish_reason": "stop"
|
37 |
+
}]
|
38 |
+
|
39 |
+
# Update token usage
|
40 |
+
data["usage"]["prompt_tokens"] = prompt_tokens
|
41 |
+
data["usage"]["completion_tokens"] = completion_tokens
|
42 |
+
data["usage"]["total_tokens"] = prompt_tokens + completion_tokens
|
43 |
+
|
44 |
+
return self.data_to_string(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|