75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
|
||
import asyncio
|
||
import sys
|
||
import os
|
||
import json
|
||
|
||
# Add project path
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/../")
|
||
|
||
from module_admin.service.ragflow_service import RAGFlowService
|
||
from module_admin.entity.vo.ragflow_vo import ConverseWithChatAssistantModel
|
||
|
||
async def test_fix_logic():
|
||
print("=" * 60)
|
||
print("VERIFICATION: Testing Delta Logic Locally")
|
||
print("=" * 60)
|
||
|
||
# Use the valid key and chat ID
|
||
# Chat ID from test_sse.py or environment
|
||
chat_id = "db4bb966895b11f08cda0242ac130006"
|
||
q = "康达什么时候成立的?"
|
||
|
||
params = ConverseWithChatAssistantModel(
|
||
chat_id=chat_id,
|
||
question=q,
|
||
stream=True
|
||
)
|
||
|
||
print(f"Query: {q}")
|
||
print("[1] Fetching Stream from Native Service (Expect Cumulative)...")
|
||
|
||
try:
|
||
# 修复:直接获取AsyncGenerator,不使用await消费
|
||
result_stream = RAGFlowService.converse_with_chat_assistant_services(params)
|
||
|
||
print("[2] Applying Controller Delta Logic...")
|
||
print("-" * 20 + " OUTPUT " + "-" * 20)
|
||
|
||
last_answer = ""
|
||
|
||
async for chunk in result_stream:
|
||
payload = chunk.get('data') if isinstance(chunk, dict) else chunk
|
||
if not payload: continue
|
||
|
||
body = payload if isinstance(payload, dict) else {'data': payload}
|
||
|
||
if isinstance(body, dict) and 'answer' in body:
|
||
current_answer = body['answer']
|
||
|
||
# --- THIS IS THE LOGIC IN CONTROLLER ---
|
||
if current_answer.startswith(last_answer):
|
||
delta = current_answer[len(last_answer):]
|
||
if delta:
|
||
print(delta, end="", flush=True)
|
||
last_answer = current_answer
|
||
else:
|
||
# Fallback
|
||
last_answer = current_answer
|
||
print(f"\n[RESET] {current_answer}", end="", flush=True)
|
||
# ---------------------------------------
|
||
else:
|
||
pass
|
||
# print(f"[Non-Answer] {body}")
|
||
|
||
print("\n" + "-" * 60)
|
||
print("Verification Complete.")
|
||
|
||
except Exception as e:
|
||
print(f"\nError: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(test_fix_logic())
|