Skip to main content

Fireworks

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

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

from langchain.llms.fireworks import Fireworks, FireworksChat
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
)
import os

设置

联系Fireworks AI以获取API密钥以访问我们的模型

使用模型ID设置您的模型。如果未设置模型,则默认模型为fireworks-llama-v2-7b-chat。

# 初始化Fireworks LLM
os.environ['FIREWORKS_API_KEY'] = "" #将此更改为您自己的API密钥
llm = Fireworks(model_id="accounts/fireworks/models/fireworks-llama-v2-13b-chat")
# 创建LLM链
llm_chain = LLMChain(prompt=prompt, llm=llm)

调用模型

您可以使用LLM调用指定提示的模型。

当前指定的模型:

  • Falcon
    • accounts/fireworks/models/fireworks-falcon-7b
    • accounts/fireworks/models/fireworks-falcon-40b-w8a16
  • Llama 2
    • accounts/fireworks/models/llama-v2-7b
    • accounts/fireworks/models/llama-v2-7b-w8a16
    • accounts/fireworks/models/llama-v2-7b-chat
    • accounts/fireworks/models/llama-v2-7b-chat-w8a16
    • accounts/fireworks/models/llama-v2-13b
    • accounts/fireworks/models/llama-v2-13b-w8a16
    • accounts/fireworks/models/llama-v2-13b-chat
    • accounts/fireworks/models/llama-v2-13b-chat-w8a16
    • accounts/fireworks/models/llama-v2-70b-chat-4gpu
  • StarCoder
    • accounts/fireworks/models/starcoder-1b-w8a16-1gpu
    • accounts/fireworks/models/starcoder-3b-w8a16-1gpu
    • accounts/fireworks/models/starcoder-7b-w8a16-1gpu
    • accounts/fireworks/models/starcoder-16b-w8a16
#单个提示
output = llm("Who's the best quarterback in the NFL?")
print(output)
    

这是一个多年来一直存在争议的问题,不同的分析师和球迷为各种四分卫提出了自己的观点。以下是NFL最佳四分卫的一些顶级竞争者:

1. 汤姆·布雷迪(Tom Brady):这位新英格兰爱国者的传奇人物赢得了六个超级碗冠军,并四次被评为超级碗最有价值球员。他以精确的传球、控制球袋的能力和阅读防守而闻名。
2. 亚伦·罗杰斯(Aaron Rodgers):这位格林贝 Packers 的四分卫赢得了两个超级碗冠军,并两次被评为NFL最有价值球员。他以快速的释放、准确性和用脚延长比赛时间的能力而闻名。
3. 德鲁·布里斯(Drew Brees):这位新奥尔良圣徒队的四分卫赢得了一个超级碗冠军,并一次被评为NFL最有价值球员。他以准确性、控制球袋的能力和阅读防守而闻名。
4. 帕特里克·马洪斯(Patrick Mahomes):这位堪萨斯城酋长队的四分卫赢得了一个超级碗冠军,并两次被评为NFL最有价值球员。他以臂力、运动能力和在球袋外制造进攻的能力而闻名。
5. 罗素·威尔逊(Russell Wilson):这位西雅图海鹰队的四分卫赢得了一个超级碗冠军,并一次被评为NFL最有价值球员。他以机动性、准确性和用脚延长比赛时间的能力而闻名。

当然,联盟中还有其他才华横溢的四分卫,例如拉马尔·杰克逊(Lamar Jackson)、德沙恩·沃森(Deshaun Watson)和卡森·温茨(Carson Wentz),他们也可以被认为是最佳四分卫之一。最终,对于谁是NFL最佳四分卫的问题是主观的,可以根据个人的观点和标准而有所不同。
#调用多个提示
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="\nWho is the best cricket player in the world in 2016?\nThe best cricket player in the world in 2016 is Virat Kohli. The Indian captain has had a fabulous year, scoring heavily in all formats of the game, leading India to several victories, and breaking several records. In Test cricket, Kohli has scored 1215 runs at an average of 75.33 with 6 centuries and 4 fifties, which is the highest number of runs scored by any player in a calendar year. In ODI cricket, he has scored 1143 runs at an average of 83.42 with 7 centuries and 6 fifties, which is also the highest number of runs scored by any player in a calendar year. Additionally, Kohli has led India to the number one ranking in Test cricket, and has been named the ICC Test Player of the Year and the ICC ODI Player of the Year.\nVirat Kohli has been in incredible form in 2016, and his performances have made him the standout player of the year. Other players who have had a great year include Steve Smith, Joe Root, and Kane Williamson, but Kohli's consistency and dominance in all formats of the game make him the best cricket player in the world in 2016.", generation_info=None)], [Generation(text="\n\nA: LeBron James.\n\nB: Kevin Durant.\n\nC: Steph Curry.\n\nD: James Harden.\n\nE: Other (please specify).\n\nWhat's your answer?", generation_info=None)]] llm_output={'token_usage': {}, 'model_id': 'fireworks-llama-v2-13b-chat'} run=[RunInfo(run_id=UUID('d14b6bee-7692-46ad-8798-acb6f72fc7fb')), RunInfo(run_id=UUID('b9f5b3b5-9e62-4eaf-b269-ecf0cbbcfb82'))]
#设置参数:model_id、temperature、max_tokens、top_p
llm = Fireworks(model_id="accounts/fireworks/models/fireworks-llama-v2-13b-chat", temperature=0.7, max_tokens=15, top_p=1.0)
print(llm("What's the weather like in Kansas City in December?"))
    
十二月份的堪萨斯城天气可能会相当寒冷,平均最高温度

创建和运行链

创建一个用于LLM链的提示模板。一旦创建了此提示模板,就可以使用LLM和提示模板初始化链,并使用指定的提示运行链。

human_message_prompt = HumanMessagePromptTemplate(
prompt=PromptTemplate(
template="What is a good name for a company that makes {product}?",
input_variables=["product"],
)
)

chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])
chat = Fireworks()
chain = LLMChain(llm=chat, prompt=chat_prompt_template)
output = chain.run("football helmets")

print(output)
    
(注意:我只是一个AI,不是品牌专家,所以把这个作为您自己研究和头脑风暴的起点。)
一个制造橄榄球头盔的公司的好名字可能是:

1. Helix Pro:这个名字与橄榄球头盔常见的螺旋形状相呼应。“Pro”意味着专业级产品。
2. Gridiron Gear: “Gridiron”是用来描述橄榄球场的术语,“Gear”强调了公司专注于生产高质量橄榄球头盔。
3. Linebacker Lab: “Linebacker”是橄榄球场上的一个位置,“Lab”暗示着对研究和开发的关注。
4. Helmet Hut:这个名字简单易记,立即传达了公司专注于橄榄球头盔的信息。
5. Tackle Tech: “Tackle”是橄榄球中用来描述撞击或碰撞的术语,“Tech”暗示了对先进技术和创新的关注。
6. Victory Vest: “Victory”暗示着对胜利和成功的关注,“Vest”可能暗示了一种保护性或装甲设计。
7. Pigskin Pro: “Pigskin”是用来描述橄榄球的术语,“Pro”意味着专业级产品。
8. Football Fusion: 这个名字可能暗示了使用不同材料或技术的组合来创建高质量的橄榄球头盔。
9. Endzone Edge: “Endzone”是橄榄球场上一个队伍得分Touchdown的区域,“Edge”暗示了竞争优势。
10. MVP Masks: “MVP”代表“最有价值球员”,“Masks”突出了公司橄榄球头盔的保护性质。

请记住,您为公司选择的名称应该易记、易发音和易拼写,并传达出优质和专业的感觉。还要确保所选名称没有被其他公司使用,并考虑任何潜在的商标问题。