{ "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": 25, "id": "60b6dbb2", "metadata": {}, "outputs": [], "source": [ "from langchain.llms.fireworks import Fireworks, FireworksChat\n", "from langchain import PromptTemplate, 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": 5, "id": "9ca87a2e", "metadata": {}, "outputs": [], "source": [ "# Initialize a Fireworks LLM\n", "os.environ['FIREWORKS_API_KEY'] = \"\" #change this to your own API KEY\n", "llm = Fireworks(model_id=\"accounts/fireworks/models/fireworks-llama-v2-13b-chat\")" ] }, { "cell_type": "code", "execution_count": 19, "id": "43a11ba8", "metadata": {}, "outputs": [], "source": [ "# Create LLM chain\n", "llm_chain = LLMChain(prompt=prompt, llm=llm)" ] }, { "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", "Current Specified Models: \n", "* accounts/fireworks/models/fireworks-falcon-7b, accounts/fireworks/models/fireworks-falcon-40b-w8a16\n", "* accounts/fireworks/models/fireworks-starcoder-1b-w8a16-1gpu, accounts/fireworks/models/fireworks-starcoder-3b-w8a16-1gpu, accounts/fireworks/models/fireworks-starcoder-7b-w8a16-1gpu, accounts/fireworks/models/fireworks-starcoder-16b-w8a16 \n", "* accounts/fireworks/models/fireworks-llama-v2-13b, accounts/fireworks/models/fireworks-llama-v2-13b-chat, accounts/fireworks/models/fireworks-llama-v2-13b-w8a16, accounts/fireworks/models/fireworks-llama-v2-13b-chat-w8a16\n", "* accounts/fireworks/models/fireworks-llama-v2-7b, accounts/fireworks/models/fireworks-llama-v2-7b-chat, accounts/fireworks/models/fireworks-llama-v2-7b-w8a16, accounts/fireworks/models/fireworks-llama-v2-7b-chat-w8a16, accounts/fireworks/models/fireworks-llama-v2-70b-chat-4gpu" ] }, { "cell_type": "code", "execution_count": 10, "id": "bf0a425c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "It's a question that has been debated for years, with different analysts and fans making their cases for various signal-callers. Here are some of the top contenders for the title of best quarterback in the NFL:\n", "\n", "1. Tom Brady: The New England Patriots legend has won six Super Bowls and has been named Super Bowl MVP four times. He's known for his precision passing, pocket presence, and ability to read defenses.\n", "2. Aaron Rodgers: The Green Bay Packers quarterback has won two Super Bowls and has been named NFL MVP twice. He's known for his quick release, accuracy, and ability to extend plays with his feet.\n", "3. Drew Brees: The New Orleans Saints quarterback has won a Super Bowl and has been named NFL MVP once. He's known for his accuracy, pocket presence, and ability to read defenses.\n", "4. Patrick Mahomes: The Kansas City Chiefs quarterback has won a Super Bowl and has been named NFL MVP twice. He's known for his arm strength, athleticism, and ability to make plays outside of the pocket.\n", "5. Russell Wilson: The Seattle Seahawks quarterback has won a Super Bowl and has been named NFL MVP once. He's known for his mobility, accuracy, and ability to extend plays with his feet.\n", "\n", "Of course, there are other talented quarterbacks in the league, such as Lamar Jackson, Deshaun Watson, and Carson Wentz, who could also be considered among the best. Ultimately, the answer to the question of who's the best quarterback in the NFL is subjective and can vary depending on individual perspectives and criteria.\n" ] } ], "source": [ "#single prompt\n", "output = llm(\"Who's the best quarterback in the NFL?\")\n", "print(output)" ] }, { "cell_type": "code", "execution_count": 12, "id": "afc7de6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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'))]\n" ] } ], "source": [ "#calling multiple prompts\n", "output = llm.generate([\"Who's the best cricket player in 2016?\", \"Who's the best basketball player in the league?\"])\n", "print(output)" ] }, { "cell_type": "code", "execution_count": 17, "id": "b801c20d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Kansas City in December can be quite chilly, with average high\n" ] } ], "source": [ "#setting parameters: model_id, temperature, max_tokens, top_p\n", "llm = Fireworks(model_id=\"accounts/fireworks/models/fireworks-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": 26, "id": "fd2c6bc1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(Note: I'm just an AI and not a branding expert, so take this as a starting point for your own research and brainstorming.)\n", "A good name for a company that makes football helmets could be:\n", "\n", "1. Helix Pro: This name plays off the idea of a helix, or spiral, shape that is commonly associated with football helmets. \"Pro\" implies a professional-level product.\n", "2. Gridiron Gear: \"Gridiron\" is a term used to describe a football field, and \"gear\" highlights the company's focus on producing high-quality football helmets.\n", "3. Linebacker Lab: \"Linebacker\" is a position on the football field, and \"Lab\" suggests a focus on research and development.\n", "4. Helmet Hut: This name is simple and easy to remember, and it immediately conveys the company's focus on football helmets.\n", "5. Tackle Tech: \"Tackle\" is a term used in football to describe a hit or collision, and \"Tech\" implies a focus on advanced technology and innovation.\n", "6. Victory Vest: \"Victory\" implies a focus on winning and success, and \"Vest\" could suggest a protective or armored design.\n", "7. Pigskin Pro: \"Pigskin\" is a term used to describe a football, and \"Pro\" implies a professional-level product.\n", "8. Football Fusion: This name could suggest a combination of different materials or technologies to create a high-quality football helmet.\n", "9. Endzone Edge: \"Endzone\" is the area of the football field where a team scores a touchdown, and \"Edge\" implies a competitive advantage.\n", "10. MVP Masks: \"MVP\" stands for \"Most Valuable Player,\" and \"Masks\" highlights the protective nature of the company's football helmets.\n", "\n", "Remember, the name you choose for your company should be memorable, easy to pronounce and spell, and convey a sense of quality and professionalism. It's also important to check that the name isn't already in use by another company, and to consider any potential trademark issues.\n" ] } ], "source": [ "human_message_prompt = HumanMessagePromptTemplate(\n", " prompt=PromptTemplate(\n", " template=\"What is a good name for a company that makes {product}?\",\n", " input_variables=[\"product\"],\n", " )\n", ")\n", "\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)" ] } ], "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }