Skip to main content
Open In Colab在 GitHub 上打开

花瓣

Petals在家中运行 100B+ 语言模型,BitTorrent 风格。

本笔记本介绍了如何将 Langchain 与 Petals 结合使用。

安装 petals

petalspackage 才能使用 Petals API。安装petalspip3 install petals.

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

!pip3 install petals

进口

import os

from langchain.chains import LLMChain
from langchain_community.llms import Petals
from langchain_core.prompts import PromptTemplate
API 参考:LLMChain | 花瓣 | 提示模板

设置环境 API Key

确保从 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]

创建提示模板

我们将为 Question and Answer (问题与答案) 创建一个提示模板。

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)