Spaces:
Runtime error
Runtime error
support stream
Browse files
app.py
CHANGED
|
@@ -205,23 +205,44 @@ def get_completion(message,history,profile: gr.OAuthProfile | None,oauth_token:
|
|
| 205 |
'messages': messages,
|
| 206 |
'temperature':temperature,
|
| 207 |
'max_tokens':max_tokens,
|
| 208 |
-
|
|
|
|
| 209 |
}
|
| 210 |
|
| 211 |
# get response
|
| 212 |
-
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
|
| 213 |
-
response_data = response.json()
|
| 214 |
-
print(response_data)
|
| 215 |
-
print('-----------------------------------\n')
|
| 216 |
-
if 'error' in response_data:
|
| 217 |
-
|
| 218 |
-
else:
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
supabase_insert_message(user_message,response_content,messages,response_data,user_name,user_oauth_token,ip,sign,cookie_value)
|
| 223 |
|
| 224 |
-
return response_content
|
| 225 |
|
| 226 |
|
| 227 |
|
|
|
|
| 205 |
'messages': messages,
|
| 206 |
'temperature':temperature,
|
| 207 |
'max_tokens':max_tokens,
|
| 208 |
+
'stream':True,
|
| 209 |
+
'stream_options':{"include_usage": True}, # retrieving token usage for stream response
|
| 210 |
}
|
| 211 |
|
| 212 |
# get response
|
| 213 |
+
# response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
|
| 214 |
+
# response_data = response.json()
|
| 215 |
+
# print(response_data)
|
| 216 |
+
# print('-----------------------------------\n')
|
| 217 |
+
# if 'error' in response_data:
|
| 218 |
+
# response_content = response_data['error']['message']
|
| 219 |
+
# else:
|
| 220 |
+
# response_content = response_data['choices'][0]['message']['content']
|
| 221 |
+
# usage = response_data['usage']
|
| 222 |
+
# return response_content
|
| 223 |
+
|
| 224 |
+
# get response with stream
|
| 225 |
+
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data,stream=True)
|
| 226 |
+
response_content = ""
|
| 227 |
+
for line in response.iter_lines():
|
| 228 |
+
line = line.decode().strip()
|
| 229 |
+
if line == "data: [DONE]":
|
| 230 |
+
continue
|
| 231 |
+
elif line.startswith("data: "):
|
| 232 |
+
line = line[6:] # remove prefix "data: "
|
| 233 |
+
try:
|
| 234 |
+
data = json.loads(line)
|
| 235 |
+
if "delta" in data["choices"][0]:
|
| 236 |
+
content = data["choices"][0]["delta"].get("content", "")
|
| 237 |
+
response_content += content
|
| 238 |
+
yield response_content
|
| 239 |
+
except json.JSONDecodeError:
|
| 240 |
+
print(f"Error decoding line: {line}")
|
| 241 |
+
|
| 242 |
+
response_data = {}
|
| 243 |
+
|
| 244 |
supabase_insert_message(user_message,response_content,messages,response_data,user_name,user_oauth_token,ip,sign,cookie_value)
|
| 245 |
|
|
|
|
| 246 |
|
| 247 |
|
| 248 |
|