This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NeuralChat] Add langchain extension example and update notebook (#1237)
* Add langchain extension example and update notebook Signed-off-by: lvliang-intel <liang1.lv@intel.com>
- Loading branch information
1 parent
7733d44
commit d40e2f1
Showing
3 changed files
with
190 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...l_extension_for_transformers/neural_chat/examples/langchain_extension/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Introduction | ||
|
||
Intel Extension for Transformers provides a comprehensive suite of Langchain-based extension APIs, including advanced retrievers, embedding models, and vector stores. These enhancements are carefully crafted to expand the capabilities of the original langchain API, ultimately boosting overall performance. This extension is specifically tailored to enhance the functionality and performance of RAG. | ||
|
||
|
||
We have introduced enhanced vector store operations, allowing users to adjust and fine-tune their settings even after the chatbot has been initialized, providing a more adaptable and user-friendly experience. For Langchain users, integrating and utilizing optimized Vector Stores is straightforward by replacing the original Chroma API in Langchain. | ||
|
||
We offer optimized retrievers such as `VectorStoreRetriever` and `ChildParentRetriever` to efficiently handle vector store operations, ensuring optimal retrieval performance. Additionally, we provide quantized embedding models to accelerate embedding documents. These Langchain extension APIs are easy to use and are optimized for both performance and accuracy, specifically tailored for Intel hardware. | ||
|
||
# Setup Environment | ||
|
||
## Setup Conda | ||
|
||
First, you need to install and configure the Conda environment: | ||
|
||
```shell | ||
# Download and install Miniconda | ||
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh | ||
bash Miniconda*.sh | ||
source ~/.bashrc | ||
``` | ||
|
||
## Install numactl | ||
|
||
Next, install the numactl library: | ||
|
||
```shell | ||
sudo apt install numactl | ||
``` | ||
|
||
## Install Intel Extension for Transformers | ||
|
||
```shell | ||
pip install intel-extension-for-transformers | ||
``` | ||
|
||
## Install Python dependencies | ||
|
||
Install the following Python dependencies using Conda: | ||
|
||
```shell | ||
conda install astunparse ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses -y | ||
conda install jemalloc gperftools -c conda-forge -y | ||
conda install git-lfs -y | ||
``` | ||
|
||
Install other dependencies using pip: | ||
|
||
```bash | ||
pip install -r ../../requirements.txt | ||
``` | ||
|
||
Install retrieval plugin dependencies using pip: | ||
```bash | ||
pip install -r ../../pipeling/plugins/retrieval/requirements.txt | ||
``` | ||
|
||
# Test | ||
|
||
```shell | ||
python main.py | ||
``` |
50 changes: 50 additions & 0 deletions
50
intel_extension_for_transformers/neural_chat/examples/langchain_extension/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2023 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline | ||
from langchain.chains import RetrievalQA | ||
from langchain_core.vectorstores import VectorStoreRetriever | ||
from langchain_core.documents import Document | ||
from langchain_community.embeddings import HuggingFaceBgeEmbeddings | ||
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | ||
from intel_extension_for_transformers.langchain.vectorstores import Chroma | ||
from intel_extension_for_transformers.neural_chat.pipeline.plugins.retrieval.parser.parser import DocumentParser | ||
import requests | ||
|
||
url = "https://d1io3yog0oux5.cloudfront.net/_897efe2d574a132883f198f2b119aa39/intel/db/888/8941/file/412439%281%29_12_Intel_AR_WR.pdf" | ||
filename = "Intel_AR_WR.pdf" | ||
response = requests.get(url) | ||
with open(filename, 'wb') as file: | ||
file.write(response.content) | ||
print(f"File '{filename}' downloaded successfully.") | ||
|
||
document_parser = DocumentParser() | ||
input_path="./Intel_AR_WR.pdf" | ||
data_collection=document_parser.load(input=input_path) | ||
documents = [] | ||
for data, meta in data_collection: | ||
doc = Document(page_content=data, metadata={"source":meta}) | ||
documents.append(doc) | ||
embeddings = HuggingFaceBgeEmbeddings(model_name="BAAI/bge-base-en-v1.5") | ||
knowledge_base = Chroma.from_documents(documents=documents, embedding=embeddings, persist_directory='./output') | ||
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf") | ||
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf") | ||
pipe = HuggingFacePipeline(pipeline=pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=128)) | ||
retriever = VectorStoreRetriever(vectorstore=knowledge_base) | ||
retrievalQA = RetrievalQA.from_llm(llm=pipe, retriever=retriever) | ||
result = retrievalQA({"query": "What is IDM 2.0?"}) | ||
print(result) |