Bắt đầu từ mã nguồn cục bộ

Điều kiện tiên quyết

Hệ điều hành
Phần mềm
Giải thích

macOS 10.14 trở lên

Docker Desktop

Thiết lập máy ảo Docker (VM) để sử dụng tối thiểu 2 CPU ảo (vCPU) và 8 GB bộ nhớ khởi tạo. Nếu không, quá trình cài đặt có thể thất bại. Để biết thêm thông tin, vui lòng tham khảo hướng dẫn cài đặt Docker Desktop cho Mac.

Các nền tảng Linux

Docker 19.03 trở lên Docker Compose 1.25.1 trở lên

Vui lòng tham khảo hướng dẫn cài đặt Docker và hướng dẫn cài đặt Docker Compose để biết thêm thông tin về cách cài đặt DockerDocker Compose, tương ứng.

Windows với WSL 2 được kích hoạt

Docker Desktop

Chúng tôi khuyến nghị lưu trữ mã nguồn và dữ liệu khác được liên kết với các container Linux trong hệ thống tệp Linux thay vì hệ thống tệp Windows. Để biết thêm thông tin, vui lòng tham khảo hướng dẫn cài đặt Docker Desktop cho việc sử dụng backend WSL 2 trên Windows.

Nếu bạn cần sử dụng OpenAI TTS, FFmpeg phải được cài đặt trên hệ thống để nó hoạt động đúng cách. Để biết thêm chi tiết, vui lòng tham khảo: Link.

Clone ChatX

git clone https://github.com/langgenius/chatx.git

Trước khi kích hoạt các dịch vụ kinh doanh, chúng ta cần trước tiên triển khai PostgresSQL / Redis / Weaviate (nếu chúng không có sẵn cục bộ). Chúng ta có thể bắt đầu chúng bằng các lệnh sau:

cd docker
docker compose -f docker-compose.middleware.yaml up -d

Triển khai máy chủ

  • Dịch vụ Giao diện API

  • Dịch vụ Tiêu thụ Hàng đợi Bất đồng bộ

Cài đặt môi trường cơ bản:

Khởi động máy chủ yêu cầu Python 3.10.x. Bạn nên sử dụng Anaconda để cài đặt nhanh môi trường Python, môi trường này đã bao gồm công cụ quản lý gói pip.

Để tạo môi trường Python 3.10 có tên 'chatx', bạn có thể sử dụng lệnh sau:

conda create --name chatx python=3.10

Để chuyển sang môi trường Python 'chatx', hãy sử dụng lệnh sau:

conda activate chatx

Thực hiện theo các bước sau:

  1. Điều hướng đến thư mục 'api':

    cd api
  2. Sao chép tệp cấu hình biến môi trường:

    cp .env.example .env
  3. Tạo khóa bí mật ngẫu nhiên và thay thế giá trị của SECRET_KEY trong tệp .env:

    openssl rand -base64 42
    sed -i 's/SECRET_KEY=.*/SECRET_KEY=<your_value>/' .env
  4. Cài đặt các phụ thuộc cần thiết:

    pip install -r requirements.txt
  5. Thực hiện di chuyển cơ sở dữ liệu. Thực hiện di chuyển cơ sở dữ liệu lên phiên bản mới nhất:

    flask db upgrade
  6. Khởi động máy chủ API:

    flask run --host 0.0.0.0 --port=5001 --debug

    output:

    * Debug mode: on
    INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
     * Running on all addresses (0.0.0.0)
     * Running on http://127.0.0.1:5001
    INFO:werkzeug:Press CTRL+C to quit
    INFO:werkzeug: * Restarting with stat
    WARNING:werkzeug: * Debugger is active!
    INFO:werkzeug: * Debugger PIN: 695-801-919
  7. bắt đầu dịch vụ Worker

    Để sử dụng các tác vụ không đồng bộ từ hàng đợi, chẳng hạn như nhập tệp tập dữ liệu và cập nhật tài liệu tập dữ liệu, hãy làm theo các bước sau để khởi động dịch vụ Worker trên Linux hoặc macOS:

    celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail --loglevel INFO

    Nếu bạn đang sử dụng hệ thống Windows để khởi động dịch vụ Worker, vui lòng sử dụng lệnh sau thay thế:

    celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail --loglevel INFO

    output:

     -------------- [email protected] v5.2.7 (dawn-chorus)
    --- ***** ----- 
    -- ******* ---- macOS-10.16-x86_64-i386-64bit 2023-07-31 12:58:08
    - *** --- * --- 
    - ** ---------- [config]
    - ** ---------- .> app:         app:0x7fb568572a10
    - ** ---------- .> transport:   redis://:**@localhost:6379/1
    - ** ---------- .> results:     postgresql://postgres:**@localhost:5432/dify
    - *** --- * --- .> concurrency: 1 (gevent)
    -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
    --- ***** ----- 
     -------------- [queues]
                    .> dataset          exchange=dataset(direct) key=dataset
                    .> generation       exchange=generation(direct) key=generation
                    .> mail             exchange=mail(direct) key=mail
    
    [tasks]
      . tasks.add_document_to_index_task.add_document_to_index_task
      . tasks.clean_dataset_task.clean_dataset_task
      . tasks.clean_document_task.clean_document_task
      . tasks.clean_notion_document_task.clean_notion_document_task
      . tasks.create_segment_to_index_task.create_segment_to_index_task
      . tasks.deal_dataset_vector_index_task.deal_dataset_vector_index_task
      . tasks.document_indexing_sync_task.document_indexing_sync_task
      . tasks.document_indexing_task.document_indexing_task
      . tasks.document_indexing_update_task.document_indexing_update_task
      . tasks.enable_segment_to_index_task.enable_segment_to_index_task
      . tasks.generate_conversation_summary_task.generate_conversation_summary_task
      . tasks.mail_invite_member_task.send_invite_member_mail_task
      . tasks.remove_document_from_index_task.remove_document_from_index_task
      . tasks.remove_segment_from_index_task.remove_segment_from_index_task
      . tasks.update_segment_index_task.update_segment_index_task
      . tasks.update_segment_keyword_index_task.update_segment_keyword_index_task
    
    [2023-07-31 12:58:08,831: INFO/MainProcess] Connected to redis://:**@localhost:6379/1
    [2023-07-31 12:58:08,840: INFO/MainProcess] mingle: searching for neighbors
    [2023-07-31 12:58:09,873: INFO/MainProcess] mingle: all alone
    [2023-07-31 12:58:09,886: INFO/MainProcess] pidbox: Connected to redis://:**@localhost:6379/1.
    [2023-07-31 12:58:09,890: INFO/MainProcess] [email protected] ready.

Triển khai trang frontend

Bắt đầu dịch vụ trang web client frontend

Cài đặt môi trường cơ bản:

Để bắt đầu dịch vụ giao diện người dùng web, bạn sẽ cần Node.js v18.x (LTS)NPM phiên bản 8.x.x hoặc Yarn.

  • Cài đặt NodeJS + NPM

Vui lòng truy cập https://nodejs.org/en/download và chọn gói cài đặt cho hệ điều hành tương ứng của bạn từ v18.x trở lên. Bạn nên tải xuống phiên bản ổn định, bao gồm NPM theo mặc định.

Thực hiện theo các bước sau:

  1. Truy cập thư mục web

    cd web
  2. Cài đặt các phụ thuộc.

    npm install
  3. Cấu hình các biến môi trường. Tạo một tệp có tên .env.local trong thư mục hiện tại và sao chép nội dung từ .env.example. Sửa đổi giá trị của các biến môi trường này theo yêu cầu của bạn:

    # For production release, change this to PRODUCTION
    NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT
    # The deployment edition, SELF_HOSTED or CLOUD
    NEXT_PUBLIC_EDITION=SELF_HOSTED
    # The base URL of console application, refers to the Console base URL of WEB service if console domain is
    # different from api or web app domain.
    # example: http://cloud.dify.ai/console/api
    NEXT_PUBLIC_API_PREFIX=http://localhost:5001/console/api
    # The URL for Web APP, refers to the Web App base URL of WEB service if web app domain is different from
    # console or api domain.
    # example: http://udify.app/api
    NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api
    
    # SENTRY
    NEXT_PUBLIC_SENTRY_DSN=
    NEXT_PUBLIC_SENTRY_ORG=
    NEXT_PUBLIC_SENTRY_PROJECT=
  4. Xây dựng mã

    npm run build
  5. Bắt đầu dịch vụ web:

    npm run start
    # or
    yarn start
    # or
    pnpm start

Sau khi khởi động thành công, terminal sẽ xuất ra thông tin sau:

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
warn  - You have enabled experimental feature (appDir) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback

Truy cập ChatX

Cuối cùng, truy cập http://127.0.0.1:3000 để sử dụng ChatX được triển khai cục bộ.