跳到主要內容
Open In ColabOpen on GitHub

Petals

Petals 在家執行 100B+ 語言模型,採用 BitTorrent 風格。

本筆記本介紹如何將 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

導入

import os

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

設定環境 API 金鑰

請務必從 Huggingface 取得您的 API 金鑰

from getpass import getpass

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

建立 Petals 實例

您可以指定不同的參數,例如模型名稱、最大新 token 數、溫度等。

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

此頁面是否對您有幫助?