2023-04-20 00:08:10 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "959300d4",
"metadata": {},
"source": [
2023-06-16 18:52:56 +00:00
"# Hugging Face Local Pipelines\n",
2023-04-20 00:08:10 +00:00
"\n",
2023-06-16 18:52:56 +00:00
"Hugging Face models can be run locally through the `HuggingFacePipeline` class.\n",
2023-04-20 00:08:10 +00:00
"\n",
"The [Hugging Face Model Hub](https://huggingface.co/models) hosts over 120k models, 20k datasets, and 50k demo apps (Spaces), all open source and publicly available, in an online platform where people can easily collaborate and build ML together.\n",
"\n",
2023-06-16 18:52:56 +00:00
"These can be called from LangChain either through this local pipeline wrapper or by calling their hosted inference endpoints through the HuggingFaceHub class. For more information on the hosted pipelines, see the [HuggingFaceHub](huggingface_hub.html) notebook."
2023-04-20 00:08:10 +00:00
]
},
{
"cell_type": "markdown",
"id": "4c1b8450-5eaf-4d34-8341-2d785448a1ff",
"metadata": {
"tags": []
},
"source": [
2023-08-18 17:08:47 +00:00
"To use, you should have the ``transformers`` python [package installed](https://pypi.org/project/transformers/), as well as [pytorch](https://pytorch.org/get-started/locally/). You can also install `xformer` for a more memory-efficient attention implementation."
2023-04-20 00:08:10 +00:00
]
},
{
"cell_type": "code",
2023-08-18 17:08:47 +00:00
"execution_count": null,
2023-04-20 00:08:10 +00:00
"id": "d772b637-de00-4663-bd77-9bc96d798db2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
2023-08-18 17:08:47 +00:00
"%pip install transformers --quiet"
2023-04-20 00:08:10 +00:00
]
},
{
"cell_type": "markdown",
"id": "91ad075f-71d5-4bc8-ab91-cc0ad5ef16bb",
"metadata": {},
"source": [
"### Load the model"
]
},
{
"cell_type": "code",
2023-08-18 17:08:47 +00:00
"execution_count": 6,
2023-04-20 00:08:10 +00:00
"id": "165ae236-962a-4763-8052-c4836d78a5d2",
"metadata": {
"tags": []
},
2023-08-18 17:08:47 +00:00
"outputs": [],
2023-04-20 00:08:10 +00:00
"source": [
2023-08-18 17:08:47 +00:00
"from langchain.llms import HuggingFacePipeline\n",
2023-04-20 00:08:10 +00:00
"\n",
2023-06-16 18:52:56 +00:00
"llm = HuggingFacePipeline.from_model_id(\n",
" model_id=\"bigscience/bloom-1b7\",\n",
" task=\"text-generation\",\n",
" model_kwargs={\"temperature\": 0, \"max_length\": 64},\n",
")"
2023-04-20 00:08:10 +00:00
]
},
{
"cell_type": "markdown",
"id": "00104b27-0c15-4a97-b198-4512337ee211",
"metadata": {},
"source": [
2023-08-18 17:08:47 +00:00
"### Create Chain\n",
"\n",
"With the model loaded into memory, you can compose it with a prompt to\n",
"form a chain."
2023-04-20 00:08:10 +00:00
]
},
{
"cell_type": "code",
2023-08-18 17:08:47 +00:00
"execution_count": 7,
2023-04-20 00:08:10 +00:00
"id": "3acf0069",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" First, we need to understand what is an electroencephalogram. An electroencephalogram is a recording of brain activity. It is a recording of brain activity that is made by placing electrodes on the scalp. The electrodes are placed\n"
]
}
],
"source": [
2023-08-18 17:08:47 +00:00
"from langchain.prompts import PromptTemplate\n",
2023-04-20 00:08:10 +00:00
"\n",
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
2023-08-18 17:08:47 +00:00
"prompt = PromptTemplate.from_template(template)\n",
2023-04-20 00:08:10 +00:00
"\n",
2023-08-18 17:08:47 +00:00
"chain = prompt | llm\n",
2023-04-20 00:08:10 +00:00
"\n",
"question = \"What is electroencephalography?\"\n",
"\n",
2023-08-18 17:08:47 +00:00
"print(chain.invoke({\"question\": question}))"
2023-04-20 00:08:10 +00:00
]
}
],
"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",
2023-06-16 18:52:56 +00:00
"version": "3.11.2"
2023-04-20 00:08:10 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}