Skip to main content
Open In ColabOpen on GitHub

Fireworks(中文简体):烟花

注意

您当前所在的页面记录了使用 Fireworks 模型作为 文本补全模型 的用法。许多流行的 Fireworks 模型是 聊天补全模型

您可能想查找的是这个页面

Fireworks accelerates product development on generative AI by creating an innovative AI experiment and production platform.

此示例介绍了如何使用 LangChain 与 Fireworks 模型进行交互。

概览

集成详情

本地可序列化的JS 支持软件包下载最新包裹
Fireworkslangchain_fireworksPyPI - DownloadsPyPI - Version

设置

凭据

登录 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/llama-v3p1-8b-instruct",
base_url="https://api.fireworks.ai/inference/v1/completions",
)

调用

你可以直接使用字符串提示调用模型以获取完成结果。

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/llama-v3p1-8b-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 表达式语言来创建一个简单的链,其中包含非聊天模型。

from langchain_core.prompts import PromptTemplate
from langchain_fireworks import Fireworks

llm = Fireworks(
model="accounts/fireworks/models/llama-v3p1-8b-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"}))
 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 语言模型功能和配置的详细文档,请访问 API 参考: https://python.langchain.com/api_reference/fireworks/llms/langchain_fireworks.llms.Fireworks.html#langchain_fireworks.llms.Fireworks