Skip to main content

烟花

caution

您当前正在查看关于将烟花模型用作 文本补全模型 的文档。许多流行的烟花模型是 聊天补全模型

您可能想查看 此页面

烟花 通过创建一个创新的AI实验和生产平台,加速生成AI的产品开发。

本示例介绍如何使用 LangChain 与 烟花 模型进行交互。

概述

集成细节

类别包名本地可序列化JS支持包下载包最新
烟花langchain_fireworksPyPI - 下载量PyPI - 版本

设置

凭证

登录 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.

实例化

<!--IMPORTS:[{"imported": "Fireworks", "source": "langchain_fireworks", "docs": "https://python.langchain.com/api_reference/fireworks/llms/langchain_fireworks.llms.Fireworks.html", "title": "Fireworks"}]-->
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",
)

调用

您可以直接使用字符串提示调用模型以获取补全。

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 表达式 (LCEL) 创建一个简单的链,使用非聊天模型。

<!--IMPORTS:[{"imported": "PromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.prompt.PromptTemplate.html", "title": "Fireworks"}, {"imported": "Fireworks", "source": "langchain_fireworks", "docs": "https://python.langchain.com/api_reference/fireworks/llms/langchain_fireworks.llms.Fireworks.html", "title": "Fireworks"}]-->
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"}))
 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

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书