{
 "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, FireworksChat\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_id=\"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",
    "Currently supported models: \n",
    "\n",
    "*   Falcon\n",
    "    *   `accounts/fireworks/models/falcon-7b`\n",
    "    *   `accounts/fireworks/models/falcon-40b-w8a16`\n",
    "*   Llama 2\n",
    "    *   `accounts/fireworks/models/llama-v2-7b`\n",
    "    *   `accounts/fireworks/models/llama-v2-7b-w8a16`\n",
    "    *   `accounts/fireworks/models/llama-v2-7b-chat`\n",
    "    *   `accounts/fireworks/models/llama-v2-7b-chat-w8a16`\n",
    "    *   `accounts/fireworks/models/llama-v2-13b`\n",
    "    *   `accounts/fireworks/models/llama-v2-13b-w8a16`\n",
    "    *   `accounts/fireworks/models/llama-v2-13b-chat`\n",
    "    *   `accounts/fireworks/models/llama-v2-13b-chat-w8a16`\n",
    "    *   `accounts/fireworks/models/llama-v2-70b-chat-4gpu`\n",
    "*   StarCoder\n",
    "    *   `accounts/fireworks/models/starcoder-1b-w8a16-1gpu`\n",
    "    *   `accounts/fireworks/models/starcoder-3b-w8a16-1gpu`\n",
    "    *   `accounts/fireworks/models/starcoder-7b-w8a16-1gpu`\n",
    "    *   `accounts/fireworks/models/starcoder-16b-w8a16`\n",
    "\n",
    "See the full, most up-to-date 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": [
      "Is it Tom Brady, Aaron Rodgers, or someone else? It's a tough question to answer, and there are strong arguments for each of these quarterbacks. Here are some of the reasons why each of these quarterbacks could be considered the best:\n",
      "\n",
      "Tom Brady:\n",
      "\n",
      "* He has the most Super Bowl wins (6) of any quarterback in NFL history.\n",
      "* He has been named Super Bowl MVP four times, more than any other player.\n",
      "* He has led the New England Patriots to 18 playoff victories, the most in NFL history.\n",
      "* He has thrown for over 70,000 yards in his career, the most of any quarterback in NFL history.\n",
      "* He has thrown for 50 or more touchdowns in a season four times, the most of any quarterback in NFL history.\n",
      "\n",
      "Aaron Rodgers:\n",
      "\n",
      "* He has led the Green Bay Packers to a Super Bowl victory in 2010.\n",
      "* He has been named Super Bowl MVP once.\n",
      "* He has thrown for over 40,000 yards in his career, the most of any quarterback in NFL history.\n",
      "* He has thrown for 40 or more touchdowns in a season three times, the most of any quarterback in NFL history.\n",
      "* He has a career passer rating of 103.1, the highest of any quarterback in NFL history.\n",
      "\n",
      "So, who's the best quarterback in the NFL? It's a tough call, but here's my opinion:\n",
      "\n",
      "I think Aaron Rodgers is the best quarterback in the NFL right now. He has led the Packers to a Super Bowl victory and has had some incredible seasons, including the 2011 season when he threw for 45 touchdowns and just 6 interceptions. He has a strong arm, great accuracy, and is incredibly mobile for a quarterback of his size. He also has a great sense of timing and knows when to take risks and when to play it safe.\n",
      "\n",
      "Tom Brady is a close second, though. He has an incredible track record of success, including six Super Bowl victories, and has been one of the most consistent quarterbacks in the league for the past two decades. He has a strong arm and is incredibly accurate\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='\\nThe best cricket player in 2016 is a matter of opinion, but some of the top contenders for the title include:\\n\\n1. Virat Kohli (India): Kohli had a phenomenal year in 2016, scoring over 1,000 runs in Test cricket, including four centuries, and averaging over 70. He also scored heavily in ODI cricket, with an average of over 80.\\n2. Steve Smith (Australia): Smith had a remarkable year in 2016, leading Australia to a Test series victory in India and scoring over 1,000 runs in the format, including five centuries. He also averaged over 60 in ODI cricket.\\n3. KL Rahul (India): Rahul had a breakout year in 2016, scoring over 1,000 runs in Test cricket, including four centuries, and averaging over 60. He also scored heavily in ODI cricket, with an average of over 70.\\n4. Joe Root (England): Root had a solid year in 2016, scoring over 1,000 runs in Test cricket, including four centuries, and averaging over 50. He also scored heavily in ODI cricket, with an average of over 80.\\n5. Quinton de Kock (South Africa): De Kock had a remarkable year in 2016, scoring over 1,000 runs in ODI cricket, including six centuries, and averaging over 80. He also scored heavily in Test cricket, with an average of over 50.\\n\\nThese are just a few of the top contenders for the title of best cricket player in 2016, but there were many other talented players who also had impressive years. Ultimately, the answer to this question is subjective and depends on individual opinions and criteria for evaluation.', generation_info=None)], [Generation(text=\"\\nThis is a tough one, as there are so many great players in the league right now. But if I had to choose one, I'd say LeBron James is the best basketball player in the league. He's a once-in-a-generation talent who can dominate the game in so many ways. He's got incredible speed, strength, and court vision, and he's always finding new ways to improve his game. Plus, he's been doing it at an elite level for over a decade now, which is just amazing.\\n\\nBut don't just take my word for it - there are plenty of other great players in the league who could make a strong case for being the best. Guys like Kevin Durant, Steph Curry, James Harden, and Giannis Antetokounmpo are all having incredible seasons, and they've all got their own unique skills and strengths that make them special. So ultimately, it's up to you to decide who you think is the best basketball player in the league.\", 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 in December is quite cold, with temperatures typically r\n"
     ]
    }
   ],
   "source": [
    "# Setting additional parameters: temperature, max_tokens, top_p\n",
    "llm = Fireworks(model_id=\"accounts/fireworks/models/llama-v2-13b-chat\", 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",
      "Naming a company can be a fun and creative process! Here are a few name ideas for a company that makes football helmets:\n",
      "\n",
      "1. Helix Headgear: This name plays off the idea of the helix shape of a football helmet and could be a memorable and catchy name for a company.\n",
      "2. Gridiron Gear: \"Gridiron\" is a term used to describe a football field, and \"gear\" refers to the products the company sells. This name is straightforward and easy to understand.\n",
      "3. Cushion Crusaders: This name emphasizes the protective qualities of football helmets and could appeal to customers looking for safety-conscious products.\n",
      "4. Helmet Heroes: This name has a fun, heroic tone and could appeal to customers looking for high-quality products.\n",
      "5. Tackle Tech: \"Tackle\" is a term used in football to describe a player's attempt to stop an opponent, and \"tech\" refers to the technology used in the helmets. This name could appeal to customers interested in innovative products.\n",
      "6. Padded Protection: This name emphasizes the protective qualities of football helmets and could appeal to customers looking for products that prioritize safety.\n",
      "7. Gridiron Gear Co.: This name is simple and straightforward, and it clearly conveys the company's focus on football-related products.\n",
      "8. Helmet Haven: This name has a soothing, protective tone and could appeal to customers looking for a reliable brand.\n",
      "\n",
      "Remember to choose a name that reflects your company's values and mission, and that resonates with your target market. Good luck with 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 = FireworksChat()\n",
    "chain = LLMChain(llm=chat, prompt=chat_prompt_template)\n",
    "output = chain.run(\"football helmets\")\n",
    "\n",
    "print(output)"
   ]
  }
 ],
 "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
}
