Files
mchat/scripts/verify-via-backends.sh
2026-04-10 12:00:03 +08:00

139 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "用法: bash scripts/verify-via-backends.sh \"你的消息\""
exit 1
fi
PROMPT="$1"
BACKEND_URL="${BACKEND_URL:-http://127.0.0.1:7080}"
MYSQL_CONTAINER="${MYSQL_CONTAINER:-ai-chat-mysql}"
PG_CONTAINER="${PG_CONTAINER:-ai-chat-pgvector}"
REDIS_HOST="${REDIS_HOST:-127.0.0.1}"
REDIS_PORT="${REDIS_PORT:-8888}"
REDIS_PASSWORD="${REDIS_PASSWORD:-123456}"
REQUEST_TIMEOUT="${REQUEST_TIMEOUT:-120}"
tmp_response="$(mktemp)"
tmp_stderr="$(mktemp)"
json_payload="$(python3 - "${PROMPT}" <<'PY'
import json, sys
print(json.dumps({"prompt": sys.argv[1], "options": {}}, ensure_ascii=False))
PY
)"
echo "[1/4] 通过 backend 发送消息"
if ! curl -sS -N \
-H 'Content-Type: application/json' \
--max-time "${REQUEST_TIMEOUT}" \
-X POST "${BACKEND_URL}/api/chat-process" \
-d "${json_payload}" \
> "${tmp_response}" 2> "${tmp_stderr}"; then
echo "backend 请求失败"
cat "${tmp_stderr}"
echo
echo "请求地址: ${BACKEND_URL}/api/chat-process"
echo "请求体: ${json_payload}"
exit 1
fi
first_line="$(head -n1 "${tmp_response}")"
if [[ -z "${first_line}" ]]; then
echo "未收到任何流式响应"
echo "响应文件: ${tmp_response}"
echo "响应内容:"
cat "${tmp_response}"
exit 1
fi
echo "响应首行:"
echo "${first_line}"
parsed_meta="$(python3 - "${tmp_response}" <<'PY'
import json, sys
path = sys.argv[1]
first = None
last = None
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
for line in f:
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
except Exception:
continue
if first is None:
first = obj
last = obj
if first is None:
print("")
sys.exit(0)
status = first.get("status", "")
error_message = first.get("message", "")
assistant_id = first.get("id", "")
parent_message_id = first.get("parentMessageId", "")
reply_text = ""
if last is not None:
reply_text = last.get("text", "")
print(status)
print(error_message)
print(assistant_id)
print(parent_message_id)
print(reply_text)
PY
)"
response_status="$(printf '%s\n' "${parsed_meta}" | sed -n '1p')"
response_error_message="$(printf '%s\n' "${parsed_meta}" | sed -n '2p')"
assistant_id="$(printf '%s\n' "${parsed_meta}" | sed -n '3p')"
parent_message_id="$(printf '%s\n' "${parsed_meta}" | sed -n '4p')"
reply_text="$(printf '%s\n' "${parsed_meta}" | sed -n '5,$p')"
if [[ "${response_status}" == "Fail" ]]; then
echo "backend 返回错误:"
echo "${response_error_message}"
echo
echo "响应文件: ${tmp_response}"
exit 1
fi
if [[ -z "${parent_message_id}" || -z "${assistant_id}" ]]; then
echo "未能解析 assistant_id 或 parentMessageId"
echo "响应文件: ${tmp_response}"
exit 1
fi
echo "assistant_id=${assistant_id}"
echo "request_parent_message_id=${parent_message_id}"
echo
echo "回复内容:"
echo "${reply_text}"
echo
echo "[2/4] 查询 MySQL chat_records"
docker exec "${MYSQL_CONTAINER}" mysql -t -uroot -proot -D ai_chat -e \
"select id,user_msg,user_msg_tokens,user_msg_keywords,create_at from chat_records order by id desc limit 5;"
echo
echo "[3/4] 查询 PostgreSQL chat_record_vectors"
docker exec "${PG_CONTAINER}" psql -U postgres -d ai_chat -c \
"select record_id, keywords_text, created_at from chat_record_vectors order by created_at desc limit 5;"
echo
echo "[4/4] 查询 Redis 上下文"
redis_response="$(printf 'AUTH %s\r\nSCANPREFIX ai_chat_service_\r\n' \
"${REDIS_PASSWORD}" | nc -w 1 "${REDIS_HOST}" "${REDIS_PORT}")"
echo "${redis_response}"
echo
echo "用户消息 Redis Key: ai_chat_service_${parent_message_id}"
echo "AI回复 Redis Key: ai_chat_service_${assistant_id}"
echo
echo "原始响应保存在: ${tmp_response}"