faiss server

This commit is contained in:
2026-04-10 11:55:00 +00:00
parent bc82e3e708
commit 8e39e609cc
30 changed files with 1271 additions and 1048 deletions

View File

@@ -1,7 +0,0 @@
echo "[1/2] MYSQL"
docker exec ai-chat-mysql mysql -uroot -proot -D ai_chat -e "TRUNCATE TABLE chat_records;"
echo "[2/2] PG"
docker exec ai-chat-pgvector psql -U postgres -d ai_chat -c "TRUNCATE TABLE chat_record_vectors;"

178
scripts/inspect_ai_redis.sh Executable file
View File

@@ -0,0 +1,178 @@
#!/usr/bin/env bash
set -euo pipefail
REDIS_HOST="${REDIS_HOST:-127.0.0.1}"
REDIS_PORT="${REDIS_PORT:-8888}"
REDIS_DB="${REDIS_DB:-0}"
REDIS_PASSWORD="${REDIS_PASSWORD:-}"
PATTERNS=("ai_chat_service_*")
usage() {
cat <<'EOF'
Usage:
inspect_ai_redis.sh [options]
Options:
--host <host> Redis host, default: 127.0.0.1
--port <port> Redis port, default: 8888
--db <db> Redis db index, default: 0
--password <pwd> Redis password
--pattern <pattern> Key pattern, can be repeated. Default: ai_chat_service_*
--help Show this help
Environment:
REDIS_HOST
REDIS_PORT
REDIS_DB
REDIS_PASSWORD
Examples:
./scripts/inspect_ai_redis.sh
./scripts/inspect_ai_redis.sh --pattern 'ai_chat_service_*' --pattern 'foo_*'
REDIS_PORT=6379 ./scripts/inspect_ai_redis.sh --host 10.0.0.8
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--host)
REDIS_HOST="$2"
shift 2
;;
--port)
REDIS_PORT="$2"
shift 2
;;
--db)
REDIS_DB="$2"
shift 2
;;
--password)
REDIS_PASSWORD="$2"
shift 2
;;
--pattern)
if [[ "${PATTERNS[*]}" == "ai_chat_service_*" && "${#PATTERNS[@]}" -eq 1 ]]; then
PATTERNS=()
fi
PATTERNS+=("$2")
shift 2
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
if ! command -v redis-cli >/dev/null 2>&1; then
echo "redis-cli not found in PATH" >&2
exit 1
fi
redis_cmd() {
if [[ -n "$REDIS_PASSWORD" ]]; then
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" -n "$REDIS_DB" -a "$REDIS_PASSWORD" --raw "$@"
else
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" -n "$REDIS_DB" --raw "$@"
fi
}
print_string() {
local key="$1"
redis_cmd GET "$key"
}
print_hash() {
local key="$1"
redis_cmd HGETALL "$key" | awk 'NR % 2 == 1 { printf "%s: ", $0; next } { print $0 }'
}
print_list() {
local key="$1"
redis_cmd LRANGE "$key" 0 -1
}
print_set() {
local key="$1"
redis_cmd SMEMBERS "$key"
}
print_zset() {
local key="$1"
redis_cmd ZRANGE "$key" 0 -1 WITHSCORES | awk 'NR % 2 == 1 { printf "%s => ", $0; next } { print $0 }'
}
print_stream() {
local key="$1"
redis_cmd XRANGE "$key" - +
}
if ! redis_cmd PING >/dev/null 2>&1; then
echo "Failed to connect to Redis at ${REDIS_HOST}:${REDIS_PORT}, db=${REDIS_DB}" >&2
exit 1
fi
declare -A seen_keys=()
keys=()
for pattern in "${PATTERNS[@]}"; do
while IFS= read -r key; do
[[ -z "$key" ]] && continue
if [[ -z "${seen_keys[$key]:-}" ]]; then
seen_keys["$key"]=1
keys+=("$key")
fi
done < <(redis_cmd --scan --pattern "$pattern")
done
if [[ "${#keys[@]}" -eq 0 ]]; then
echo "No keys matched patterns: ${PATTERNS[*]}"
exit 0
fi
printf 'Redis: %s:%s db=%s\n' "$REDIS_HOST" "$REDIS_PORT" "$REDIS_DB"
printf 'Patterns: %s\n' "${PATTERNS[*]}"
printf 'Matched keys: %s\n\n' "${#keys[@]}"
for key in "${keys[@]}"; do
key_type="$(redis_cmd TYPE "$key" | tr -d '\r')"
ttl="$(redis_cmd TTL "$key" | tr -d '\r')"
echo "KEY: $key"
echo "TYPE: $key_type"
echo "TTL: $ttl"
echo "VALUE:"
case "$key_type" in
string)
print_string "$key"
;;
hash)
print_hash "$key"
;;
list)
print_list "$key"
;;
set)
print_set "$key"
;;
zset)
print_zset "$key"
;;
stream)
print_stream "$key"
;;
*)
echo "(unsupported type: $key_type)"
;;
esac
echo
done

View File

View File

@@ -1,138 +0,0 @@
#!/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}"