How to make your completions outputs consistent with the new seed parameter

OpenAI Logo
Shyamal H Anadkat
Nov 6, 2023
Open in Github

TLDR: Developers can now specify seed parameter in the Chat Completion request for consistent completions. We always include a system_fingerprint in the response that helps developers understand changes in our system that will affect determinism.

Context

Determinism has always been a big request from user communities when using our APIs. For instance, when granted the capability of getting deterministic numerical result, users can unlock quite a bit of use cases that’s sensitive to numerical changes.

Model level features for consistent outputs

The Chat Completions and Completions APIs are non-deterministic by default (which means model outputs may differ from request to request), but now offer some control towards deterministic outputs using a few model level controls.

This can unlock consistent completions which enables full control on the model behaviors for anything built on top of the APIs, and quite useful for reproducing results and testing so you know get peace of mind from knowing exactly what you’d get.

Implementing consistent outputs

To receive mostly deterministic outputs across API calls:

  • Set the seed parameter to any integer of your choice, but use the same value across requests. For example, 12345.
  • Set all other parameters (prompt, temperature, top_p, etc.) to the same values across requests.
  • In the response, check the system_fingerprint field. The system fingerprint is an identifier for the current combination of model weights, infrastructure, and other configuration options used by OpenAI servers to generate the completion. It changes whenever you change request parameters, or OpenAI updates numerical configuration of the infrastructure serving our models (which may happen a few times a year).

If the seed, request parameters, and system_fingerprint all match across your requests, then model outputs will mostly be identical. There is a small chance that responses differ even when request parameters and system_fingerprint match, due to the inherent non-determinism of computers.

Model level controls for consistent outputs - seed and system_fingerprint

seed

If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.

system_fingerprint

This fingerprint represents the backend configuration that the model runs with. It can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism.This is the indicator on whether users should expect "almost always the same result".

Example: Generating a consistent short story with a fixed seed

In this example, we will demonstrate how to generate a consistent short story using a fixed seed. This can be particularly useful in scenarios where you need to reproduce the same results for testing, debugging, or for applications that require consistent outputs.

import asyncio
import openai
import pprint
import difflib
from IPython.display import display, HTML

GPT_MODEL = "gpt-3.5-turbo-1106"
async def get_chat_response(system_message: str, user_request: str, seed: int = None):
    try:
        messages = [
            {"role": "system", "content": system_message},
            {"role": "user", "content": user_request},
        ]

        response = openai.ChatCompletion.create(
            model=GPT_MODEL,
            messages=messages,
            seed=seed,
            max_tokens=200,
            temperature=0.7,
        )

        response_content = response["choices"][0]["message"]["content"]
        system_fingerprint = response["system_fingerprint"]
        prompt_tokens = response["usage"]["prompt_tokens"]
        completion_tokens = (
            response["usage"]["total_tokens"] - response["usage"]["prompt_tokens"]
        )

        table = f"""
        <table>
        <tr><th>Response</th><td>{response_content}</td></tr>
        <tr><th>System Fingerprint</th><td>{system_fingerprint}</td></tr>
        <tr><th>Number of prompt tokens</th><td>{prompt_tokens}</td></tr>
        <tr><th>Number of completion tokens</th><td>{completion_tokens}</td></tr>
        </table>
        """
        display(HTML(table))

        return response_content
    except Exception as e:
        print(f"An error occurred: {e}")
        return None


# This function compares two responses and displays the differences in a table.
# Deletions are highlighted in red and additions are highlighted in green.
# If no differences are found, it prints "No differences found."


def compare_responses(previous_response: str, response: str):
    d = difflib.Differ()
    diff = d.compare(previous_response.splitlines(), response.splitlines())

    diff_table = "<table>"
    diff_exists = False

    for line in diff:
        if line.startswith("- "):
            diff_table += f"<tr style='color: red;'><td>{line}</td></tr>"
            diff_exists = True
        elif line.startswith("+ "):
            diff_table += f"<tr style='color: green;'><td>{line}</td></tr>"
            diff_exists = True
        else:
            diff_table += f"<tr><td>{line}</td></tr>"

    diff_table += "</table>"

    if diff_exists:
        display(HTML(diff_table))
    else:
        print("No differences found.")

First, let's try generating a short story about "a journey to Mars" without the seed parameter. This is the default behavior:

topic = "a journey to Mars"
system_message = "You are a helpful assistant that generates short stories."
user_request = f"Generate a short story about {topic}."

previous_response = await get_chat_response(
    system_message=system_message, user_request=user_request
)

response = await get_chat_response(
    system_message=system_message, user_request=user_request
)

# The function compare_responses is then called with the two responses as arguments.
# This function will compare the two responses and display the differences in a table.
# If no differences are found, it will print "No differences found."
compare_responses(previous_response, response)
ResponseIn the year 2050, a team of courageous astronauts embarked on a groundbreaking mission to Mars. The journey was filled with uncertainty and danger, but the crew was undeterred by the challenges that lay ahead. As their spacecraft hurtled through the vast expanse of space, the astronauts marveled at the beauty of the stars and the distant planets. They passed the time by conducting experiments, training for the mission ahead, and bonding with one another. After months of travel, the red planet finally came into view. The crew prepared for the landing, their hearts pounding with a mix of excitement and nervous anticipation. As the spacecraft touched down on the Martian surface, cheers erupted in the control room back on Earth. The astronauts stepped out onto the alien terrain, taking in the breathtaking landscape of rusty red rocks and dusty plains. They set up their base camp and began their scientific research, collecting samples and conducting experiments to better understand the planet's composition and potential for sustaining life. Despite the challenges of living
System Fingerprintfp_fefa7b2153
Number of prompt tokens31
Number of completion tokens200
ResponseIn the year 2050, a team of astronauts set out on a groundbreaking mission to Mars. The journey was long and arduous, but the crew was determined to make history. As they approached the red planet, they marveled at its otherworldly beauty and the sense of awe and wonder filled their hearts. Upon landing, the astronauts began to explore the alien landscape, conducting scientific experiments and collecting samples. They were amazed by the vast canyons, towering mountains, and the eerie silence that surrounded them. Each step they took was a giant leap for humankind, and they felt a profound sense of accomplishment. As they prepared to return to Earth, the astronauts reflected on the significance of their journey. They knew that their discoveries would pave the way for future generations to explore and inhabit Mars. With their mission complete, they boarded their spacecraft and set their sights on the distant blue planet in the sky, knowing that they had left their mark on the history of space exploration.
System Fingerprintfp_fefa7b2153
Number of prompt tokens31
Number of completion tokens198
- In the year 2050, a team of courageous astronauts embarked on a groundbreaking mission to Mars. The journey was filled with uncertainty and danger, but the crew was undeterred by the challenges that lay ahead.
+ In the year 2050, a team of astronauts set out on a groundbreaking mission to Mars. The journey was long and arduous, but the crew was determined to make history. As they approached the red planet, they marveled at its otherworldly beauty and the sense of awe and wonder filled their hearts.
- As their spacecraft hurtled through the vast expanse of space, the astronauts marveled at the beauty of the stars and the distant planets. They passed the time by conducting experiments, training for the mission ahead, and bonding with one another.
+ Upon landing, the astronauts began to explore the alien landscape, conducting scientific experiments and collecting samples. They were amazed by the vast canyons, towering mountains, and the eerie silence that surrounded them. Each step they took was a giant leap for humankind, and they felt a profound sense of accomplishment.
+ As they prepared to return to Earth, the astronauts reflected on the significance of their journey. They knew that their discoveries would pave the way for future generations to explore and inhabit Mars. With their mission complete, they boarded their spacecraft and set their sights on the distant blue planet in the sky, knowing that they had left their mark on the history of space exploration.
- After months of travel, the red planet finally came into view. The crew prepared for the landing, their hearts pounding with a mix of excitement and nervous anticipation. As the spacecraft touched down on the Martian surface, cheers erupted in the control room back on Earth.
-
- The astronauts stepped out onto the alien terrain, taking in the breathtaking landscape of rusty red rocks and dusty plains. They set up their base camp and began their scientific research, collecting samples and conducting experiments to better understand the planet's composition and potential for sustaining life.
-
- Despite the challenges of living

Now, let's try to generate the short story with the same topic (a journey to Mars) with a constant seed of 123 and compare the responses and system_fingerprint.

SEED = 123
response = await get_chat_response(
    system_message=system_message, seed=SEED, user_request=user_request
)
previous_response = response
response = await get_chat_response(
    system_message=system_message, seed=SEED, user_request=user_request
)

compare_responses(previous_response, response)
ResponseIn the not-so-distant future, a team of brave astronauts embarked on a groundbreaking journey to Mars. The spacecraft, named "Odyssey," soared through the vast expanse of space, leaving Earth behind as they ventured toward the mysterious red planet. As the crew navigated through the cosmos, they encountered a series of challenges and obstacles, from intense solar flares to treacherous asteroid fields. However, their unwavering determination and spirit of camaraderie propelled them forward, overcoming each hurdle with courage and resilience. Upon reaching Mars, the astronauts were greeted by a breathtaking landscape of rust-colored deserts and towering canyons. They marveled at the alien terrain, conducting scientific experiments and collecting samples to better understand the planet's enigmatic history. Amidst their exploration, the crew faced unexpected setbacks, including a sudden dust storm that threatened their safety. Yet, they stood united, devising ingenious solutions and supporting each other through the adversity. After a successful mission on Mars, the
System Fingerprintfp_fefa7b2153
Number of prompt tokens31
Number of completion tokens200
ResponseIn the not-so-distant future, a team of brave astronauts embarked on a groundbreaking journey to Mars. The spacecraft, named "Odyssey," soared through the vast expanse of space, leaving Earth behind as they ventured toward the mysterious red planet. As the crew navigated through the cosmos, they encountered a series of challenges and obstacles, from intense solar flares to treacherous asteroid fields. However, their unwavering determination and spirit of camaraderie propelled them forward, overcoming each hurdle with courage and resilience. Upon reaching Mars, the astronauts were greeted by a breathtaking landscape of rust-colored deserts and towering canyons. They marveled at the alien terrain, conducting scientific experiments and collecting samples to better understand the planet's enigmatic history. Amidst their exploration, the crew faced unexpected setbacks, including a sudden dust storm that threatened their safety. Yet, they stood united, devising ingenious solutions and supporting each other through the adversity. After a successful mission on Mars, the
System Fingerprintfp_fefa7b2153
Number of prompt tokens31
Number of completion tokens200
No differences found.

Conclusion

We demonstrated how to use a fixed integer seed to generate consistent outputs from our model.This is particularly useful in scenarios where reproducibility is important. However, it's important to note that while the seed ensures consistency, it does not guarantee the quality of the output. For instance, in the example provided, we used the same seed to generate a short story about a journey to Mars. Despite querying the model multiple times, the output remained consistent, demonstrating the effectiveness of using this model level control for reproducibility. Another great extension of this could be to use consistent seed when benchmarking/evaluating the performance of different prompts or models, to ensure that each version is evaluated under the same conditions, making the comparisons fair and the results reliable.