Fireworks
注意 (caution)
您目前正在瀏覽的文件頁面說明如何將 Fireworks 模型用作文字完成模型 (text completion models)。 許多流行的 Fireworks 模型是聊天完成模型 (chat completion models)。
您可能正在尋找這個頁面。
Fireworks 透過建立創新的 AI 實驗和生產平台,加速生成式 AI 的產品開發。
此範例介紹如何使用 LangChain 與 Fireworks
模型互動。
概述 (Overview)
整合詳細資訊 (Integration details)
類別 (Class) | 套件 (Package) | 本地 (Local) | 可序列化 (Serializable) | JS 支援 | 套件下載 (Package downloads) | 套件最新版本 (Package latest) |
---|---|---|---|---|---|---|
Fireworks | langchain_fireworks | ❌ | ❌ | ✅ |
設定 (Setup)
憑證 (Credentials)
登入 Fireworks AI 以取得 API 金鑰來存取我們的模型,並確保它已設定為 FIREWORKS_API_KEY
環境變數。 3. 使用模型 ID 設定您的模型。 如果未設定模型,預設模型為 fireworks-llama-v2-7b-chat。 請在 fireworks.ai 上查看完整、最新的模型清單。
import getpass
import os
if "FIREWORKS_API_KEY" not in os.environ:
os.environ["FIREWORKS_API_KEY"] = getpass.getpass("Fireworks API Key:")
安裝 (Installation)
您需要安裝 langchain_fireworks
python 套件,以便筆記本的其餘部分正常運作。
%pip install -qU langchain-fireworks
Note: you may need to restart the kernel to use updated packages.
實例化 (Instantiation)
from langchain_fireworks import Fireworks
# Initialize a Fireworks model
llm = Fireworks(
model="accounts/fireworks/models/mixtral-8x7b-instruct",
base_url="https://api.fireworks.ai/inference/v1/completions",
)
API 參考 (API Reference):Fireworks
調用 (Invocation)
您可以直接使用字串提示呼叫模型以取得完成結果。
output = llm.invoke("Who's the best quarterback in the NFL?")
print(output)
If Manningville Station, Lions rookie EJ Manuel's
使用多個提示詞呼叫
# Calling multiple prompts
output = llm.generate(
[
"Who's the best cricket player in 2016?",
"Who's the best basketball player in the league?",
]
)
print(output.generations)
[[Generation(text=" We're not just asking, we've done some research. We'")], [Generation(text=' The conversation is dominated by Kobe Bryant, Dwyane Wade,')]]
使用額外參數呼叫
# Setting additional parameters: temperature, max_tokens, top_p
llm = Fireworks(
model="accounts/fireworks/models/mixtral-8x7b-instruct",
temperature=0.7,
max_tokens=15,
top_p=1.0,
)
print(llm.invoke("What's the weather like in Kansas City in December?"))
December is a cold month in Kansas City, with temperatures of
鏈接 (Chaining)
您可以使用 LangChain Expression Language 來建立一個包含非聊天模型 (non-chat models) 的簡單鏈接。
from langchain_core.prompts import PromptTemplate
from langchain_fireworks import Fireworks
llm = Fireworks(
model="accounts/fireworks/models/mixtral-8x7b-instruct",
temperature=0.7,
max_tokens=15,
top_p=1.0,
)
prompt = PromptTemplate.from_template("Tell me a joke about {topic}?")
chain = prompt | llm
print(chain.invoke({"topic": "bears"}))
API 參考資料:PromptTemplate | Fireworks
What do you call a bear with no teeth? A gummy bear!
串流 (Streaming)
如果您需要,您可以串流輸出。
for token in chain.stream({"topic": "bears"}):
print(token, end="", flush=True)
Why do bears hate shoes so much? They like to run around in their
API 參考資料
如需所有 Fireworks
LLM 功能和設定的詳細文件,請參閱 API 參考資料:https://langchain-python.dev.org.tw/api_reference/fireworks/llms/langchain_fireworks.llms.Fireworks.html#langchain_fireworks.llms.Fireworks