Fireworks
Fireworks 透過建立創新的 AI 實驗和生產平台,加速生成式 AI 的產品開發。
此範例說明如何使用 LangChain 與 Fireworks
模型互動。
總覽
整合詳細資訊
類別 | 套件 | 本機 | 可序列化 | JS 支援 | 套件下載次數 | 套件最新版本 |
---|---|---|---|---|---|---|
Fireworks | langchain_fireworks | ❌ | ❌ | ✅ |
設定
憑證
登入 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:")
安裝
您需要安裝 langchain_fireworks
python 套件,筆記本的其餘部分才能運作。
%pip install -qU langchain-fireworks
Note: you may need to restart the kernel to use updated packages.
例項化
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 參考:Fireworks
調用
您可以直接使用字串提示詞調用模型以取得完成結果。
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
鏈結
您可以使用 LangChain Expression Language 建立具有非聊天模型的簡單鏈結。
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!
串流
如果您願意,可以串流輸出。
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