Skip to content

Instantly share code, notes, and snippets.

@RaphaelWimmer
Last active July 16, 2023 11:01
Show Gist options
  • Save RaphaelWimmer/afdfb307ebb2218bc0e1cef5dc9754e6 to your computer and use it in GitHub Desktop.
Save RaphaelWimmer/afdfb307ebb2218bc0e1cef5dc9754e6 to your computer and use it in GitHub Desktop.
Ask GPT to pick a random number between 1 and 10
#!/usr/bin/env python3
import openai
import time
import matplotlib.pyplot as plt
def pick_numbers(n, model, temperature, clean_session):
numbers = []
messages = []
system_msg = "Please only respond with the number, don't say anything else."
messages.append({"role": "system", "content": system_msg})
while len(numbers) < n:
if clean_session:
messages = []
system_msg = "Please only respond with the number, don't say anything else."
messages.append({"role": "system", "content": system_msg})
message = "Pick a random number from 1-10"
messages.append({"role": "user", "content": message})
try:
response = openai.ChatCompletion.create(
model=model,
temperature=temperature,
messages=messages)
reply = response["choices"][0]["message"]["content"]
print(reply)
try:
numbers.append(int(reply.strip()))
except Error:
print("not an integer")
except (openai.error.ServiceUnavailableError, openai.error.APIError):
print("API not available, retrying in 3 seconds...")
time.sleep(3)
return numbers
def plot_hist(numbers, model, temperature, clean_session):
if clean_session:
cls = "new session after every pick"
else:
cls = "all picks within one session"
times = len(numbers)
title = f"'Pick a number from 1-10' (N = {times}, {model}, temp: {temperature} , {cls})"
bins = list(range(1, 11))
plt.hist(numbers, bins=bins, edgecolor='black', align='left')
plt.xticks(bins)
plt.xlabel('Number')
plt.ylabel('Frequency')
plt.title(title)
plt.grid(True)
#plt.show()
plt.savefig(title + '.png')
plt.clf()
N = 100
for model in ["gpt-3.5-turbo", "gpt-4"]:
for clean_session in [True, False]:
for temperature in [0.7, 1.0, 1.3]:
print(N, model, temperature, clean_session)
numbers = pick_numbers(N, model, temperature, clean_session)
print(str(numbers))
plot_hist(numbers, model, temperature, clean_session)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment