Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker compose and update readme #288

Merged
merged 9 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ output
*.local.toml

localdata/
docker/app_data
model_repository/
*.tmp
__*

arms.trace*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ENV VIRTUAL_ENV=/app/.venv \
ENABLE_AIOHTTPCLIENT=false \
ENABLE_HTTPX=false

RUN apt-get update && apt-get install -y libgl1 libglib2.0-0
RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 curl
WORKDIR /app
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY . .
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_gpu
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ENV VIRTUAL_ENV=/app/.venv \
ENABLE_AIOHTTPCLIENT=false \
ENABLE_HTTPX=false

RUN apt-get update && apt-get install -y libgl1 libglib2.0-0
RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 curl

WORKDIR /app
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile_nginx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM nginx:latest
COPY ./nginx/default.conf etc/nginx/conf.d/default.conf
COPY ./nginx/nginx.conf etc/nginx/nginx.conf
COPY ./docker/nginx/default.conf etc/nginx/conf.d/default.conf
COPY ./docker/nginx/nginx.conf etc/nginx/nginx.conf
3 changes: 1 addition & 2 deletions Dockerfile_ui
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ RUN rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Harbin /etc/localti
ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"

RUN apt-get update && apt-get install -y libgl1 libglib2.0-0

RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 curl
WORKDIR /app
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY . .
Expand Down
313 changes: 26 additions & 287 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
<details open>
<summary></b>📕 Contents</b></summary>

- 💡 [What is PAI-RAG?](#what-is-pai-rag)
- 🌟 [Key Features](#key-features)
- 🔎 [Get Started](#get-started)
- [Local](#run-in-local-environment)
- 💡 [What is PAI-RAG?](#-what-is-pai-rag)
- 🌟 [Key Features](#-key-features)
- 🔎 [Get Started](#-get-started)
- [Docker](#run-in-docker)
- 🔧 [API Service](#api-service)
- [Local](#run-in-local-environment)
- 📜 [Documents](#-documents)
- [API specification](#api-specification)
- [Agentic RAG](#agentic-rag)
- [Data Analysis](#data-analysis)
- [Supported File Types](#supported-file-types)

</details>

Expand All @@ -27,9 +31,8 @@ PAI-RAG is an easy-to-use opensource framework for modular RAG (Retrieval-Augmen

# 🌟 Key Features

![framework](docs/figures/framework.jpg)

- Modular design, flexible and configurable
- Powerful RAG capability: multi-modal rag, agentic-rag and nl2sql support
- Built on community open source components, low customization threshold
- Multi-dimensional automatic evaluation system, easy to grasp the performance quality of each module
- Integrated llm-based-application tracing and evaluation visualization tools
Expand All @@ -38,304 +41,40 @@ PAI-RAG is an easy-to-use opensource framework for modular RAG (Retrieval-Augmen

# 🔎 Get Started

## Run in Local Environment

1. Clone Repo

```bash
git clone [email protected]:aigc-apps/PAI-RAG.git
```

2. Development Environment Settings

This project uses poetry for management. To ensure environmental consistency and avoid problems caused by Python version differences, we specify Python version 3.11.

```bash
conda create -n rag_env python==3.11
conda activate rag_env
```

if you use macOS and need to process PPTX files, you need use the following command to install the dependencies to process PPTX files:

```bash
brew install mono-libgdiplus
```

### (1) CPU

Use poetry to install project dependency packages directly:

```bash
pip install poetry
poetry install
poetry run aliyun-bootstrap -a install
```

### (2) GPU

First replace the default pyproject.toml with the GPU version, and then use poetry to install the project dependency package:

```bash
mv pyproject_gpu.toml pyproject.toml && rm poetry.lock
pip install poetry
poetry install
poetry run aliyun-bootstrap -a install
```

- Common network timeout issues

Note: During the installation, if you encounter a network connection timeout, you can add the Alibaba Cloud or Tsinghua mirror source and append the following lines to the end of the pyproject.toml file:

```bash
[[tool.poetry.source]]
name = "mirrors"
url = "http://mirrors.aliyun.com/pypi/simple/" # Aliyun
# url = "https://pypi.tuna.tsinghua.edu.cn/simple/" # Qsinghua
priority = "default"
```

After that, execute the following commands:

```bash
poetry lock
poetry install
```

3. Load Data

Insert new files in the data_path into the current index storage:

```bash
load_data -c src/pai_rag/config/settings.yaml -d data_path -p pattern
```

path examples:

```
a. load_data -d test/example
b. load_data -d test/example_data/pai_document.pdf
c. load_data -d test/example_data -p *.pdf

```

4. Run RAG Service

To use the OpenAI or DashScope API, you need to introduce environment variables:

```bash
export OPENAI_API_KEY=""
export DASHSCOPE_API_KEY=""
```

To utilize Object Storage Service (OSS) for file storage, particularly when operating in multimodal mode, you must first configure settings in both the src/pai_rag/config/settings.toml and src/pai_rag/config/settings_multi_modal.toml configuration files. Append the following TOML configuration snippet within these files:

```toml
[rag.oss_store]
bucket = ""
endpoint = ""
prefix = ""
```

Additionally, you need to introduce environment variables:

```bash
export OSS_ACCESS_KEY_ID=""
export OSS_ACCESS_KEY_SECRET=""
```

```bash
# Support custom host (default 0.0.0.0), port (default 8001), config (default src/pai_rag/config/settings.yaml), enable-example (default True), skip-download-models (default False)
# Download [bge-large-zh-v1.5, easyocr] by default, you can skip it by setting --skip-download-models.
# you can use tool "load_model" to download other models including [bge-large-zh-v1.5, easyocr, SGPT-125M-weightedmean-nli-bitfit, bge-large-zh-v1.5, bge-m3, bge-reranker-base, bge-reranker-large, paraphrase-multilingual-MiniLM-L12-v2, qwen_1.8b, text2vec-large-chinese]
pai_rag serve [--host HOST] [--port PORT] [--config CONFIG_FILE] [--enable-example False] [--skip-download-models]
```

The default configuration file is src/pai_rag/config/settings.yaml. However, if you require the multimodal llm module, you should switch to the src/pai_rag/config/settings_multi_modal.yaml file instead.

```bash
pai_rag serve -c src/pai_rag/config/settings_multi_modal.yaml
```

5. Download provided models to local directory

```bash
# Support model name (default ""), download all models mentioned before without parameter model_name.
load_model [--model-name MODEL_NAME]
```

6. Run RAG WebUI

```bash
# Supports custom host (default 0.0.0.0), port (default 8002), config (default localhost:8001)
pai_rag ui [--host HOST] [--port PORT] [rag-url RAG_URL]
```

You can also open http://127.0.0.1:8002/ to configure the RAG service and upload local data.

## Run in Docker

To make it easier to use and save time on environment installation, we also provide a method to start directly based on the image.

### Use public images directly

1. RAG Service

- CPU

```bash
docker pull mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0

# -p (port) -v (mount embedding and rerank model directories) -e (set environment variables, if using Dashscope LLM/Embedding, need to be introduced) -w (number of workers, can be specified as the approximate number of CPU cores)
docker run --name pai_rag \
-p 8001:8001 \
-v /huggingface:/huggingface \
-v /your_local_documents_path:/data \
-e DASHSCOPE_API_KEY=${DASHSCOPE_API_KEY} \
-d \
mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0 gunicorn -b 0.0.0.0:8001 -w 16 -k uvicorn.workers.UvicornH11Worker pai_rag.main:app
```

- GPU

```bash
docker pull mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0-gpu

# -p (port) -v (mount embedding and rerank model directories) -e (set environment variables, if using Dashscope LLM/Embedding, you need to introduce it) -w (number of workers, which can be specified as the approximate number of CPU cores)
docker run --name pai_rag \
-p 8001:8001 \
-v /huggingface:/huggingface \
-v /your_local_documents_path:/data \
--gpus all \
-e DASHSCOPE_API_KEY=${DASHSCOPE_API_KEY} \
-d \
mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0-gpu gunicorn -b 0.0.0.0:8001 -w 16 -k uvicorn.workers.UvicornH11Worker pai_rag.main:app
```

2. Load Data

Insert new files in the /data into the current index storage:

1. Setup environmental variables.
```bash
sudo docker exec -ti pai_rag bash
load_data -c src/pai_rag/config/settings.yaml -d /data -p pattern
cd docker
cp .env.example .env
```

path examples:

```
a. load_data -d /data/test/example
b. load_data -d /data/test/example_data/pai_document.pdf
c. load_data -d /data/test/example_data -p *.pdf
edit `.env` file if you are using dashscope api or oss store:
2. Start with docker compose command:
```bash
docker compose up -d
```
3. Now you can open http://localhost:8000 to check whether it works. The service will need to download the model weights, which may take a while (usually 20 minutes).

3. RAG UI
Linux:

```bash
docker pull mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0-ui

docker run --network host -d mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0-ui
```

Mac/Windows:

```bash
docker pull mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0-ui

docker run -p 8002:8002 -d mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairag:0.1.0_ui pai_rag ui -p 8002 -c http://host.docker.internal:8001/
```

You can also open http://127.0.0.1:8002/ to configure the RAG service and upload local data.

### Build your own image based on Dockerfile

You can refer to [How to Build Docker](docs/docker_build.md) to build the image yourself.

After the image is built, you can refer to the above steps to start the Rag service and WebUI.

# 🔧 API Service

You can use the command line to send API requests to the server, for example, calling the [Upload API](#upload-api) to upload a knowledge base file.

## Upload API

It supports uploading local files through API and supports specifying different failure_paths. Each time an API request is sent, a task_id will be returned. The file upload status (processing, completed, failed) can then be checked through the task_id.

- upload_data

```bash
curl -X 'POST' http://127.0.0.1:8000/service/upload_data -H 'Content-Type: multipart/form-data' -F 'files=@local_path/PAI.txt' -F 'faiss_path=localdata/storage'

# Return: {"task_id": "2c1e557733764fdb9fefa063538914da"}
```

- get_upload_state

```bash
curl http://127.0.0.1:8001/service/get_upload_state\?task_id\=2c1e557733764fdb9fefa063538914da

# Return: {"task_id":"2c1e557733764fdb9fefa063538914da","status":"completed"}
```

## Query API

- Supports three dialogue modes:
- /query/retrieval
- /query/llm
- /query: (default) RAG (retrieval + llm)

```bash
curl -X 'POST' http://127.0.0.1:8000/service/query -H "Content-Type: application/json" -d '{"question":"PAI是什么?"}'
```

- Multi-round dialogue

```bash
curl -X 'POST' http://127.0.0.1:8000/service/query -H "Content-Type: application/json" -d '{"question":"What is PAI?"}'
```
## Run in Local Environment

> Parameters: session_id
>
> The unique identifier of the conversation history session. After the session_id is passed in, the conversation history will be recorded. Calling the large model will automatically carry the stored conversation history.
>
> ```bash
> curl -X 'POST' http://127.0.0.1:8000/service/query -H "Content-Type: application/json" -d '{"question":"What are its advantages?", "session_id": "1702ffxxad3xxx6fxxx97daf7c"}'
> ```
If you want to start running/developing pai_rag locally, please refer to [local development](./docs/develop/local_develop.md)

> Parameters: chat_history
>
> The conversation history between the user and the model. Each element in the list is a round of conversation in the form of {"user":"user input","bot":"model output"}. Multiple rounds of conversations are arranged in chronological order.
>
> ```bash
> curl -X 'POST' http://127.0.0.1:8000/service/query -H "Content-Type: application/json" -d '{"question":"What are its features?", "chat_history": [{"user":"What is PAI?", "bot":"PAI is Alibaba Cloud's artificial intelligence platform, which provides a one-stop machine learning solution. This platform supports various machine learning tasks, including supervised learning, unsupervised learning, and reinforcement learning, and is suitable for multiple scenarios such as marketing, finance, and social networks."}]}'
> ```
# 📜 Documents

> Parameters: session_id + chat_history
>
> Chat_history will be used to append and update the conversation history corresponding to the stored session_id
>
> ```bash
> curl -X 'POST' http://127.0.0.1:8000/service/query -H "Content-Type: application/json" -d '{"question":"What are its advantages?", "chat_history": [{"user":"PAI是什么?", "bot":"PAI is Alibaba Cloud's artificial intelligence platform, which provides a one-stop machine learning solution. This platform supports various machine learning tasks, including supervised learning, unsupervised learning, and reinforcement learning, and is suitable for multiple scenarios such as marketing, finance, and social networks."}], "session_id": "1702ffxxad3xxx6fxxx97daf7c"}'
> ```
## API specification

- Agent And Function Tool
You can access and integrate our RAG service according to our [API specification](./docs/api.md).

# Agentic RAG
## Agentic RAG

You can use agent with function calling api-tools in PAI-RAG, please refer to the documentation:
[Agentic RAG](./docs/agentic_rag.md)

# Data Analysis
## Data Analysis

You can use data analysis based on database or sheet file in PAI-RAG, please refer to the documentation: [Data Analysis](./docs/data_analysis_doc.md)

# Parameter Configuration

For more customization options, please refer to the documentation:

[Parameter Configuration Instruction](./docs/config_guide_en.md)

# Supported File Types
## Supported File Types

| 文件类型 | 文件格式 |
| ------------ | -------------------------------------- |
Expand Down
Loading
Loading