2ch commited on
Commit
2cf5962
·
verified ·
1 Parent(s): 5ab7fca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -68,13 +68,24 @@ def normalize_headers(request: Request, original_url: str) -> dict:
68
  return normalized
69
 
70
 
71
- def format_chunk(chunk: bytes, model: str) -> bytes | str | dict:
72
- chunk_id = 'chatcmpl-AQ8Lzxlg8eSCB1lgVmboiXwZiexqE'
73
- timestamp = int(datetime.now().timestamp())
74
  try:
75
  content = chunk.decode('utf-8')
76
  except UnicodeDecodeError:
77
- content = chunk.decode('latin-1', errors='ignore')
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  data = {
79
  "id": chunk_id,
80
  "object": "chat.completion.chunk",
@@ -85,7 +96,7 @@ def format_chunk(chunk: bytes, model: str) -> bytes | str | dict:
85
  {
86
  "index": 0,
87
  "delta": {
88
- "content": content
89
  },
90
  "finish_reason": None
91
  }
 
68
  return normalized
69
 
70
 
71
+ def decode(chunk: bytes) -> str:
 
 
72
  try:
73
  content = chunk.decode('utf-8')
74
  except UnicodeDecodeError:
75
+ try:
76
+ content = chunk.decode('latin-1')
77
+ except UnicodeDecodeError:
78
+ try:
79
+ content = chunk.decode('iso-8859-1')
80
+ except UnicodeDecodeError:
81
+ content = chunk.decode(errors='replace')
82
+ return content
83
+
84
+
85
+ def format_chunk(chunk: bytes, model: str) -> bytes | str | dict:
86
+ chunk_id = 'chatcmpl-AQ8Lzxlg8eSCB1lgVmboiXwZiexqE'
87
+ timestamp = int(datetime.now().timestamp())
88
+
89
  data = {
90
  "id": chunk_id,
91
  "object": "chat.completion.chunk",
 
96
  {
97
  "index": 0,
98
  "delta": {
99
+ "content": decode(chunk)
100
  },
101
  "finish_reason": None
102
  }