diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..85a2fce --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.env.example +.git +.gitignore +*.md +images/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e19d0dd..5919dda 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,11 +15,11 @@ COPY . . # 暴露 Flask 应用监听的端口 # 注意:EXPOSE 只是声明端口,不会实际发布端口,需要在运行容器时进行端口映射 -EXPOSE 5000 +EXPOSE ${SERVER_PORT} # 定义容器启动时执行的命令 # 这里使用 Gunicorn 作为生产级的 WSGI 服务器,而不是 Flask 内置的开发服务器 # 你需要先在 requirements.txt 中添加 gunicorn # CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"] # 如果只是测试或简单应用,也可以直接用 Flask 开发服务器 -CMD ["python", "main.py"] \ No newline at end of file +CMD ["python", "main.py"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 4ed80a8..427b9fd 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,22 +1,11 @@ services: web: build: ./ # 指定 Dockerfile 的构建上下文路径 + container_name: OpenDify ports: - - "181:5000" # 端口映射:主机端口:容器端口 + - "${SERVER_PORT}:${SERVER_PORT}" # 端口映射:主机端口:容器端口 + restart: always volumes: - ./:/app # 挂载本地代码到容器,方便开发时修改代码立即生效 environment: # 环境变量,例如 Flask 的开发模式 - FLASK_ENV: development - # depends_on: # 如果有其他服务,例如数据库,可以添加依赖 - # - db - # db: - # image: postgres:13-alpine - # environment: - # POSTGRES_DB: mydatabase - # POSTGRES_USER: user - # POSTGRES_PASSWORD: password - # volumes: - # - db_data:/var/lib/postgresql/data - -# volumes: # 如果有持久化数据需求,例如数据库数据 -# db_data: \ No newline at end of file + FLASK_ENV: development \ No newline at end of file diff --git a/main.py b/main.py index 25ce6de..0e726f8 100644 --- a/main.py +++ b/main.py @@ -517,14 +517,14 @@ def chat_completions(): ) as response: generate.message_id = None buffer = "" - + for raw_bytes in response.iter_raw(): if not raw_bytes: continue try: buffer += raw_bytes.decode('utf-8') - + while '\n' in buffer: line, buffer = buffer.split('\n', 1) line = line.strip() @@ -558,8 +558,10 @@ def chat_completions(): # time.sleep(delay) if current_answer == "\n\n": current_answer = "" + yield send_char(current_answer, message_id) + current_answer = "\n" yield send_char(current_answer, message_id) - + logger.info(f"message_id: {message_id}") # 立即继续处理下一个请求 continue