Skip to main content
Open In ColabOpen on GitHub

Petals

Petals 以 BitTorrent 的方式,在家中运行 100B+ 的语言模型。

本教程将介绍如何将 Langchain 与 Petals 结合使用。

安装 petals

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

Apple Silicon (M1/M2) 用户请遵循此指南 https://github.com/bigscience-workshop/petals/issues/147#issuecomment-1365379642 来安装 petals

!pip3 install petals

Imports

import os

from langchain.chains import LLMChain
from langchain_community.llms import Petals
from langchain_core.prompts import PromptTemplate
API Reference:LLMChain | Petals | PromptTemplate

设置环境变量 API 密钥

请确保从 Huggingface 获取你的 API 密钥

from getpass import getpass

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

创建 Petals 实例

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

# 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]

创建提示模板

我们将创建一个用于问答的提示模板。

template = """Question: {question}

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

prompt = PromptTemplate.from_template(template)

初始化 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)