Skip to content

Commit e23549f

Browse files
authored
Update llamaindex examples (#11940)
* modify rag.py * update readme of gpu example * update llamaindex cpu example and readme * add llamaindex doc * update note style * import before instancing IpexLLMEmbedding * update index in readme * update links * update link * update related links
1 parent 23f51f8 commit e23549f

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

python/llm/example/CPU/LlamaIndex/README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ The RAG example ([rag.py](./rag.py)) is adapted from the [Official llama index R
1414

1515
* **Install LlamaIndex Packages**
1616
```bash
17-
pip install llama-index-readers-file llama-index-vector-stores-postgres llama-index-embeddings-huggingface
17+
pip install llama-index-llms-ipex-llm==0.1.8
18+
pip install llama-index-embeddings-ipex-llm==0.1.5
19+
pip install llama-index-readers-file==0.1.33
20+
pip install llama-index-vector-stores-postgres==0.1.14
21+
pip install pymupdf
1822
```
19-
20-
* **Install IPEX-LLM**
21-
Ensure `ipex-llm` is installed by following the [IPEX-LLM Installation Guide](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Overview/install.html) before proceeding with the examples provided here.
22-
23+
> [!NOTE]
24+
> - You could refer [llama-index-llms-ipex-llm](https://docs.llamaindex.ai/en/stable/examples/llm/ipex_llm/) and [llama-index-embeddings-ipex-llm](https://docs.llamaindex.ai/en/stable/examples/embeddings/ipex_llm/) for more information.
25+
> - The installation of `llama-index-llms-ipex-llm` or `llama-index-embeddings-ipex-llm` will also install `IPEX-LLM` and its dependencies.
26+
> - `IpexLLMEmbedding` currently only provides optimization for Hugging Face Bge models.
2327

2428
* **Database Setup (using PostgreSQL)**:
2529
* Installation:

python/llm/example/CPU/LlamaIndex/rag.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717

1818
import torch
19-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
2019
from sqlalchemy import make_url
2120
from llama_index.vector_stores.postgres import PGVectorStore
2221
# from llama_index.llms.llama_cpp import LlamaCPP
@@ -161,10 +160,11 @@ def messages_to_prompt(messages):
161160
return prompt
162161

163162
def main(args):
164-
embed_model = HuggingFaceEmbedding(model_name=args.embedding_model_path)
163+
from llama_index.embeddings.ipex_llm import IpexLLMEmbedding
164+
embed_model = IpexLLMEmbedding(model_name=args.embedding_model_path)
165165

166166
# Use custom LLM in BigDL
167-
from ipex_llm.llamaindex.llms import IpexLLM
167+
from llama_index.llms.ipex_llm import IpexLLM
168168
llm = IpexLLM.from_model_id(
169169
model_name=args.model_path,
170170
tokenizer_name=args.tokenizer_path,

python/llm/example/GPU/LlamaIndex/README.md

+27-13
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,31 @@ This folder contains examples showcasing how to use [**LlamaIndex**](https://git
88
## Retrieval-Augmented Generation (RAG) Example
99
The RAG example ([rag.py](./rag.py)) is adapted from the [Official llama index RAG example](https://docs.llamaindex.ai/en/stable/examples/low_level/oss_ingestion_retrieval.html). This example builds a pipeline to ingest data (e.g. llama2 paper in pdf format) into a vector database (e.g. PostgreSQL), and then build a retrieval pipeline from that vector database.
1010

11+
### 1. Install Prerequisites
1112

13+
To benefit from IPEX-LLM on Intel GPUs, there are several prerequisite steps for tools installation and environment preparation.
1214

13-
### 1. Setting up Dependencies
15+
If you are a Windows user, visit the [Install IPEX-LLM on Windows with Intel GPU Guide](../../../../../docs/mddocs/Quickstart/install_windows_gpu.md), and follow [Install Prerequisites](../../../../../docs/mddocs/Quickstart/install_windows_gpu.md#install-prerequisites) to update GPU driver (optional) and install Conda.
16+
17+
If you are a Linux user, visit the [Install IPEX-LLM on Linux with Intel GPU](../../../../../docs/mddocs/Quickstart/install_linux_gpu.md), and follow [Install Prerequisites](../../../../../docs/mddocs/Quickstart/install_linux_gpu.md#install-prerequisites) to install GPU driver, Intel® oneAPI Base Toolkit 2024.0, and Conda.
18+
19+
20+
### 2. Setting up Dependencies
1421

1522
* **Install LlamaIndex Packages**
1623
```bash
17-
pip install llama-index-readers-file llama-index-vector-stores-postgres llama-index-embeddings-huggingface
24+
conda activate <your-conda-env-name>
25+
pip install llama-index-llms-ipex-llm[xpu]==0.1.8 --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
26+
pip install llama-index-embeddings-ipex-llm[xpu]==0.1.5 --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
27+
pip install llama-index-readers-file==0.1.33
28+
pip install llama-index-vector-stores-postgres==0.1.14
29+
pip install pymupdf
1830
```
19-
* **Install IPEX-LLM**
20-
21-
Follow the instructions in [GPU Install Guide](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Overview/install.html) to install ipex-llm.
31+
> [!NOTE]
32+
> - You could refer [llama-index-llms-ipex-llm](https://docs.llamaindex.ai/en/stable/examples/llm/ipex_llm_gpu/) and [llama-index-embeddings-ipex-llm](https://docs.llamaindex.ai/en/stable/examples/embeddings/ipex_llm_gpu/) for more information.
33+
> - The installation of `llama-index-llms-ipex-llm` or `llama-index-embeddings-ipex-llm` will also install `IPEX-LLM` and its dependencies.
34+
> - You can also use `https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/` as the `extra-indel-url`.
35+
> - `IpexLLMEmbedding` currently only provides optimization for Hugging Face Bge models.
2236

2337
* **Database Setup (using PostgreSQL)**:
2438
* Linux
@@ -71,7 +85,7 @@ The RAG example ([rag.py](./rag.py)) is adapted from the [Official llama index R
7185
wget --user-agent "Mozilla" "https://arxiv.org/pdf/2307.09288.pdf" -O "data/llama2.pdf"
7286
```
7387
74-
### 2. Configures OneAPI environment variables for Linux
88+
### 3. Configures OneAPI environment variables for Linux
7589
7690
> [!NOTE]
7791
> Skip this step if you are running on Windows.
@@ -82,9 +96,9 @@ This is a required step on Linux for APT or offline installed oneAPI. Skip this
8296
source /opt/intel/oneapi/setvars.sh
8397
```
8498
85-
### 3. Runtime Configurations
99+
### 4. Runtime Configurations
86100
For optimal performance, it is recommended to set several environment variables. Please check out the suggestions based on your device.
87-
#### 3.1 Configurations for Linux
101+
#### 4.1 Configurations for Linux
88102
<details>
89103
90104
<summary>For Intel Arc™ A-Series Graphics and Intel Data Center GPU Flex Series</summary>
@@ -121,7 +135,7 @@ export BIGDL_LLM_XMX_DISABLED=1
121135
122136
</details>
123137
124-
#### 3.2 Configurations for Windows
138+
#### 4.2 Configurations for Windows
125139
<details>
126140
127141
<summary>For Intel iGPU</summary>
@@ -147,7 +161,7 @@ set SYCL_CACHE_PERSISTENT=1
147161
> For the first time that each model runs on Intel iGPU/Intel Arc™ A300-Series or Pro A60, it may take several minutes to compile.
148162
149163
150-
### 4. Running the RAG example
164+
### 5. Running the RAG example
151165
152166
In the current directory, run the example with command:
153167
@@ -164,7 +178,7 @@ python rag.py -m <path_to_model> -t <path_to_tokenizer>
164178
- `-n N_PREDICT`: max predict tokens
165179
- `-t TOKENIZER_PATH`: **Required**, path to the tokenizer model
166180
167-
### 5. Example Output
181+
### 6. Example Output
168182
169183
A query such as **"How does Llama 2 compare to other open-source models?"** with the Llama2 paper as the data source, using the `Llama-2-7b-chat-hf` model, will produce the output like below:
170184
@@ -178,6 +192,6 @@ However, it's important to note that the performance of Llama 2 can vary dependi
178192
In conclusion, while Llama 2 performs well on most benchmarks compared to other open-source models, its performance
179193
```
180194
181-
### 6. Trouble shooting
182-
#### 6.1 Core dump
195+
### 7. Trouble shooting
196+
#### 7.1 Core dump
183197
If you encounter a core dump error in your Python code, it is crucial to verify that the `import torch` statement is placed at the top of your Python file, just as what we did in `rag.py`.

python/llm/example/GPU/LlamaIndex/rag.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#
1616

1717
import torch
18-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
1918
from sqlalchemy import make_url
2019
from llama_index.vector_stores.postgres import PGVectorStore
2120
# from llama_index.llms.llama_cpp import LlamaCPP
@@ -160,10 +159,11 @@ def messages_to_prompt(messages):
160159
return prompt
161160

162161
def main(args):
163-
embed_model = HuggingFaceEmbedding(model_name=args.embedding_model_path)
162+
from llama_index.embeddings.ipex_llm import IpexLLMEmbedding
163+
embed_model = IpexLLMEmbedding(model_name=args.embedding_model_path, device="xpu")
164164

165165
# Use custom LLM in BigDL
166-
from ipex_llm.llamaindex.llms import IpexLLM
166+
from llama_index.llms.ipex_llm import IpexLLM
167167
llm = IpexLLM.from_model_id(
168168
model_name=args.model_path,
169169
tokenizer_name=args.tokenizer_path,

0 commit comments

Comments
 (0)