Notebooks
F
Fireworks AI
Fireworks Firefunction Openai Qa

Fireworks Firefunction Openai Qa

fireworks-aifireworks-cookbookarchivedlearntutorialsmachine-learningjupyter-notebooksfunction-callingai-workflowsnotebooks_firefunction_openaiintegrationsproduction-examplesgenerative-aiagentic-systemsrag

Open in Colab

Structured answers with Fireworks functions

Several real world applications of LLM require them to respond in a structured manner. This structured response could look like JSON or YAML. For e.g. answering research questions using arxiv along with citations. Instead of parsing the entire LLM response and trying to figure out the actual answer of the LLM vs the citations provided by the LLM, we can use function calling ability of the LLMs to answer questions in a structured way.

In this notebook, we demonstrate structured response generation ability of the Fireworks function calling model. We will build an application that can answer questions (along with citations) regarding the State of the Union speech of 2022.

🚀 Running This Notebook

This notebook is designed to be run in Google Colab for a seamless experience. If you prefer to run it locally, please follow the setup instructions below.

To Running Locally

To run this notebook locally, make sure to:

  1. Set up a Python virtual environment.
  2. Install the required libraries (openai, jupyter, and python-dotenv).
  3. Configure your API key and launch the Jupyter Notebook server.

You can find detailed setup instructions in the following cells.

Local Setup Instructions

To run this notebook locally, follow these steps:

Step 1: Create a Virtual Environment

In your terminal, navigate to the directory where this notebook is located and run:

python3 -m venv venv
source venv/bin/activate  # On macOS/Linux
.env\Scriptsctivate   # On Windows

Step 2: Install Required Libraries

Install the necessary packages:

pip install jupyter openai python-dotenv

Step 3: Set Up Your API Key

You can set your API key in the terminal:

  • On macOS/Linux:
    export FIREWORKS_API_KEY=<YOUR_FIREWORKS_API_KEY>
    
  • On Windows:
    set FIREWORKS_API_KEY=<YOUR_FIREWORKS_API_KEY>
    

Alternatively, create a .env file in the project directory with:

FIREWORKS_API_KEY=<YOUR_FIREWORKS_API_KEY>

Load the .env file in your Python code with:

from dotenv import load_dotenv
load_dotenv()

Step 4: Launch Jupyter Notebook

Start the Jupyter Notebook server:

jupyter notebook

Open this notebook file (fireworks_demo.ipynb) and proceed to run the cells.

How Function Calling Works

The function-calling process involves the following steps:

  1. Define User Query and Tools: Specify the user query and the available tools using the messages and tools arguments.
  2. Model Decision: The model determines whether to respond directly or generate a tool call based on the user query.
  3. User Executes Tool Call: If the model generates a tool call, the user must execute the function manually and provide the result back to the model.
  4. Response Generation: The model uses the tool call result to generate a final response.

For more details, refer to:

Setup

Install all the dependencies and import the required python modules.

[ ]
[9]

Download & Clean the Content

We are going to download the content using the python package requests and perform minor cleanup by removing several newlines. Even minimal cleanup should be good enough to obtain good results with the model.

Downloading the Document

[10]

Cleaning Up the Content

Minor cleanup is performed by removing extra newlines:

[11]
[12]

Setup your API Key

In order to use the Fireworks AI function calling model, you must first obtain Fireworks API Keys. If you don't already have one, you can one by following the instructions here.

[13]

Define the Structure

Let's define the strucutre in which we want our model to responsd. The JSON structure for function calling follows the conventions of JSON Schema. Here we define a structure with answer and citations field.

[14]

Perform Sanity Test

Let's perform a sanity test by querying the speech for some basic information. This would ensure that our model setup is working correctly and the document is being processed correctly.

[15]
[16]
[17]
{
    "content": null,
    "refusal": null,
    "role": "assistant",
    "function_call": null,
    "tool_calls": [
        {
            "id": "call_kIpcwu7koMRkUtkhVvxuuQQu",
            "function": {
                "arguments": "{\"answer\": \"The President mentioned Ketanji Brown Jackson as the first Black woman to serve on the Supreme Court.\", \"sources\": [\"2022 State of the Union speech\"]}",
                "name": "get_answer_with_sources"
            },
            "type": "function",
            "index": 0
        }
    ]
}
[18]

Using Function Calling in Conversation

Our model currently support multi-turn conversation when using function calling. You can reference previous completions generated by the model to ask more clarifying questions.

[19]
[20]
{
    "content": null,
    "refusal": null,
    "role": "assistant",
    "function_call": null,
    "tool_calls": [
        {
            "id": "call_tdAos9CIlEKaq1ym4y6WvlL0",
            "function": {
                "arguments": "{\"answer\": \"The President mentioned Justice Stephen Breyer, who retired from the Supreme Court, making way for Ketanji Brown Jackson to take his seat.\", \"sources\": [\"2022 State of the Union speech\"]}",
                "name": "get_answer_with_sources"
            },
            "type": "function",
            "index": 0
        }
    ]
}

Modifying the output format to more specific one

During the conversation, some questions might need a more flexible response format. We have flexibility to change that during the conversation.

[21]
[22]
[23]
[24]
{
    "content": null,
    "refusal": null,
    "role": "assistant",
    "function_call": null,
    "tool_calls": [
        {
            "id": "call_Meyq8T6lEcoLTHmteAJvhRp2",
            "function": {
                "arguments": "{\"answer\": \"The President mentioned that human traffickers are being brought to justice and that the US is working with other countries to combat human trafficking.\", \"countries\": [\"United States\"]}",
                "name": "get_answer_with_countries"
            },
            "type": "function",
            "index": 0
        }
    ]
}