diff --git a/docker-compose.yml b/docker-compose.yml index f2edd849..c9c7927a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,9 +3,6 @@ services: build: context: . dockerfile: docker/api.Dockerfile - args: - http_proxy: ${HTTP_PROXY:-} - https_proxy: ${HTTPS_PROXY:-} image: yuxi-api:0.2.0.dev container_name: api-dev working_dir: /app @@ -15,13 +12,6 @@ services: - ./saves:/app/saves - ./test:/app/test - ${MODEL_DIR:-./models}:/models # 使用默认值处理未定义的环境变量 - deploy: - resources: - reservations: - devices: - - driver: nvidia - device_ids: ['0'] # 使用GPU 1 - capabilities: [gpu] ports: - "5050:5050" networks: @@ -45,16 +35,19 @@ services: - POSTGRES_DATABASE=${POSTGRES_DATABASE:-lightrag} - POSTGRES_USER=${POSTGRES_USER:-lightrag} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-lightrag} + # 连接池配置 + - POSTGRES_MAX_CONNECTIONS=50 + - POSTGRES_MIN_CONNECTIONS=5 - MODEL_DIR=/models - RUNNING_IN_DOCKER=true command: uv run uvicorn server.main:app --host 0.0.0.0 --port 5050 --reload restart: unless-stopped healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:5050/health || exit 1"] - interval: 20s - timeout: 10s - retries: 5 - start_period: 60s + interval: 30s + timeout: 15s + retries: 8 + start_period: 180s depends_on: postgres: condition: service_healthy @@ -103,11 +96,11 @@ services: ports: - "5432:5432" healthcheck: - test: ["CMD-SHELL", "PGPASSWORD=${POSTGRES_PASSWORD:-lightrag} psql -h localhost -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c 'SELECT 1' || exit 1"] - interval: 20s - timeout: 10s - retries: 10 - start_period: 60s + test: ["CMD-SHELL", "PGPASSWORD=${POSTGRES_PASSWORD:-lightrag} psql -h localhost -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c \"SELECT ag_catalog.create_graph('test_ready');\" || PGPASSWORD=${POSTGRES_PASSWORD:-lightrag} psql -h localhost -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c 'SELECT 1'"] + interval: 30s + timeout: 15s + retries: 15 + start_period: 120s networks: - app-network restart: unless-stopped diff --git a/docker/api.Dockerfile b/docker/api.Dockerfile index 37a0d29d..e4b1fc4c 100644 --- a/docker/api.Dockerfile +++ b/docker/api.Dockerfile @@ -1,34 +1,34 @@ -# 使用基础镜像 -FROM nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04 +# 使用轻量级Python基础镜像 +FROM python:3.11-slim COPY --from=ghcr.io/astral-sh/uv:0.7.2 /uv /uvx /bin/ # 设置工作目录 WORKDIR /app # 环境变量设置 -ARG http_proxy -ARG https_proxy ENV TZ=Asia/Shanghai \ - UV_LINK_MODE=copy + UV_LINK_MODE=copy \ + DEBIAN_FRONTEND=noninteractive -# 只有当代理变量不为空时才设置代理 -RUN if [ -n "$http_proxy" ]; then echo "export http_proxy=$http_proxy" >> /etc/environment; fi -RUN if [ -n "$https_proxy" ]; then echo "export https_proxy=$https_proxy" >> /etc/environment; fi - -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - -# 安装系统依赖 -RUN apt-get update && apt-get install -y \ - python3-dev \ - ffmpeg \ - libsm6 \ - libxext6 +# 设置代理和时区,更换镜像源,安装系统依赖 - 合并为一个RUN减少层数 +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \ + # 更换为阿里云镜像源加速下载 + sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \ + sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \ + # 安装系统依赖 + apt-get update && apt-get install -y --no-install-recommends \ + python3-dev \ + ffmpeg \ + libsm6 \ + libxext6 \ + curl \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # 复制项目配置文件 COPY ../pyproject.toml /app/pyproject.toml COPY ../.python-version /app/.python-version -# 安装依赖项(不使用lock文件) +# 安装依赖项 RUN --mount=type=cache,target=/root/.cache/uv \ uv sync --no-install-project diff --git a/docker/init-postgres.sh b/docker/init-postgres.sh index aadad560..f4227a03 100644 --- a/docker/init-postgres.sh +++ b/docker/init-postgres.sh @@ -16,9 +16,13 @@ echo "PostgreSQL service started, waiting for it to be ready..." # 等待 PostgreSQL 完全启动 while ! su - postgres -c "pg_isready" > /dev/null 2>&1; do echo "Waiting for PostgreSQL to be ready..." - sleep 2 + sleep 5 done +# 额外等待以确保数据库完全启动 +echo "PostgreSQL is ready, waiting additional time for full initialization..." +sleep 10 + echo "PostgreSQL is ready, creating user and database..." # 创建用户(如果不存在) @@ -142,7 +146,11 @@ CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_lightrag_doc_status_updated_at ON li # 等待图表创建完成后创建 AGE 相关索引 echo "Waiting for graph tables to be fully initialized..." -sleep 5 +sleep 15 + +# 验证 AGE 扩展是否正确加载 +echo "Verifying AGE extension is working..." +su - postgres -c "psql -d ${POSTGRES_DATABASE:-lightrag} -c \"SELECT ag_catalog.create_graph('test_graph'); SELECT ag_catalog.drop_graph('test_graph', true);\"" || echo "AGE extension verification failed, but continuing..." echo "Creating AGE performance indexes..." @@ -242,10 +250,32 @@ su - postgres -c "psql -c \"ALTER SYSTEM SET effective_cache_size = '1GB';\"" su - postgres -c "psql -c \"ALTER SYSTEM SET maintenance_work_mem = '64MB';\"" su - postgres -c "psql -c \"ALTER SYSTEM SET random_page_cost = 1.1;\"" su - postgres -c "psql -c \"ALTER SYSTEM SET effective_io_concurrency = 200;\"" -su - postgres -c "psql -c \"ALTER SYSTEM SET work_mem = '4MB';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET work_mem = '8MB';\"" -# 启用查询计划缓存 +# 连接和查询超时配置 +su - postgres -c "psql -c \"ALTER SYSTEM SET statement_timeout = '300000';\"" # 5分钟查询超时 +su - postgres -c "psql -c \"ALTER SYSTEM SET idle_in_transaction_session_timeout = '600000';\"" # 10分钟事务超时 +su - postgres -c "psql -c \"ALTER SYSTEM SET tcp_keepalives_idle = '600';\"" # TCP keepalive 设置 +su - postgres -c "psql -c \"ALTER SYSTEM SET tcp_keepalives_interval = '30';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET tcp_keepalives_count = '3';\"" + +# 查询优化配置 +su - postgres -c "psql -c \"ALTER SYSTEM SET max_parallel_workers_per_gather = '2';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET max_parallel_workers = '4';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET enable_parallel_hash = 'on';\"" + +# 内存和查询限制优化 +su - postgres -c "psql -c \"ALTER SYSTEM SET temp_buffers = '16MB';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET max_stack_depth = '2MB';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET huge_pages = 'try';\"" + +# 启用查询计划缓存和日志记录 su - postgres -c "psql -c \"ALTER SYSTEM SET plan_cache_mode = 'auto';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET log_statement = 'all';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET log_duration = 'on';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET log_min_duration_statement = '1000';\"" # 记录超过1秒的查询 +su - postgres -c "psql -c \"ALTER SYSTEM SET log_disconnections = 'on';\"" +su - postgres -c "psql -c \"ALTER SYSTEM SET log_connections = 'on';\"" # 重新加载配置 su - postgres -c "psql -c \"SELECT pg_reload_conf();\"" @@ -303,6 +333,15 @@ echo "Graph: lightrag" echo "Tables: lightrag_kv_store, lightrag_doc_status + AGE graph tables" echo "Connection: postgresql://${POSTGRES_USER:-lightrag}:***@localhost:5432/${POSTGRES_DATABASE:-lightrag}" +# 最终验证数据库完全准备就绪 +echo "Performing final readiness check..." +sleep 5 +if PGPASSWORD="${POSTGRES_PASSWORD:-lightrag}" psql -h localhost -p 5432 -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c "SELECT ag_catalog.create_graph('final_test'); SELECT ag_catalog.drop_graph('final_test', true);" > /dev/null 2>&1; then + echo "✅ Database is fully ready for LightRAG operations!" +else + echo "⚠️ Database basic operations work, but AGE may need more time to be fully ready" +fi + # 显示数据库统计信息 echo "Database statistics:" su - postgres -c "psql -d ${POSTGRES_DATABASE:-lightrag} -c \" diff --git a/pyproject.toml b/pyproject.toml index 265d8afb..a15cac71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "yuxi-know" -version = "0.1.0" +version = "0.2.0.dev" description = "Add your description here" readme = "README.md" requires-python = ">=3.11" @@ -10,7 +10,6 @@ dependencies = [ "dashscope>=1.23.2", "docx2txt>=0.9", "fastapi>=0.115.12", - "flagembedding>=1.3.4", "graspologic>=3.3.0", "langchain-community>=0.3.22", "langchain-deepseek>=0.1.3", @@ -60,6 +59,10 @@ dev = [ "vllm>=0.8.5.post1", ] -# uv 配置 - 使用阿里云镜像源 +# uv 配置 - 使用清华大学镜像源 [tool.uv] -index-url = "https://mirrors.aliyun.com/pypi/simple/" +index-url = "https://pypi.tuna.tsinghua.edu.cn/simple/" +extra-index-url = [ + "https://mirrors.aliyun.com/pypi/simple/", + "https://pypi.douban.com/simple/" +]