Skip to main content

Context

When using a pre-trained large language model, several inputs influence user experience, including the prompts used, the “inference parameters” given to the model, often including parameters like temperature, length penalties and repetition penalties, and even the exact model selected. Tools like Statsig can streamline the process of assigning users to various experiments, such as modifying these inputs, and can automatically identify when changes have a statistically significant impact on user experience metrics. This enables efficient iteration and optimization of user experience by tweaking model choices, prompts, and inference parameters. In this case we show how you can log both implicit indicators of user feedback (like response time) and more explicit ones, like self-reported satisfaction.

Introduction

The included Python code offers an example of the simple but powerful interaction between OpenAI’s GPT and Statsig to experiment with model inputs and log user events. This example uses OpenAI’s ChatCompletion feature to answer questions, plus a Statsig integration to experiment with model versions and log user feedback to the changes. This example assumes you have a funded OpenAI account, plus a Statsig experiment that varies the model selected between “gpt-3.5-turbo” and “gpt-4”. For more info on setting up a Statsig experiment, see the experiments page.

Code Breakdown

Initial Configuration

Of course, you’ll need to install both the Statsig and OpenAI Python packages before starting:
After that, we can begin coding in a python file:

The ask_question Function

The following code all occurs in one function titled ask_question (see the final code)
  1. Get User Input
First, we prompt the user with the question they’d like to ask ChatGPT
  1. Query OpenAI’s GPT
Next, we request a completion that queries the GPT model specified by the Statsig experiment (either gpt-3.5-turbo or gpt-4). We also start a timer so we can track the response time in our events later on.
  1. Display Response & Log Implicit Feedback
With the response from OpenAI we have our first set of useful information to log to Statsig - like the response time and tokens used. We log this information with Statsig’s SDK, storing them in the metadata of the request.
  1. Collect User Feedback & Log to Statsig
Next, we log a more explicit indicator of feedback, the user’s self-reported satisfaction or dissatisfaction. The satisfaction metric can provide a strong indicator of the model’s overall power. And we’re done - we can run this Python program with the following code, now outside of our ask_question function.

Tips for Using Statsig with AI

  1. Experimentation: You can also test other model parameters like temperature, top_p, or initial prompts.
  2. Log Useful Data: Consider logging other interesting user interactions or feedback.
  3. Analyze and Iterate: After collecting enough data, analyze the results on the Statsig dashboard.
  4. User Identification: Consider integrating a mechanism to uniquely identify each user/session.
Always ensure you’re in compliance with user privacy regulations and that you have user consent where necessary.

Final code