{ "cells": [ { "cell_type": "raw", "id": "529aeba9", "metadata": {}, "source": [ "---\n", "sidebar_label: Fireworks\n", "---" ] }, { "attachments": {}, "cell_type": "markdown", "id": "642fd21c-600a-47a1-be96-6e1438b421a9", "metadata": {}, "source": [ "# ChatFireworks\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 `ChatFireworks` models." ] }, { "cell_type": "raw", "id": "4a7c795e", "metadata": {}, "source": [ "%pip install langchain-fireworks" ] }, { "cell_type": "code", "execution_count": 1, "id": "d00d850917865298", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "from langchain_core.messages import HumanMessage, SystemMessage\n", "from langchain_fireworks import ChatFireworks" ] }, { "cell_type": "markdown", "id": "f28ebf8b-f14f-46c7-9962-8b8dc42e31be", "metadata": {}, "source": [ "# Setup\n", "\n", "1. Make sure the `langchain-fireworks` package is installed in your environment.\n", "2. Sign in to [Fireworks AI](http://fireworks.ai) for the an API Key to access our models, and make sure it is set as the `FIREWORKS_API_KEY` environment variable.\n", "3. Set up your model using a model id. If the model is not set, the default model is fireworks-llama-v2-7b-chat. See the full, most up-to-date model list on [app.fireworks.ai](https://app.fireworks.ai)." ] }, { "cell_type": "code", "execution_count": 2, "id": "d096fb14-8acc-4047-9cd0-c842430c3a1d", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if \"FIREWORKS_API_KEY\" not in os.environ:\n", " os.environ[\"FIREWORKS_API_KEY\"] = getpass.getpass(\"Fireworks API Key:\")\n", "\n", "# Initialize a Fireworks chat model\n", "chat = ChatFireworks(model=\"accounts/fireworks/models/mixtral-8x7b-instruct\")" ] }, { "cell_type": "markdown", "id": "d8f13144-37cf-47a5-b5a0-e3cdf76d9a72", "metadata": {}, "source": [ "# Calling the Model Directly\n", "\n", "You can call the model directly with a system and human message to get answers." ] }, { "cell_type": "code", "execution_count": 3, "id": "72340871-ae2f-415f-b399-0777d32dc379", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content=\"Hello! I'm an AI language model, a helpful assistant designed to chat and assist you with any questions or information you might need. I'm here to make your experience as smooth and enjoyable as possible. How can I assist you today?\")" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ChatFireworks Wrapper\n", "system_message = SystemMessage(content=\"You are to chat with the user.\")\n", "human_message = HumanMessage(content=\"Who are you?\")\n", "\n", "chat.invoke([system_message, human_message])" ] }, { "cell_type": "code", "execution_count": 5, "id": "68c6b1fa-2ff7-4a63-8d88-3cec302180b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content=\"I'm an AI and do not have the ability to experience the weather firsthand. However,\")" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Setting additional parameters: temperature, max_tokens, top_p\n", "chat = ChatFireworks(\n", " model=\"accounts/fireworks/models/mixtral-8x7b-instruct\",\n", " temperature=1,\n", " max_tokens=20,\n", ")\n", "system_message = SystemMessage(content=\"You are to chat with the user.\")\n", "human_message = HumanMessage(content=\"How's the weather today?\")\n", "chat.invoke([system_message, human_message])" ] }, { "cell_type": "markdown", "id": "8c44cb36", "metadata": {}, "source": [ "# Tool Calling\n", "\n", "Fireworks offers the [`FireFunction-v1` tool calling model](https://fireworks.ai/blog/firefunction-v1-gpt-4-level-function-calling). You can use it for structured output and function calling use cases:" ] }, { "cell_type": "code", "execution_count": 11, "id": "ee2db682", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'function': {'arguments': '{\"name\": \"Erick\", \"age\": 27}',\n", " 'name': 'ExtractFields'},\n", " 'id': 'call_J0WYP2TLenaFw3UeVU0UnWqx',\n", " 'index': 0,\n", " 'type': 'function'}\n" ] } ], "source": [ "from pprint import pprint\n", "\n", "from langchain_core.pydantic_v1 import BaseModel\n", "\n", "\n", "class ExtractFields(BaseModel):\n", " name: str\n", " age: int\n", "\n", "\n", "chat = ChatFireworks(\n", " model=\"accounts/fireworks/models/firefunction-v1\",\n", ").bind_tools([ExtractFields])\n", "\n", "result = chat.invoke(\"I am a 27 year old named Erick\")\n", "\n", "pprint(result.additional_kwargs[\"tool_calls\"][0])" ] }, { "cell_type": "code", "execution_count": null, "id": "2321a4e6", "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 }