Files
leo-gan 5420a0e404 updated langchain/docs/modules/models/llms/integrations/ notebooks (#3041)
- Updated `langchain/docs/modules/models/llms/integrations/` notebooks:
added links to the original sites, the install information, etc.
- Added the `nlpcloud` notebook.
- Removed "Example" from Titles of some notebooks, so all notebook
titles are consistent.
2023-04-17 20:25:32 -07:00

4.0 KiB

Petals

Petals runs 100B+ language models at home, BitTorrent-style.

This notebook goes over how to use Langchain with Petals.

Install petals

The petals package is required to use the Petals API. Install petals using pip3 install petals.

In [ ]:
!pip3 install petals

Imports

In [4]:
import os
from langchain.llms import Petals
from langchain import PromptTemplate, LLMChain

Set the Environment API Key

Make sure to get your API key from Huggingface.

In [2]:
from getpass import getpass

HUGGINGFACE_API_KEY = getpass()
 ········
In [5]:
os.environ["HUGGINGFACE_API_KEY"] = HUGGINGFACE_API_KEY

Create the Petals instance

You can specify different parameters such as the model name, max new tokens, temperature, etc.

In [ ]:
# this can take several minutes to download big files!

llm = Petals(model_name="bigscience/bloom-petals")
Downloading:   1%|▏                        | 40.8M/7.19G [00:24<15:44, 7.57MB/s]

Create a Prompt Template

We will create a prompt template for Question and Answer.

In [ ]:
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

Initiate the LLMChain

In [ ]:
llm_chain = LLMChain(prompt=prompt, llm=llm)

Run the LLMChain

Provide a question and run the LLMChain.

In [ ]:
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

llm_chain.run(question)