Update app.py
Browse files
app.py
CHANGED
@@ -368,10 +368,18 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0301"):
|
|
368 |
def stream_notdiamond_response(response, model):
|
369 |
"""流式处理 notdiamond API 响应。"""
|
370 |
buffer = ""
|
|
|
|
|
371 |
for chunk in response.iter_content(1024):
|
372 |
if chunk:
|
373 |
buffer += chunk.decode('utf-8')
|
374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
|
376 |
yield create_openai_chunk('', model, 'stop')
|
377 |
|
@@ -412,10 +420,14 @@ def handle_non_stream_response(response, model, prompt_tokens):
|
|
412 |
def generate_stream_response(response, model, prompt_tokens):
|
413 |
"""生成流式 HTTP 响应。"""
|
414 |
total_completion_tokens = 0
|
|
|
415 |
|
416 |
for chunk in stream_notdiamond_response(response, model):
|
417 |
content = chunk['choices'][0]['delta'].get('content', '')
|
418 |
-
|
|
|
|
|
|
|
419 |
|
420 |
chunk['usage'] = {
|
421 |
"prompt_tokens": prompt_tokens,
|
|
|
368 |
def stream_notdiamond_response(response, model):
|
369 |
"""流式处理 notdiamond API 响应。"""
|
370 |
buffer = ""
|
371 |
+
previous_content = ""
|
372 |
+
|
373 |
for chunk in response.iter_content(1024):
|
374 |
if chunk:
|
375 |
buffer += chunk.decode('utf-8')
|
376 |
+
current_content = buffer.split('\n\n')[-1] # 获取最新的内容块
|
377 |
+
|
378 |
+
if current_content:
|
379 |
+
new_content = current_content[len(previous_content):] # 提取新内容
|
380 |
+
previous_content = current_content # 更新已处理的内容
|
381 |
+
|
382 |
+
yield create_openai_chunk(new_content, model)
|
383 |
|
384 |
yield create_openai_chunk('', model, 'stop')
|
385 |
|
|
|
420 |
def generate_stream_response(response, model, prompt_tokens):
|
421 |
"""生成流式 HTTP 响应。"""
|
422 |
total_completion_tokens = 0
|
423 |
+
previous_content = ""
|
424 |
|
425 |
for chunk in stream_notdiamond_response(response, model):
|
426 |
content = chunk['choices'][0]['delta'].get('content', '')
|
427 |
+
new_content = content[len(previous_content):] # 提取新内容
|
428 |
+
previous_content = content # 更新已处理的内容
|
429 |
+
|
430 |
+
total_completion_tokens += count_tokens(new_content, model)
|
431 |
|
432 |
chunk['usage'] = {
|
433 |
"prompt_tokens": prompt_tokens,
|