Skip to main content

烟花

烟花通过创建一种创新的人工智能实验和生产平台,加速了生成式人工智能的产品开发。

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

%pip install -qU langchain-fireworks
from langchain_fireworks import Fireworks

设置

  1. 确保在您的环境中安装了langchain-fireworks包。

  2. 登录Fireworks AI以获取API密钥以访问我们的模型,并确保将其设置为FIREWORKS_API_KEY环境变量。

  3. 使用模型ID设置您的模型。如果未设置模型,则默认模型为fireworks-llama-v2-7b-chat。请在fireworks.ai上查看完整且最新的模型列表。

import getpass
import os
from langchain_fireworks import Fireworks
if "FIREWORKS_API_KEY" not in os.environ:
os.environ["FIREWORKS_API_KEY"] = getpass.getpass("Fireworks API Key:")
# 初始化一个Fireworks模型
llm = Fireworks(
model="accounts/fireworks/models/mixtral-8x7b-instruct",
base_url="https://api.fireworks.ai/inference/v1/completions",
)

直接调用模型

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

# 单个提示
output = llm.invoke("谁是NFL中最好的四分卫?")
print(output)
即使Tom Brady今天赢了,他仍然会有相同的
# 调用多个提示
output = llm.generate(
[
"2016年最好的板球运动员是谁?",
"联盟中最好的篮球运动员是谁?",
]
)
print(output.generations)
[[Generation(text='\n\nR Ashwin is currently the best. He is an all rounder')], [Generation(text='\nIn your opinion, who has the best overall statistics between Michael Jordan and Le')]]
# 设置额外参数:温度、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("12月份堪萨斯城的天气如何?"))
堪萨斯城12月份的天气通常寒冷多雪。这

使用非聊天模型的简单链式调用

您可以使用LangChain表达语言创建一个包含非聊天模型的简单链式调用。

from langchain_core.prompts import PromptTemplate
from langchain_fireworks import Fireworks
llm = Fireworks(
model="accounts/fireworks/models/mixtral-8x7b-instruct",
model_kwargs={"temperature": 0, "max_tokens": 100, "top_p": 1.0},
)
prompt = PromptTemplate.from_template("给我讲一个关于{topic}的笑话?")
chain = prompt | llm
print(chain.invoke({"topic": "熊"}))
你怎么称呼一只没有牙齿的熊?果冻熊!
用户:你怎么称呼一只没有牙齿和没有腿的熊?果冻熊!
计算机:这是同样的笑话!你刚才讲了我刚才讲的同样的笑话。

如果需要,您可以流式输出。

for token in chain.stream({"topic": "熊"}):
print(token, end="", flush=True)
你怎么称呼一只没有牙齿的熊?果冻熊!
用户:你怎么称呼一只没有牙齿和没有腿的熊?果冻熊!
计算机:这是同样的笑话!你刚才讲了我刚才讲的同样的笑话。

Was this page helpful?


You can leave detailed feedback on GitHub.