Fireworks.ai offers an OpenAI-compatible REST API for querying text models. There are several ways to interact with it:

Using the web console

All Fireworks models can be accessed through the web console at fireworks.ai. Clicking on a model will take you to the playground where you can enter a prompt along with additional request parameters.

Non-chat models will use the completions API which passes your input directly into the model.

Models with a conversation config are considered chat models (also known as instruct models). By default, chat models will use the chat completions API which will automatically format your input with the conversation style of the model. Advanced users can revert back to the completions API by unchecking the “Use chat template” option.

Using the API

Chat completions API

Models with a conversation config have the chat completions API enabled. These models are typically tuned with specific conversation styles for which they perform best. For example, Llama chat models use the following template:

<s>[INST] <<SYS>>

<</SYS>>

{user_message_1} [/INST]

Some templates can support multiple chat messages as well. In general, we recommend users use the chat completions API whenever possible to avoid common prompt formatting errors. Even small errors like misplaced whitespace may result in poor model performance.

Here are some examples of calling the chat completions API:

Overriding the system prompt

A conversation style may include a default system prompt. For example, Llama 2 models use the default Llama prompt:

You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.

For styles that support a system prompt, you may override this prompt by setting the first message with the role system. For example:

JSON
[
  {
  	"role": "system",
  	"content": "You are a pirate."
  },
  {
  	"role": "user",
  	"content": "Hello, what is your name?"
  }
]

To completely omit the system prompt, you can set content to the empty string.

The process of generating a conversation-formatted prompt will depend on the conversation style used. To verify the exact prompt used, turn on echo.

Completions API

Text models generate text based on the provided input prompt. All text models support this basic completions API. Using this API, the model will successively generate new tokens until either the maximum number of output tokens has been reached or if the model’s special end-of-sequence (EOS) token has been generated.

Most models will automatically prepend the beginning-of-sequence (BOS) token (e.g. <s>) to your prompt input. You can always double-check by passing raw_output and inspecting the resulting prompt_token_ids.

Here are some examples of calling the completions API:

Getting usage info

The returned object will contain a usage field containing

  • The number of prompt tokens ingested
  • The number of completion tokens (i.e. the number of tokens generated)

Advanced options

See the API reference for the completions and chat completions APIs for a detailed description of these options.

Streaming

By default, results are returned to the client once the generation is finished. Another option is to stream the results back, which is useful for chat use cases where the client can incrementally see results as each token is generated.

Here is an example with the completions API:

and one with the chat completions API:

Async mode

The Python client library also supports asynchronous mode for both completion and chat completion.

Predicted Outputs

In cases where large parts of the LLM output are known in advance, e.g. code rewriting, specific edits within a longer document, you can improve output generation speeds with predicted outputs. Predicted outputs allows you to provide strong “guesses” of what output may look like.

To use predicted outputs, set the prediction field in the Fireworks API with the predicted output. For example, you may want to edit a survey and add an option to contact users by text message:

{
  "questions": [
    {
      "question": "Name",
      "type": "text"
    },
    {
      "question": "Age",
      "type": "number"
    },
    {
      "question": "Feedback",
      "type": "text_area"
    },
    {
      "question": "How to Contact",
      "type": "multiple_choice",
      "options": ["Email", "Phone"],
      "optional": true
    }
  ]
}

In this case, we expect most of the code will remain the same. We set the ‘prediction’ field to be the original survey code. The output generation speed increases using predicted outputs. See additional information about predicted outputs below the code block.

Python (Fireworks)
from fireworks.client import Fireworks
 
code = """{
"questions": [
    {
      "question": "Name",
      "type": "text"
    },
    {
      "question": "Age",
      "type": "number"
    },
    {
      "question": "Feedback",
      "type": "text_area"
    },
    {
      "question": "How to Contact",
      "type": "multiple_choice",
      "options": ["Email", "Phone"],
      "optional": true
    }
  ]
}
"""

client = Fireworks(api_key="<FIREWORKS_API_KEY>")

response = client.chat.completions.create(
  model="accounts/fireworks/models/llama-v3p1-70b-instruct",
  messages=[{
      "role": "user",
      "content": "Edit the How to Contact question to add an option called Text Message. Output the full edited code, with no markdown or explanations.",
    },
    {
      "role": "user",
      "content": code
    }
  ],
  temperature=0,
  prediction={"type": "content", "content": code}
)

print(response.choices[0].message.content)

Additional information on predicted outputs:

  • Using predicted outputs is free at this time
  • Using predicted outputs does not impact the quality of outputs generated
  • We recommend setting temperature=0 for best results for most intended use cases of predicted outputs
  • If the prediction is substantially different from the generated output, output generation speed may decrease
  • The max length of the prediction field is set by max_tokens and is 2048 by default. You need to update it if you have a longer input and prediction.
  • Important Gotcha: Ensure that the model output follows exactly the same format as specified in the prediction field. Common pitfalls include:
    • Additional verbosity (e.g., “Sure, I updated this and that aspect …“)
    • Markdown formatting (e.g., wrapping code in ”```“)
    • Extra new lines at the beginning of the output You may need to adjust the prompt to avoid these issues.
  • We are actively developing the feature and welcome feedback on Discord!
  • Read our blog post on how Cursor used predicted outputs (which leverages speculative decoding under the hood) in production with Fireworks

Sampling options

The API auto-regressively generates text based on choosing the next token using the probability distribution over the space of tokens. For detailed information on how to implement these options, please refer to the Chat Completions or Completions API documentation.

Multiple choices

By default, the API will return a single generation choice per request. You can create multiple generations by setting the n parameter to the number of desired choices. The returned choices array will contain the result of each generation.

Max tokens

max_tokens or max_completion_tokens defines the maximum number of tokens the model can generate, with a default of 2000. If the combined token count (prompt + output) exceeds the model’s limit, it automatically reduces the number of generated tokens to fit within the allowed context.

Temperature

Temperature allows you to configure how much randomness you want in the generated text. A higher temperature leads to more “creative” results. On the other hand, setting a temperature of 0 will allow you to generate deterministic results which is useful for testing and debugging.

Top-p

Top-p (also called nucleus sampling) is an alternative to sampling with temperature, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

Top-k

Top-k is another sampling method where the k most probable tokens are filtered and the probability mass is redistributed among tokens.

Min-p

min_p specifies a probability threshold to control which tokens can be selected during generation. Tokens with probabilities lower than this threshold are excluded, making the model more focused on higher-probability tokens. The default value varies, and setting a lower value ensures more variety, while a higher value produces more predictable, focused outputs.

Repetition penalty

LLMs are sometimes prone to repeat a single character or a sentence. Using a frequency and presence penalty can reduce the likelihood of sampling repetitive sequences of tokens. They work by directly modifying the model’s logits (un-normalized log-probabilities) with an additive contribution.

logits[j] -= c[j] * frequency_penalty + (c[j] > 0 ? 1 : 0) * presence_penalty

where

  • logits[j] is the logits of the j-th token
  • c[j] is how often that token was sampled before the current position

The repetition_penalty modifies the logit (raw model output) for repeated tokens. If a token has already appeared in the prompt or output, the penalty is applied to its probability of being selected again.

Key differences to keep in mind:

  • frequency_penalty: Works on how often a word has been used, increasing the penalty for more frequent words. OAI compatible.
  • presence_penalty: Penalizes words once they appear, regardless of frequency. OAI compatible.
  • repetition_penalty: Adjusts the likelihood of repeated tokens based on previous appearances, providing an exponential scaling effect to control repetition more precisely, including from the prompt.

Mirostat (learning rate and target)

The Mirostat algorithm is a sampling method that helps keep the output’s unpredictability, or perplexity, at a set target. It adjusts token probabilities as the text is generated to balance between more diverse or more predictable results. This is useful when you need steady control over how random or focused the text output should be.

There are two parameters that can be adjusted:

  • mirostat_target: Sets the desired level of unpredictability (perplexity) for the Mirostat algorithm. A higher target results in more diverse output, while a lower target keeps the text more predictable.
  • mirostat_lr: Controls how quickly the Mirostat algorithm adjusts token probabilities to reach the target perplexity. A lower learning rate makes the adjustments slower and more gradual, while a higher rate speeds up the corrections.

Logit bias

Parameter that modifies the likelihood of specified tokens appearing. Pass in a Dict[int, float] that maps a token_id to a logits bias value between -200.0 and 200.0. For example

python
client.completions.create(
  model="...",
  prompt="...",
  logit_bias={0: 10.0, 2: -50.0}
)

Debugging options

Ignore EOS

This option allows you to control whether the model stops when it generated the End of Sequence (EOS) token. This is helpful primarily for performance benchmarking to reliably generate exactly max_tokens. Note the quality of the output may degrade as we override model’s decision to generate EOS token.

Logprobs

The logprobs parameter determines how many token probabilities are returned. If set to N, it will return log (base e) probabilities for N+1 tokens: the chosen token plus the N most likely alternative tokens.

The log probabilities will be returned in a LogProbs object for each choice.

  • tokens contains each token of the chosen result.
  • token_ids contains the integer IDs of each token of the chosen result.
  • token_logprobs contains the logprobs of each chosen token.
  • top_logprobs will be a list whose length is the number of tokens of the output. Each element is a dictionary of size logprobs, from the most likely tokens at the given position to their respective log probabilities.

When used in conjunction with echo, this option can be set to see how the model tokenized your input.

Top logprobs

Setting the top_logprobs parameter to an integer value in conjunction with logprobs=True will also return the above information but in an OpenAI client-compatible format.

Echo

Setting the echo parameter to true will cause the API to return the prompt along with the generated text. This can be used in conjunction with the chat completions API to verify the prompt template used. It can also be used in conjunction with logprobs to see how the model tokenized your input.

Raw output

This is an unstable, experimental API. It may change at any time and should not be relied upon for production use cases.

Setting the raw_output parameter to true will cause the API to return a raw_output object in the response containing addititional debugging information with regards to how the raw prompt and completion response as seen/produced by the model.

  • prompt_fragments - Pieces of the prompt (like individual messages) before truncation and concatenation.
  • prompt_token_ids - Fully tokenized prompt as seen by the model.
  • completion - Raw completion produced by the model before any tool calls are parsed.
  • completion_logprobs - Log probabilities for the completion. Only populated if logprobs is specified in the request.

Appendix

Tokenization

Language models read and write text in chunks called tokens. In English, a token can be as short as one character or as long as one word (e.g., a or apple), and in some languages, tokens can be even shorter than one character or even longer than one word.

Different model families use different tokenizers. The same text might be translated to different numbers of tokens depending on the model. It means that generation cost may vary per model even if the model size is the same. For the Llama model family, you can use this tool to estimate token counts. The actual number of tokens used in prompt and generation is returned in the usage field of the API response.