跳到主要內容

Petals

Petals 讓您在家中以 BitTorrent 方式運行 100B+ 以上的語言模型。

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

此頁面是否對您有幫助?