mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
6dd44ff1c0
Description * Refactor Fireworks within Langchain LLMs. * Remove FireworksChat within Langchain LLMs. * Add ChatFireworks (which uses chat completion api) to Langchain chat models. * Users have to install `fireworks-ai` and register an api key to use the api. Issue - Not applicable Dependencies - None Tag maintainer - @rlancemartin @baskaryan
320 lines
15 KiB
Plaintext
320 lines
15 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cc6caafa",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Fireworks\n",
|
|
"\n",
|
|
">[Fireworks](https://app.fireworks.ai/) accelerates product development on generative AI by creating an innovative AI experiment and production platform. \n",
|
|
"\n",
|
|
"This example goes over how to use LangChain to interact with `Fireworks` models."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "60b6dbb2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.llms.fireworks import Fireworks\n",
|
|
"from langchain.prompts import PromptTemplate\nfrom langchain.chains import LLMChain\n",
|
|
"from langchain.prompts.chat import (\n",
|
|
" ChatPromptTemplate,\n",
|
|
" HumanMessagePromptTemplate,\n",
|
|
")\n",
|
|
"import os"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ccff689e",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Setup\n",
|
|
"\n",
|
|
"Contact Fireworks AI for the an API Key to access our models\n",
|
|
"\n",
|
|
"Set up your model using a model id. If the model is not set, the default model is fireworks-llama-v2-7b-chat."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "9ca87a2e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Initialize a Fireworks LLM\n",
|
|
"os.environ['FIREWORKS_API_KEY'] = \"<your_api_key>\" # Change this to your own API key\n",
|
|
"llm = Fireworks(model=\"accounts/fireworks/models/llama-v2-13b-chat\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "acc24d0c",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Calling the Model\n",
|
|
"\n",
|
|
"You can use the LLMs to call the model for specified prompt(s). \n",
|
|
"\n",
|
|
"See the full, most up-to-date model list on [app.fireworks.ai](https://app.fireworks.ai)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "bf0a425c",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"It's a question that's been debated for years, and there are plenty of strong candidates. Here are some of the top quarterbacks in the league right now:\n",
|
|
"\n",
|
|
"1. Tom Brady (New England Patriots): Brady is widely considered one of the greatest quarterbacks of all time, and for good reason. He's led the Patriots to six Super Bowl wins and has been named Super Bowl MVP four times. He's known for his precision passing and ability to read defenses.\n",
|
|
"2. Aaron Rodgers (Green Bay Packers): Rodgers is another top-tier quarterback who's known for his accuracy and ability to make plays outside of the pocket. He's led the Packers to a Super Bowl win and has been named NFL MVP twice.\n",
|
|
"3. Drew Brees (New Orleans Saints): Brees is one of the most prolific passers in NFL history, and he's shown no signs of slowing down. He's led the Saints to a Super Bowl win and has been named NFL MVP once.\n",
|
|
"4. Russell Wilson (Seattle Seahawks): Wilson is a dynamic quarterback who's known for his ability to make plays with his legs and his arm. He's led the Seahawks to a Super Bowl win and has been named NFL MVP once.\n",
|
|
"5. Patrick Mahomes (Kansas City Chiefs): Mahomes is a young quarterback who's quickly become one of the best in the league. He led the Chiefs to a Super Bowl win last season and has been named NFL MVP twice. He's known for his incredible arm talent and ability to make plays outside of the pocket.\n",
|
|
"\n",
|
|
"Of course, there are other great quarterbacks in the league as well, such as Ben Roethlisberger, Matt Ryan, and Deshaun Watson. Ultimately, the \"best\" quarterback is a matter of personal opinion and depends on how you define \"best.\" Some people might value accuracy and precision passing, while others might prefer a quarterback who can make plays with their legs. Either way, the NFL is filled with talented quarterbacks who are making incredible plays every week.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Single prompt\n",
|
|
"output = llm(\"Who's the best quarterback in the NFL?\")\n",
|
|
"print(output)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "afc7de6f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[[Generation(text=\"\\n\\nNote: This is a subjective question, and the answer will depend on individual opinions and perspectives.\\n\\nThere are many great cricket players, and it's difficult to identify a single best player. However, here are some of the top performers in 2016:\\n\\n1. Virat Kohli (India): Kohli had an outstanding year in all formats of the game, scoring heavily in Tests, ODIs, and T20Is. He was especially impressive in the Test series against England, where he scored four centuries and averaged over 100.\\n2. Steve Smith (Australia): Smith had a phenomenal year as well, leading Australia to a Test series win in India and averaging over 100 in the longer format. He also scored a century in the ODI series against Pakistan.\\n3. Kane Williamson (New Zealand): Williamson had a consistent year, scoring heavily in all formats and leading New Zealand to a Test series win against Australia. He also won the ICC Test Player of the Year award.\\n4. Joe Root (England): Root had a solid year, scoring three hundreds in the Test series against Pakistan and India, and averaging over 50 in Tests.\\n5. AB de Villiers (South Africa): De Villiers had a brilliant year in ODIs, scoring four hundreds and averaging over 100. He also had a good year in Tests, scoring two hundreds and averaging over 50.\\n6. Quinton de Kock (South Africa): De Kock had a great year behind the wickets, scoring heavily in all formats and averaging over 50 in Tests.\\n7. Rohit Sharma (India): Sharma had a fantastic year in ODIs, scoring four hundreds and averaging over 100. He also had a good year in Tests, scoring two hundreds and averaging over 40.\\n8. David Warner (Australia): Warner had a great year in ODIs, scoring three hundreds and averaging over 100. He also had a good year in Tests, scoring two hundreds and averaging over 40.\\n\\nThese are just a few examples of top performers in 2016, and opinions on the best player will vary depending on individual perspectives\", generation_info=None)], [Generation(text='\\n\\nThere are a lot of great players in the NBA, and opinions on who\\'s the best can vary depending on personal preferences and criteria for evaluation. However, here are some of the top candidates for the title of best basketball player in the league based on their recent performances and achievements:\\n\\n1. LeBron James: James is a four-time NBA champion and four-time MVP, and is widely regarded as one of the greatest players of all time. He has led the Los Angeles Lakers to the best record in the Western Conference this season and is averaging 25.7 points, 7.9 rebounds, and 7.4 assists per game.\\n2. Giannis Antetokounmpo: Antetokounmpo, known as the \"Greek Freak,\" is a dominant force in the paint and has led the Milwaukee Bucks to the best record in the Eastern Conference. He is averaging 30.5 points, 12.6 rebounds, and 5.9 assists per game, and is a strong contender for the MVP award.\\n3. Stephen Curry: Curry is a three-time NBA champion and two-time MVP, and is known for his incredible shooting ability. He has led the Golden State Warriors to the playoffs despite injuries to key players, and is averaging 23.5 points, 5.2 rebounds, and 5.2 assists per game.\\n4. Kevin Durant: Durant is a two-time NBA champion and four-time scoring champion, and is one of the most skilled scorers in the league. He has led the Brooklyn Nets to the playoffs in their first season since moving from New Jersey, and is averaging 27.2 points, 7.2 rebounds, and 6.4 assists per game.\\n5. James Harden: Harden is a three-time scoring champion and has led the Houston Rockets to the playoffs for the past eight seasons. He is averaging 35.4 points, 8.3 rebounds, and 7.5 assists per game, and is a strong contender for the MVP award.\\n\\nUltimately, determining the best basketball player in the league is subjective and depends on individual opinions and criteria. However, these five players are among', generation_info=None)]]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Calling multiple prompts\n",
|
|
"output = llm.generate([\n",
|
|
" \"Who's the best cricket player in 2016?\",\n",
|
|
" \"Who's the best basketball player in the league?\",\n",
|
|
"])\n",
|
|
"print(output.generations)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "b801c20d",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"Kansas City's weather in December can be quite chilly,\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Setting additional parameters: temperature, max_tokens, top_p\n",
|
|
"llm = Fireworks(model=\"accounts/fireworks/models/llama-v2-13b-chat\", model_kwargs={\"temperature\":0.7, \"max_tokens\":15, \"top_p\":1.0})\n",
|
|
"print(llm(\"What's the weather like in Kansas City in December?\"))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "137662a6",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Create and Run Chain\n",
|
|
"\n",
|
|
"Create a prompt template to be used with the LLM Chain. Once this prompt template is created, initialize the chain with the LLM and prompt template, and run the chain with the specified prompts."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "fd2c6bc1",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"Assistant: That's a great question! There are many factors to consider when choosing a name for a company that makes football helmets. Here are a few suggestions:\n",
|
|
"\n",
|
|
"1. Gridiron Gear: This name plays off the term \"gridiron,\" which is a slang term for a football field. It also suggests that the company's products are high-quality and durable, like gear used in a gridiron game.\n",
|
|
"2. Helmet Headquarters: This name is straightforward and to the point. It clearly communicates that the company is a leading manufacturer of football helmets.\n",
|
|
"3. Tackle Tough: This name plays off the idea of tackling a tough opponent on the football field. It suggests that the company's helmets are designed to protect players from even the toughest hits.\n",
|
|
"4. Block Breakthrough: This name is a play on words that suggests the company's helmets are breaking through the competition. It also implies that the company is innovative and forward-thinking.\n",
|
|
"5. First Down Fashion: This name combines the idea of scoring a first down on the football field with the idea of fashionable clothing. It suggests that the company's helmets are not only functional but also stylish.\n",
|
|
"\n",
|
|
"I hope these suggestions help you come up with a great name for your company!\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"human_message_prompt = HumanMessagePromptTemplate.from_template(\"What is a good name for a company that makes {product}?\")\n",
|
|
"chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])\n",
|
|
"chat = Fireworks()\n",
|
|
"chain = LLMChain(llm=chat, prompt=chat_prompt_template)\n",
|
|
"output = chain.run(\"football helmets\")\n",
|
|
"\n",
|
|
"print(output)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "25812db3-23a6-41dd-8636-5a49c52bb6eb",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Run Stream"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "26d67ecf-9290-4ec2-8b39-ff17fc99620f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" Tom Brady, Aaron Rod\n",
|
|
"gers, or Drew Bre\n",
|
|
"es?\n",
|
|
"Some people might\n",
|
|
" say Tom Brady, who\n",
|
|
" has won six Super Bowls\n",
|
|
" and four Super Bowl MVP\n",
|
|
" awards, is the best quarter\n",
|
|
"back in the NFL. O\n",
|
|
"thers might argue that Aaron\n",
|
|
" Rodgers, who has led\n",
|
|
" his team to a Super Bowl\n",
|
|
" victory and has been named the\n",
|
|
" NFL MVP twice, is\n",
|
|
" the best. Still, others\n",
|
|
" might say that Drew Bre\n",
|
|
"es, who holds the NFL\n",
|
|
" record for most career passing yards\n",
|
|
" and has led his team to\n",
|
|
" a Super Bowl victory, is\n",
|
|
" the best.\n",
|
|
"But what\n",
|
|
" if I told you there'\n",
|
|
"s actually a fourth quarterback\n",
|
|
" who could make a strong case\n",
|
|
" for being the best in the\n",
|
|
" NFL? Meet Russell Wilson\n",
|
|
", the Seattle Seahaw\n",
|
|
"ks' dynamic signal-call\n",
|
|
"er who has led his team\n",
|
|
" to a Super Bowl victory and\n",
|
|
" has been named the NFL M\n",
|
|
"VP twice.\n",
|
|
"Wilson\n",
|
|
" has a unique combination of physical\n",
|
|
" and mental skills that set him\n",
|
|
" apart from other quarterbacks\n",
|
|
" in the league. He'\n",
|
|
"s incredibly athletic,\n",
|
|
" with the ability to make plays\n",
|
|
" with his feet and his arm\n",
|
|
", and he's also\n",
|
|
" highly intelligent, with a\n",
|
|
" quick mind and the ability to\n",
|
|
" read defenses like a pro\n",
|
|
".\n",
|
|
"But what really\n",
|
|
" sets Wilson apart is his\n",
|
|
" leadership ability. He'\n",
|
|
"s a natural-born\n",
|
|
" leader who has a way\n",
|
|
" of inspiring his team\n",
|
|
"mates and getting them\n",
|
|
" to buy into his vision\n",
|
|
" for the game. He\n",
|
|
"'s also an excellent\n",
|
|
" communicator, who can\n",
|
|
" articulate his strategy\n",
|
|
" and game plan in a\n",
|
|
" way that his teamm\n",
|
|
"ates can understand and execute\n",
|
|
".\n",
|
|
"So, who\n",
|
|
"'s the best quarter\n",
|
|
"back in the NFL?\n",
|
|
" It's hard to\n",
|
|
" say for sure, but\n",
|
|
" if you ask me,\n",
|
|
" Russell Wilson is definitely in\n",
|
|
" the conversation. He'\n",
|
|
"s got the physical skills\n",
|
|
", the mental skills,\n",
|
|
" and the leadership ability to\n",
|
|
" be the best of the\n",
|
|
" best.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"llm = Fireworks()\n",
|
|
"generator = llm.stream(\"Who's the best quarterback in the NFL?\")\n",
|
|
"\n",
|
|
"for token in generator:\n",
|
|
" print(token)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e3a35e0b-c875-493a-8143-d802d273247c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|