You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1 KiB

3 months ago
import json
import traceback
3 months ago
from util.neo4j_utils import Neo4jUtil, neo4j_client
3 months ago
import httpx
from robyn import jsonify, Response
from app import app
from controller.client import client
@app.post("/api/qa/analyze")
async def analyze(request):
body = request.json()
input_text = body.get("text", "").strip()
if not input_text:
return jsonify({"error": "缺少 text 字段"}), 400
try:
# 直接转发到大模型服务(假设它返回 { "task_id": "xxx" })
resp = await client.post(
"/getEntity",
json={"text": input_text},
timeout=1800.0 # 30分钟
)
if resp.status_code == 202 or resp.status_code == 200:
resp_json = resp.json()
resp_json_data = resp_json.get("data",{})
resp_json_data = json.loads(resp_json_data)
entities = resp_json_data.get("entities", [])
3 months ago
print(entities)
3 months ago
data = []
3 months ago
for name in entities:
3 months ago
neighbors =neo4j_client.find_neighbors_with_relationshipsAI(
3 months ago
node_label=None,
direction="both",
node_properties={"name": name},
3 months ago
rel_type=None
3 months ago
)
3 months ago
data.append({
name:neighbors
})
resp = await client.post(
"/question_agent",
json={"neo4j_data": [],
"text": input_text},
timeout=1800.0 # 30分钟
)
3 months ago
return Response(
status_code=200,
3 months ago
description=jsonify(resp.json()),
3 months ago
headers={"Content-Type": "text/plain; charset=utf-8"}
)
else:
return jsonify({
"error": "提交失败",
"detail": resp.text
}), resp.status_code
except Exception as e:
error_trace = traceback.format_exc()
print("❌ 发生异常:")
print(error_trace)
return jsonify({"error": str(e),"traceback": error_trace}), 500