diff --git a/notebooks/ipex/README.md b/notebooks/ipex/README.md index 88e184a328..1cadd6104a 100644 --- a/notebooks/ipex/README.md +++ b/notebooks/ipex/README.md @@ -6,4 +6,4 @@ You can find here a list of the notebooks for the IPEX integration in 🤗 Optim | Notebook | Description | | | |:----------|:-------------|:-------------|------:| | [How to optimize your model with IPEX for text generation](https://github.com/huggingface/optimum-intel/blob/main/notebooks/ipex/text_generation.ipynb)| Show how to apply operators and graph-level optimizations using Intel [IPEX](https://github.com/intel/intel-extension-for-pytorch) | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/optimum-intel/blob/main/notebooks/ipex/text_generation.ipynb)| [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/optimum-intel/blob/main/notebooks/ipex/text_generation.ipynb)| - +| [How to optimize your langchain pipeline with IPEX](https://github.com/huggingface/optimum-intel/blob/main/notebooks/ipex/langchain_hf_pipelines.ipynb)| Show how to optimize your langchain pipeline with IPEX [IPEX](https://github.com/intel/intel-extension-for-pytorch) | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/optimum-intel/blob/main/notebooks/ipex/langchain_hf_pipelines.ipynb)| [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/optimum-intel/blob/main/notebooks/ipex/langchain_hf_pipelines.ipynb)| \ No newline at end of file diff --git a/notebooks/ipex/langchain_hf_pipelines.ipynb b/notebooks/ipex/langchain_hf_pipelines.ipynb new file mode 100644 index 0000000000..7311f2e0c7 --- /dev/null +++ b/notebooks/ipex/langchain_hf_pipelines.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hugging Face Pipelines\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you're opening this Notebook on colab, you will probably need to install Langchain and 🤗 Optimum. Uncomment the following cell and run it." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#! pip install langchain-huggingface optimum[ipex]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Make sure your version of langchain-huggingface is at least v0.2 and 🤗 Optimum is at least v1.22.0 since the functionality was introduced in these versions:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from optimum.intel.version import __version__\n", + "\n", + "print(\"optimum-intel version is\", __version__)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from optimum.intel.utils.import_utils import _langchain_hf_version\n", + "\n", + "print(\"langchain-huggingface version is\", _langchain_hf_version)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Model Loading\n", + "\n", + "Models can be loaded by specifying the model parameters using the `from_model_id` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_huggingface.llms import HuggingFacePipeline\n", + "\n", + "hf = HuggingFacePipeline.from_model_id(\n", + " model_id=\"gpt2\",\n", + " task=\"text-generation\",\n", + " pipeline_kwargs={\"max_new_tokens\": 10},\n", + " backend=\"ipex\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create Chain\n", + "\n", + "With the model loaded into memory, you can compose it with a prompt to form a chain." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.prompts import PromptTemplate\n", + "\n", + "template = \"\"\"Question: {question}\n", + "\n", + "Answer: Let's think step by step.\"\"\"\n", + "prompt = PromptTemplate.from_template(template)\n", + "\n", + "chain = prompt | hf\n", + "\n", + "question = \"What is electroencephalography?\"\n", + "\n", + "print(chain.invoke({\"question\": question}))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To get response without prompt, you can bind skip_prompt=True with LLM." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "chain = prompt | hf.bind(skip_prompt=True)\n", + "\n", + "question = \"What is electroencephalography?\"\n", + "\n", + "print(chain.invoke({\"question\": question}))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Streaming response :" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for chunk in chain.stream(question):\n", + " print(chunk, end=\"\", flush=True)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.14" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebooks/ipex/text_generation.ipynb b/notebooks/ipex/text_generation.ipynb index 4c97d5b6b0..021c8bffd5 100644 --- a/notebooks/ipex/text_generation.ipynb +++ b/notebooks/ipex/text_generation.ipynb @@ -22,7 +22,7 @@ "source": [ "import torch\n", "from transformers import AutoTokenizer\n", - "from optimum.intel.ipex import IPEXModelForCausalLM" + "from optimum.intel import IPEXModelForCausalLM" ] }, {