|
1 |
| -## Langchain Examples |
| 1 | +# LangChain Example |
2 | 2 |
|
3 |
| -This folder contains examples showcasing how to use `langchain` with `ipex-llm`. |
| 3 | +The examples in this folder shows how to use [LangChain](https://www.langchain.com/) with `ipex-llm` on Intel CPU. |
4 | 4 |
|
5 |
| -### Install-IPEX LLM |
| 5 | +> [!NOTE] |
| 6 | +> Please refer [here](https://python.langchain.com/docs/integrations/llms/ipex_llm) for upstream LangChain LLM documentation with ipex-llm and [here](https://python.langchain.com/docs/integrations/text_embedding/ipex_llm/) for upstream LangChain embedding documentation with ipex-llm. |
6 | 7 |
|
7 |
| -Ensure `ipex-llm` is installed by following the [IPEX-LLM Installation Guide](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Overview/install_cpu.html). |
| 8 | +## 0. Requirements |
| 9 | +To run these examples with IPEX-LLM, we have some recommended requirements for your machine, please refer to [here](../README.md#recommended-requirements) for more information. |
8 | 10 |
|
9 |
| -### Install Dependences Required by the Examples |
| 11 | +## 1. Install |
10 | 12 |
|
| 13 | +We suggest using conda to manage environment: |
| 14 | + |
| 15 | +On Linux: |
11 | 16 |
|
12 | 17 | ```bash
|
13 |
| -pip install langchain==0.0.184 |
14 |
| -pip install -U chromadb==0.3.25 |
15 |
| -pip install -U pandas==2.0.3 |
| 18 | +conda create -n llm python=3.11 |
| 19 | +conda activate llm |
| 20 | + |
| 21 | +# install ipex-llm with 'all' option |
| 22 | +pip install --pre --upgrade ipex-llm[all] --extra-index-url https://download.pytorch.org/whl/cpu |
16 | 23 | ```
|
17 | 24 |
|
| 25 | +On Windows: |
| 26 | +```cmd |
| 27 | +onda create -n llm python=3.11 |
| 28 | +conda activate llm |
| 29 | +
|
| 30 | +pip install --pre --upgrade ipex-llm[all] |
| 31 | +``` |
18 | 32 |
|
19 |
| -### Example: Chat |
| 33 | +## 2. Run examples with LangChain |
20 | 34 |
|
21 |
| -The chat example ([chat.py](./chat.py)) shows how to use `LLMChain` to build a chat pipeline. |
| 35 | +### 2.1. Example: Streaming Chat |
22 | 36 |
|
23 |
| -To run the example, execute the following command in the current directory: |
| 37 | +Install LangChain dependencies: |
24 | 38 |
|
25 | 39 | ```bash
|
26 |
| -python chat.py -m <path_to_model> [-q <your_question>] |
| 40 | +pip install -U langchain langchain-community |
27 | 41 | ```
|
28 |
| -> Note: if `-q` is not specified, it will use `What is AI` by default. |
29 |
| -
|
30 |
| -### Example: RAG (Retrival Augmented Generation) |
31 |
| - |
32 |
| -The RAG example ([rag.py](./rag.py)) shows how to load the input text into vector database, and then use `load_qa_chain` to build a retrival pipeline. |
33 | 42 |
|
34 |
| -To run the example, execute the following command in the current directory: |
| 43 | +In the current directory, run the example with command: |
35 | 44 |
|
36 | 45 | ```bash
|
37 |
| -python rag.py -m <path_to_model> [-q <your_question>] [-i <path_to_input_txt>] |
| 46 | +python chat.py -m MODEL_PATH -q QUESTION |
38 | 47 | ```
|
39 |
| -> Note: If `-i` is not specified, it will use a short introduction to Big-DL as input by default. if `-q` is not specified, `What is IPEX LLM?` will be used by default. |
| 48 | +**Additional Parameters for Configuration:** |
| 49 | +- `-m MODEL_PATH`: **required**, path to the model |
| 50 | +- `-q QUESTION`: question to ask. Default is `What is AI?`. |
40 | 51 |
|
| 52 | +### 2.2. Example: Retrival Augmented Generation (RAG) |
41 | 53 |
|
42 |
| -### Example: Math |
| 54 | +The RAG example ([rag.py](./rag.py)) shows how to load the input text into vector database, and then use LangChain to build a retrival pipeline. |
43 | 55 |
|
44 |
| -The math example ([math.py](./llm_math.py)) shows how to build a chat pipeline specialized in solving math questions. For example, you can ask `What is 13 raised to the .3432 power?` |
| 56 | +Install LangChain dependencies: |
45 | 57 |
|
46 |
| -To run the exmaple, execute the following command in the current directory: |
| 58 | +```bash |
| 59 | +pip install -U langchain langchain-community langchain-chroma sentence-transformers==3.0.1 |
| 60 | +``` |
| 61 | + |
| 62 | +In the current directory, run the example with command: |
47 | 63 |
|
48 | 64 | ```bash
|
49 |
| -python llm_math.py -m <path_to_model> [-q <your_question>] |
| 65 | +python rag.py -m <path_to_llm_model> -e <path_to_embedding_model> [-q QUESTION] [-i INPUT_PATH] |
50 | 66 | ```
|
51 |
| -> Note: if `-q` is not specified, it will use `What is 13 raised to the .3432 power?` by default. |
| 67 | +**Additional Parameters for Configuration:** |
| 68 | +- `-m LLM_MODEL_PATH`: **required**, path to the model. |
| 69 | +- `-e EMBEDDING_MODEL_PATH`: **required**, path to the embedding model. |
| 70 | +- `-q QUESTION`: question to ask. Default is `What is IPEX-LLM?`. |
| 71 | +- `-i INPUT_PATH`: path to the input doc. |
52 | 72 |
|
53 | 73 |
|
54 |
| -### Example: Voice Assistant |
| 74 | +### 2.3. Example: Low Bit |
55 | 75 |
|
56 |
| -The voice assistant example ([voiceassistant.py](./voiceassistant.py)) showcases how to use langchain to build a pipeline that takes in your speech as input in realtime, use an ASR model (e.g. [Whisper-Medium](https://huggingface.co/openai/whisper-medium)) to turn speech into text, and then feed the text into large language model to get response. |
| 76 | +The low_bit example ([low_bit.py](./low_bit.py)) showcases how to use use LangChain with low_bit optimized model. |
| 77 | +By `save_low_bit` we save the weights of low_bit model into the target folder. |
| 78 | +> [!NOTE] |
| 79 | +> `save_low_bit` only saves the weights of the model. |
| 80 | +> Users could copy the tokenizer model into the target folder or specify `tokenizer_id` during initialization. |
57 | 81 |
|
58 |
| -To run the exmaple, execute the following command in the current directory: |
| 82 | +Install LangChain dependencies: |
59 | 83 |
|
60 | 84 | ```bash
|
61 |
| -python voiceassistant.py -m <path_to_model> [-q <your_question>] |
| 85 | +pip install -U langchain langchain-community |
62 | 86 | ```
|
63 |
| -**Runtime Arguments Explained**: |
64 |
| -- `-m MODEL_PATH`: **Required**, the path to the |
65 |
| -- `-r RECOGNITION_MODEL_PATH`: **Required**, the path to the huggingface speech recognition model |
66 |
| -- `-x MAX_NEW_TOKENS`: the max new tokens of model tokens input |
67 |
| -- `-l LANGUAGE`: you can specify a language such as "english" or "chinese" |
68 |
| -- `-d True|False`: whether the model path specified in -m is saved low bit model. |
69 |
| - |
70 | 87 |
|
71 |
| -### Example: Low Bit |
| 88 | +In the current directory, run the example with command: |
72 | 89 |
|
73 |
| -The low_bit example ([low_bit.py](./low_bit.py)) showcases how to use use langchain with low_bit optimized model. |
74 |
| -By `save_low_bit` we save the weights of low_bit model into the target folder. |
75 |
| -> Note: `save_low_bit` only saves the weights of the model. |
76 |
| -> Users could copy the tokenizer model into the target folder or specify `tokenizer_id` during initialization. |
77 | 90 | ```bash
|
78 | 91 | python low_bit.py -m <path_to_model> -t <path_to_target> [-q <your question>]
|
79 | 92 | ```
|
80 |
| -**Runtime Arguments Explained**: |
| 93 | +**Additional Parameters for Configuration:** |
81 | 94 | - `-m MODEL_PATH`: **Required**, the path to the model
|
82 | 95 | - `-t TARGET_PATH`: **Required**, the path to save the low_bit model
|
83 |
| -- `-q QUESTION`: the question |
| 96 | +- `-q QUESTION`: question to ask. Default is `What is AI?`. |
| 97 | + |
| 98 | +### 2.4. Example: Math |
84 | 99 |
|
| 100 | +The math example ([math.py](./llm_math.py)) shows how to build a chat pipeline specialized in solving math questions. For example, you can ask `What is 13 raised to the .3432 power?` |
| 101 | + |
| 102 | +Install LangChain dependencies: |
| 103 | + |
| 104 | +```bash |
| 105 | +pip install -U langchain langchain-community |
| 106 | +``` |
| 107 | + |
| 108 | +In the current directory, run the example with command: |
| 109 | + |
| 110 | +```bash |
| 111 | +python llm_math.py -m <path_to_model> [-q <your_question>] |
| 112 | +``` |
| 113 | + |
| 114 | +**Additional Parameters for Configuration:** |
| 115 | +- `-m MODEL_PATH`: **Required**, the path to the model |
| 116 | +- `-q QUESTION`: question to ask. Default is `What is 13 raised to the .3432 power?`. |
85 | 117 |
|
| 118 | +> [!NOTE] |
| 119 | +> If `-q` is not specified, it will use `What is 13 raised to the .3432 power?` by default. |
86 | 120 |
|
87 |
| -### Legacy (Native INT4 examples) |
| 121 | +### 2.5. Example: Voice Assistant |
88 | 122 |
|
89 |
| -IPEX-LLM also provides langchain integrations using native INT4 mode. Those examples can be foud in [native_int4](./native_int4/) folder. For detailed instructions of settting up and running `native_int4` examples, refer to [Native INT4 Examples README](./README_nativeint4.md). |
| 123 | +The voice assistant example ([voiceassistant.py](./voiceassistant.py)) showcases how to use LangChain to build a pipeline that takes in your speech as input in realtime, use an ASR model (e.g. [Whisper-Medium](https://huggingface.co/openai/whisper-medium)) to turn speech into text, and then feed the text into large language model to get response. |
90 | 124 |
|
| 125 | +Install LangChain dependencies: |
| 126 | +```bash |
| 127 | +pip install -U langchain langchain-community |
| 128 | +pip install transformers==4.36.2 |
| 129 | +``` |
| 130 | + |
| 131 | +To run the exmaple, execute the following command in the current directory: |
| 132 | + |
| 133 | +```bash |
| 134 | +python voiceassistant.py -m <path_to_model> -r <path_to_recognition_model> [-q <your_question>] |
| 135 | +``` |
| 136 | +**Additional Parameters for Configuration:** |
| 137 | +- `-m MODEL_PATH`: **Required**, the path to the |
| 138 | +- `-r RECOGNITION_MODEL_PATH`: **Required**, the path to the huggingface speech recognition model |
| 139 | +- `-x MAX_NEW_TOKENS`: the max new tokens of model tokens input |
| 140 | +- `-l LANGUAGE`: you can specify a language such as "english" or "chinese" |
| 141 | +- `-d True|False`: whether the model path specified in -m is saved low bit model. |
0 commit comments