yangdx
commited on
Commit
·
49a6af5
1
Parent(s):
3a5c6c3
修改流式响应的输出格式:从event-stream改为x-ndjson
Browse files- lightrag/api/lightrag_ollama.py +12 -13
lightrag/api/lightrag_ollama.py
CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI, HTTPException, File, UploadFile, Form, Request
|
|
2 |
from pydantic import BaseModel
|
3 |
import logging
|
4 |
import argparse
|
|
|
5 |
from typing import List, Dict, Any, Optional
|
6 |
from lightrag import LightRAG, QueryParam
|
7 |
from lightrag.llm import openai_complete_if_cache, ollama_embedding
|
@@ -474,19 +475,18 @@ def create_app(args):
|
|
474 |
|
475 |
if request.stream:
|
476 |
from fastapi.responses import StreamingResponse
|
477 |
-
import json
|
478 |
|
479 |
async def stream_generator():
|
480 |
async for chunk in response:
|
481 |
-
yield f"
|
482 |
|
483 |
return StreamingResponse(
|
484 |
stream_generator(),
|
485 |
-
media_type="
|
486 |
headers={
|
487 |
"Cache-Control": "no-cache",
|
488 |
"Connection": "keep-alive",
|
489 |
-
"Content-Type": "
|
490 |
"Access-Control-Allow-Origin": "*",
|
491 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
492 |
"Access-Control-Allow-Headers": "Content-Type"
|
@@ -513,15 +513,15 @@ def create_app(args):
|
|
513 |
|
514 |
async def stream_generator():
|
515 |
async for chunk in response:
|
516 |
-
yield f"
|
517 |
|
518 |
return StreamingResponse(
|
519 |
stream_generator(),
|
520 |
-
media_type="
|
521 |
headers={
|
522 |
"Cache-Control": "no-cache",
|
523 |
"Connection": "keep-alive",
|
524 |
-
"Content-Type": "
|
525 |
"Access-Control-Allow-Origin": "*",
|
526 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
527 |
"Access-Control-Allow-Headers": "Content-Type"
|
@@ -699,7 +699,6 @@ def create_app(args):
|
|
699 |
|
700 |
if request.stream:
|
701 |
from fastapi.responses import StreamingResponse
|
702 |
-
import json
|
703 |
|
704 |
response = await rag.aquery( # 需要 await 来获取异步生成器
|
705 |
cleaned_query,
|
@@ -721,7 +720,7 @@ def create_app(args):
|
|
721 |
},
|
722 |
"done": True
|
723 |
}
|
724 |
-
yield f"
|
725 |
else:
|
726 |
# 流式响应
|
727 |
async for chunk in response:
|
@@ -736,7 +735,7 @@ def create_app(args):
|
|
736 |
},
|
737 |
"done": False
|
738 |
}
|
739 |
-
yield f"
|
740 |
|
741 |
# 发送完成标记,包含性能统计信息
|
742 |
data = {
|
@@ -750,7 +749,7 @@ def create_app(args):
|
|
750 |
"eval_count": 999,
|
751 |
"eval_duration": 1
|
752 |
}
|
753 |
-
yield f"
|
754 |
return # 确保生成器在发送完成标记后立即结束
|
755 |
except Exception as e:
|
756 |
logging.error(f"Error in stream_generator: {str(e)}")
|
@@ -758,11 +757,11 @@ def create_app(args):
|
|
758 |
|
759 |
return StreamingResponse(
|
760 |
stream_generator(),
|
761 |
-
media_type="
|
762 |
headers={
|
763 |
"Cache-Control": "no-cache",
|
764 |
"Connection": "keep-alive",
|
765 |
-
"Content-Type": "
|
766 |
"Access-Control-Allow-Origin": "*",
|
767 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
768 |
"Access-Control-Allow-Headers": "Content-Type"
|
|
|
2 |
from pydantic import BaseModel
|
3 |
import logging
|
4 |
import argparse
|
5 |
+
import json
|
6 |
from typing import List, Dict, Any, Optional
|
7 |
from lightrag import LightRAG, QueryParam
|
8 |
from lightrag.llm import openai_complete_if_cache, ollama_embedding
|
|
|
475 |
|
476 |
if request.stream:
|
477 |
from fastapi.responses import StreamingResponse
|
|
|
478 |
|
479 |
async def stream_generator():
|
480 |
async for chunk in response:
|
481 |
+
yield f"{json.dumps({'response': chunk})}\n"
|
482 |
|
483 |
return StreamingResponse(
|
484 |
stream_generator(),
|
485 |
+
media_type="application/x-ndjson",
|
486 |
headers={
|
487 |
"Cache-Control": "no-cache",
|
488 |
"Connection": "keep-alive",
|
489 |
+
"Content-Type": "application/x-ndjson",
|
490 |
"Access-Control-Allow-Origin": "*",
|
491 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
492 |
"Access-Control-Allow-Headers": "Content-Type"
|
|
|
513 |
|
514 |
async def stream_generator():
|
515 |
async for chunk in response:
|
516 |
+
yield f"{json.dumps({'response': chunk})}\n"
|
517 |
|
518 |
return StreamingResponse(
|
519 |
stream_generator(),
|
520 |
+
media_type="application/x-ndjson",
|
521 |
headers={
|
522 |
"Cache-Control": "no-cache",
|
523 |
"Connection": "keep-alive",
|
524 |
+
"Content-Type": "application/x-ndjson",
|
525 |
"Access-Control-Allow-Origin": "*",
|
526 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
527 |
"Access-Control-Allow-Headers": "Content-Type"
|
|
|
699 |
|
700 |
if request.stream:
|
701 |
from fastapi.responses import StreamingResponse
|
|
|
702 |
|
703 |
response = await rag.aquery( # 需要 await 来获取异步生成器
|
704 |
cleaned_query,
|
|
|
720 |
},
|
721 |
"done": True
|
722 |
}
|
723 |
+
yield f"{json.dumps(data, ensure_ascii=False)}\n"
|
724 |
else:
|
725 |
# 流式响应
|
726 |
async for chunk in response:
|
|
|
735 |
},
|
736 |
"done": False
|
737 |
}
|
738 |
+
yield f"{json.dumps(data, ensure_ascii=False)}\n"
|
739 |
|
740 |
# 发送完成标记,包含性能统计信息
|
741 |
data = {
|
|
|
749 |
"eval_count": 999,
|
750 |
"eval_duration": 1
|
751 |
}
|
752 |
+
yield f"{json.dumps(data, ensure_ascii=False)}\n"
|
753 |
return # 确保生成器在发送完成标记后立即结束
|
754 |
except Exception as e:
|
755 |
logging.error(f"Error in stream_generator: {str(e)}")
|
|
|
757 |
|
758 |
return StreamingResponse(
|
759 |
stream_generator(),
|
760 |
+
media_type="application/x-ndjson",
|
761 |
headers={
|
762 |
"Cache-Control": "no-cache",
|
763 |
"Connection": "keep-alive",
|
764 |
+
"Content-Type": "application/x-ndjson",
|
765 |
"Access-Control-Allow-Origin": "*",
|
766 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
767 |
"Access-Control-Allow-Headers": "Content-Type"
|