feat: stream
Browse files
app.py
CHANGED
@@ -4,10 +4,8 @@ import json
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
-
# 加载.env文件中的环境变量
|
8 |
load_dotenv()
|
9 |
|
10 |
-
# 从环境变量中读取配置
|
11 |
API_URL = os.getenv("API_URL")
|
12 |
API_TOKEN = os.getenv("API_TOKEN")
|
13 |
|
@@ -24,7 +22,7 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
24 |
|
25 |
def respond(
|
26 |
message,
|
27 |
-
history: list[dict],
|
28 |
system_message,
|
29 |
max_tokens,
|
30 |
temperature,
|
@@ -32,10 +30,8 @@ def respond(
|
|
32 |
):
|
33 |
messages = [{"role": "system", "content": system_message}]
|
34 |
|
35 |
-
# 添加历史消息
|
36 |
messages.extend(history)
|
37 |
|
38 |
-
# 添加当前用户消息
|
39 |
messages.append({"role": "user", "content": message})
|
40 |
|
41 |
headers = {
|
@@ -45,7 +41,7 @@ def respond(
|
|
45 |
|
46 |
data = {
|
47 |
"model": "/data/DMind-1",
|
48 |
-
"stream":
|
49 |
"messages": messages,
|
50 |
"temperature": temperature,
|
51 |
"top_p": top_p,
|
@@ -58,29 +54,29 @@ def respond(
|
|
58 |
print(f"[INFO] userMsg: {message}")
|
59 |
|
60 |
try:
|
61 |
-
with requests.post(API_URL, headers=headers, json=data) as r:
|
62 |
-
# print(f"[INFO] response status: {r.status_code}")
|
63 |
if r.status_code == 200:
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
else:
|
79 |
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
80 |
-
|
81 |
except Exception as e:
|
82 |
print(f"[ERROR] Request error: {e}")
|
83 |
-
|
84 |
|
85 |
|
86 |
"""
|
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
|
|
|
7 |
load_dotenv()
|
8 |
|
|
|
9 |
API_URL = os.getenv("API_URL")
|
10 |
API_TOKEN = os.getenv("API_TOKEN")
|
11 |
|
|
|
22 |
|
23 |
def respond(
|
24 |
message,
|
25 |
+
history: list[dict],
|
26 |
system_message,
|
27 |
max_tokens,
|
28 |
temperature,
|
|
|
30 |
):
|
31 |
messages = [{"role": "system", "content": system_message}]
|
32 |
|
|
|
33 |
messages.extend(history)
|
34 |
|
|
|
35 |
messages.append({"role": "user", "content": message})
|
36 |
|
37 |
headers = {
|
|
|
41 |
|
42 |
data = {
|
43 |
"model": "/data/DMind-1",
|
44 |
+
"stream": True,
|
45 |
"messages": messages,
|
46 |
"temperature": temperature,
|
47 |
"top_p": top_p,
|
|
|
54 |
print(f"[INFO] userMsg: {message}")
|
55 |
|
56 |
try:
|
57 |
+
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
|
|
58 |
if r.status_code == 200:
|
59 |
+
for line in r.iter_lines():
|
60 |
+
if line:
|
61 |
+
line = line.decode('utf-8')
|
62 |
+
if line.startswith('data: '):
|
63 |
+
try:
|
64 |
+
json_response = json.loads(line[6:])
|
65 |
+
if 'choices' in json_response and len(json_response['choices']) > 0:
|
66 |
+
delta = json_response['choices'][0].get('delta', {})
|
67 |
+
if 'content' in delta:
|
68 |
+
content = delta['content']
|
69 |
+
if content:
|
70 |
+
yield content
|
71 |
+
except json.JSONDecodeError as e:
|
72 |
+
print(f"[ERROR] JSON decode error: {e}")
|
73 |
+
continue
|
74 |
else:
|
75 |
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
76 |
+
yield "Service temporarily unavailable"
|
77 |
except Exception as e:
|
78 |
print(f"[ERROR] Request error: {e}")
|
79 |
+
yield "Service error occurred"
|
80 |
|
81 |
|
82 |
"""
|