ka1kuk commited on
Commit
43c8935
1 Parent(s): ed570c3

Update messagers/message_outputer.py

Browse files
Files changed (1) hide show
  1. messagers/message_outputer.py +55 -34
messagers/message_outputer.py CHANGED
@@ -1,44 +1,65 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)