Skip to main content

Petals

Petals 在家中运行超过100B的语言模型,类似于BitTorrent。

本笔记本介绍如何使用 Petals 与 Langchain。

安装 petals​

使用 Petals API 需要安装 petals 包。使用 pip3 install petals 安装 petals。

对于 Apple Silicon(M1/M2) 用户,请按照此指南 https://github.com/bigscience-workshop/petals/issues/147#issuecomment-1365379642 安装 petals

pip3 install petals

导入​

import os
from langchain.llms import Petals
from langchain import PromptTemplate, LLMChain

设置环境 API 密钥​

确保从 Huggingface 获取 您的 API 密钥。

from getpass import getpass

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

创建 Petals 实例​

您可以指定不同的参数,如模型名称、最大新标记、温度等。

# 下载大文件可能需要几分钟的时间!

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

创建 Prompt 模板​

我们将为问题和答案创建一个 Prompt 模板。

template = """Question: {question}

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

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

初始化 LLMChain​

llm_chain = LLMChain(prompt=prompt, llm=llm)

运行 LLMChain​

提供一个问题并运行 LLMChain。

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

llm_chain.run(question)