2023-02-14 05:20:19 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2023-04-18 03:25:32 +00:00
|
|
|
"# Petals\n",
|
|
|
|
"\n",
|
|
|
|
"`Petals` runs 100B+ language models at home, BitTorrent-style.\n",
|
|
|
|
"\n",
|
2023-02-14 05:20:19 +00:00
|
|
|
"This notebook goes over how to use Langchain with [Petals](https://github.com/bigscience-workshop/petals)."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Install petals\n",
|
|
|
|
"The `petals` package is required to use the Petals API. Install `petals` using `pip3 install petals`."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-04-18 03:25:32 +00:00
|
|
|
"!pip3 install petals"
|
2023-02-14 05:20:19 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Imports"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-04-18 03:25:32 +00:00
|
|
|
"execution_count": 4,
|
2023-02-14 05:20:19 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import os\n",
|
|
|
|
"from langchain.llms import Petals\n",
|
|
|
|
"from langchain import PromptTemplate, LLMChain"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Set the Environment API Key\n",
|
2023-04-18 03:25:32 +00:00
|
|
|
"Make sure to get [your API key](https://huggingface.co/docs/api-inference/quicktour#get-your-api-token) from Huggingface."
|
2023-02-14 05:20:19 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-04-18 03:25:32 +00:00
|
|
|
"execution_count": 2,
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdin",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
" ········\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"from getpass import getpass\n",
|
|
|
|
"\n",
|
|
|
|
"HUGGINGFACE_API_KEY = getpass()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 5,
|
2023-02-14 05:20:19 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-04-18 03:25:32 +00:00
|
|
|
"os.environ[\"HUGGINGFACE_API_KEY\"] = HUGGINGFACE_API_KEY"
|
2023-02-14 05:20:19 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Create the Petals instance\n",
|
|
|
|
"You can specify different parameters such as the model name, max new tokens, temperature, etc."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
2023-04-18 03:25:32 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stderr",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Downloading: 1%|▏ | 40.8M/7.19G [00:24<15:44, 7.57MB/s]"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2023-02-14 05:20:19 +00:00
|
|
|
"source": [
|
2023-04-18 03:25:32 +00:00
|
|
|
"# this can take several minutes to download big files!\n",
|
|
|
|
"\n",
|
2023-02-14 05:20:19 +00:00
|
|
|
"llm = Petals(model_name=\"bigscience/bloom-petals\")"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Create a Prompt Template\n",
|
|
|
|
"We will create a prompt template for Question and Answer."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"template = \"\"\"Question: {question}\n",
|
|
|
|
"\n",
|
|
|
|
"Answer: Let's think step by step.\"\"\"\n",
|
|
|
|
"\n",
|
|
|
|
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Initiate the LLMChain"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Run the LLMChain\n",
|
|
|
|
"Provide a question and run the LLMChain."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
|
|
|
|
"\n",
|
|
|
|
"llm_chain.run(question)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
2023-02-16 07:04:28 +00:00
|
|
|
"display_name": "Python 3 (ipykernel)",
|
2023-02-14 05:20:19 +00:00
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
2023-02-16 07:04:28 +00:00
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
2023-02-14 05:20:19 +00:00
|
|
|
"name": "python",
|
2023-02-16 07:04:28 +00:00
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2023-04-18 03:25:32 +00:00
|
|
|
"version": "3.10.6"
|
2023-02-14 05:20:19 +00:00
|
|
|
},
|
|
|
|
"vscode": {
|
|
|
|
"interpreter": {
|
|
|
|
"hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
2023-04-18 03:25:32 +00:00
|
|
|
"nbformat_minor": 4
|
2023-02-14 05:20:19 +00:00
|
|
|
}
|