From bca9e63eba4d784aba1e831b28b1ca433c7d3274 Mon Sep 17 00:00:00 2001 From: Ella Charlaix Date: Tue, 28 Jan 2025 15:42:06 +0100 Subject: [PATCH 1/3] Add notebook --- notebooks/ipex/langchain_hf_pipelines.ipynb | 192 ++++++++++++++++++++ notebooks/ipex/text_generation.ipynb | 2 +- 2 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 notebooks/ipex/langchain_hf_pipelines.ipynb diff --git a/notebooks/ipex/langchain_hf_pipelines.ipynb b/notebooks/ipex/langchain_hf_pipelines.ipynb new file mode 100644 index 0000000000..4ee76b63f8 --- /dev/null +++ b/notebooks/ipex/langchain_hf_pipelines.ipynb @@ -0,0 +1,192 @@ +{ + "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": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/echarlaix/miniconda3/envs/ipex/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "optimum-intel version is 1.22.0.dev0\n" + ] + } + ], + "source": [ + "from optimum.intel.version import __version__\n", + "\n", + "print(\"optimum-intel version is\", __version__)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "langchain-huggingface version is 0.1.2\n" + ] + } + ], + "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" ] }, { From 5a635d53670c0578a093ebb314bbae425a2b6a8f Mon Sep 17 00:00:00 2001 From: Ella Charlaix Date: Tue, 28 Jan 2025 15:44:37 +0100 Subject: [PATCH 2/3] add notebook in readme --- notebooks/ipex/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 0fb526d80be0a8664fa72e5ea8d942ea105bd46c Mon Sep 17 00:00:00 2001 From: Ella Charlaix Date: Tue, 28 Jan 2025 15:44:55 +0100 Subject: [PATCH 3/3] clear --- notebooks/ipex/langchain_hf_pipelines.ipynb | 32 +++------------------ 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/notebooks/ipex/langchain_hf_pipelines.ipynb b/notebooks/ipex/langchain_hf_pipelines.ipynb index 4ee76b63f8..7311f2e0c7 100644 --- a/notebooks/ipex/langchain_hf_pipelines.ipynb +++ b/notebooks/ipex/langchain_hf_pipelines.ipynb @@ -32,25 +32,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/home/echarlaix/miniconda3/envs/ipex/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "optimum-intel version is 1.22.0.dev0\n" - ] - } - ], + "outputs": [], "source": [ "from optimum.intel.version import __version__\n", "\n", @@ -59,17 +43,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "langchain-huggingface version is 0.1.2\n" - ] - } - ], + "outputs": [], "source": [ "from optimum.intel.utils.import_utils import _langchain_hf_version\n", "\n",