Skip to main content
Open In ColabOpen on GitHub

花瓣

Petals 以类似 BitTorrent 的方式在家中运行超过 1000 亿参数的语言模型。

本笔记本介绍了如何将 Langchain 与 Petals 一起使用。

安装 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

Imports

import os

from langchain.chains import LLMChain
from langchain_community.llms import Petals
from langchain_core.prompts import 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)