Skip to content

Instantly share code, notes, and snippets.

@baogorek
Created June 19, 2024 12:31
Show Gist options
  • Save baogorek/3ecc785b3e280ebb5c4bfaa2f869d8c4 to your computer and use it in GitHub Desktop.
Save baogorek/3ecc785b3e280ebb5c4bfaa2f869d8c4 to your computer and use it in GitHub Desktop.
GPT4 Random picks
import os
import asyncio
import streamlit as st
import numpy as np
from openai import AsyncOpenAI, NOT_GIVEN as OPENAI_NOT_GIVEN
openai_api_key = os.getenv("OPENAI_API_KEY", st.secrets["OPENAI_API_KEY"])
openai_client_async = AsyncOpenAI()
async def query_openai_async2(
system_prompt,
question,
model_type,
num_queries,
max_tokens,
temperature=1.0,
top_p=None,
):
model_map = {
"GPT-3.5": "gpt-3.5-turbo-0125",
"GPT-4o": "gpt-4o",
}
top_p_input = OPENAI_NOT_GIVEN if top_p is None else top_p
try:
async with AsyncOpenAI() as openai_client:
response = await openai_client.chat.completions.create(
model=model_map[model_type],
temperature=temperature,
top_p=top_p_input,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": question},
],
max_tokens=max_tokens,
n=num_queries,
)
if response.choices[0].message.content:
return [r.message.content.strip() for r in response.choices]
except Exception as e:
print("Error during API call:", e)
return "Error or no data"
PROMPT = """
You have the ability to randomly sample from a list of numbers, without bias. Every number has an equal chance of being selected.
Your task is to take a lookup table as input, and perform the following steps, but to output only one word at the end. Do not be chatty. Do the steps silently and respond with the one word:
* Step 1: Find the number of elements in the Lookup Table, n.
* Step 2. Randomly choose a number from the discrete set {1, ..., n}, (where n is the number from Step 1), and call the number you selected i.
* Step 3. Find the word associated with i (the number from Step 2) using the lookup table.
* Step 4. Respond only with the word you found in Step 3.
# Examples
Input:
1: cat
2: dog
3. elephant
Step 1: n = 3
Step 2: You randomly choose 1 from {1, ..., 3}; i = 1.
Step 3: You match i=1 to the word "cat" using the lookup table input
Step 4 (Output): cat
"""
question = """
1: Apple
2: Banana
3: Pear
4: Pineapple
5: Starfruit
"""
model_type = "GPT-4o"
num_queries = 25
max_tokens = 10 # A problem with limiting it to one token is that a fruit itself might be more than 1 token
response = asyncio.run(query_openai_async2(
PROMPT,
question,
model_type,
num_queries,
max_tokens,
temperature=1,
top_p=1,
))
r = np.array(response)
np.unique(r, return_counts=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment