|
|
@@ -324,27 +324,20 @@ def extract_drug_name(content: str) -> str:
|
|
|
# ============================================================
|
|
|
# API Caller
|
|
|
# ============================================================
|
|
|
-async def call_production_api(query: str, api_base: str = "https://pharmacopoeia.kailin.com.cn"):
|
|
|
+async def call_production_api(query: str, token: str, api_base: str = "https://pharmacopoeia.kailin.com.cn"):
|
|
|
"""Call the production chat API and capture full SSE response."""
|
|
|
import httpx
|
|
|
|
|
|
- # Step 1: Get guest token
|
|
|
- print(f"\n[API] Getting guest token from {api_base}/api/v1/auth/guest ...")
|
|
|
- async with httpx.AsyncClient(timeout=30) as client:
|
|
|
- resp = await client.post(f"{api_base}/api/v1/auth/guest")
|
|
|
- if resp.status_code != 200:
|
|
|
- print(f" FAILED: {resp.status_code} {resp.text}")
|
|
|
- return None
|
|
|
- token_data = resp.json()
|
|
|
- token = token_data.get("access_token", "")
|
|
|
- print(f" OK: token={token[:20]}...")
|
|
|
+ if not token:
|
|
|
+ print("\n[API] ERROR: No token provided (use --token).")
|
|
|
+ return None
|
|
|
|
|
|
- # Step 2: Call chat stream
|
|
|
- print(f"\n[API] Calling {api_base}/api/v1/chat/stream ...")
|
|
|
- print(f" Query: {query}")
|
|
|
+ print(f"\n[API] Calling {api_base}/api/v1/chat/stream ...")
|
|
|
+ print(f" Query: {query}")
|
|
|
|
|
|
- events = {"intent": None, "status": [], "content": [], "meta": None}
|
|
|
+ events = {"intent": None, "status": [], "content": [], "meta": None}
|
|
|
|
|
|
+ async with httpx.AsyncClient(timeout=120) as client:
|
|
|
async with client.stream(
|
|
|
"POST",
|
|
|
f"{api_base}/api/v1/chat/stream",
|
|
|
@@ -376,22 +369,23 @@ async def call_production_api(query: str, api_base: str = "https://pharmacopoeia
|
|
|
except json.JSONDecodeError:
|
|
|
pass
|
|
|
|
|
|
- answer = "".join(events["content"])
|
|
|
- print(f"\n Answer length: {len(answer)} chars")
|
|
|
+ answer = "".join(events["content"])
|
|
|
+ print(f"\n Answer length: {len(answer)} chars")
|
|
|
|
|
|
- return {
|
|
|
- "intent": events["intent"],
|
|
|
- "sources": events["meta"].get("sources", []) if events["meta"] else [],
|
|
|
- "answer_preview": answer[:500] + ("..." if len(answer) > 500 else ""),
|
|
|
- "status": events["status"],
|
|
|
- }
|
|
|
+ return {
|
|
|
+ "intent": events["intent"],
|
|
|
+ "sources": events["meta"].get("sources", []) if events["meta"] else [],
|
|
|
+ "answer_preview": answer[:500] + ("..." if len(answer) > 500 else ""),
|
|
|
+ "status": events["status"],
|
|
|
+ }
|
|
|
|
|
|
|
|
|
# ============================================================
|
|
|
# Main Diagnostic
|
|
|
# ============================================================
|
|
|
async def run_diagnosis(query: str, top_k: int = 20, call_api: bool = False,
|
|
|
- api_base: str = "https://pharmacopoeia.kailin.com.cn"):
|
|
|
+ api_base: str = "https://pharmacopoeia.kailin.com.cn",
|
|
|
+ token: str = ""):
|
|
|
print("=" * 80)
|
|
|
print(f" RAG Pipeline Diagnosis: '{query}'")
|
|
|
print("=" * 80)
|
|
|
@@ -542,7 +536,7 @@ async def run_diagnosis(query: str, top_k: int = 20, call_api: bool = False,
|
|
|
print(f" PHASE 5: Production API Call")
|
|
|
print(f"{'='*80}")
|
|
|
try:
|
|
|
- api_result = await call_production_api(query, api_base)
|
|
|
+ api_result = await call_production_api(query, token, api_base)
|
|
|
if api_result:
|
|
|
print(f"\n API Intent: {api_result['intent']}")
|
|
|
print(f" API Sources ({len(api_result['sources'])}):")
|
|
|
@@ -654,11 +648,21 @@ if __name__ == "__main__":
|
|
|
"--api-base", default="https://pharmacopoeia.kailin.com.cn",
|
|
|
help="API base URL (default: https://pharmacopoeia.kailin.com.cn)"
|
|
|
)
|
|
|
+ parser.add_argument(
|
|
|
+ "--token", default="",
|
|
|
+ help="JWT token for API authentication (required when --call-api)"
|
|
|
+ )
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
+ if args.call_api and not args.token:
|
|
|
+ print("ERROR: --token is required when using --call-api")
|
|
|
+ import sys
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
asyncio.run(run_diagnosis(
|
|
|
query=args.query,
|
|
|
top_k=args.top_k,
|
|
|
call_api=args.call_api,
|
|
|
api_base=args.api_base,
|
|
|
+ token=args.token,
|
|
|
))
|