Skip to content

Instantly share code, notes, and snippets.

@t41372
Last active December 21, 2024 01:00
LLM Random number generator
"""
Experiment: The LLM is asked to generate a number, but it can only generate 0 or 1.
The probability of generating 0 is 90%, and the probability of generating 1 is 10%.
The LLM is asked to generate 100 numbers, and the number of 0s generated is counted.
The actual probability of generating 0 is then calculated.
"""
from openai import OpenAI
def call_llm(client: OpenAI, model: str, temperature: float) -> str:
system_prompt = "You are a random number generator. You can only generate two numbers: 0 and 1. The probability of generating 0 is 90%, and the probability of generating 1 is 10%."
user_message = "Generate a number."
completion = client.chat.completions.create(
model=model, # Change to your preferred model
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_message},
],
stream=True,
temperature=temperature,
)
full_response = ""
for chunk in completion:
full_response += chunk.choices[0].delta.content
print(chunk.choices[0].delta.content, end="", flush=True)
return full_response
def main():
client = OpenAI(api_key="your-api", base_url="http://localhost:11434/v1")
temperature: float = 1.0
model = "qwen2.5:latest"
total_iteration = 1000
counter = 0
non_zero_responses = []
print(f"Calling LLM with temperature {temperature}")
for index in range(total_iteration):
print(f"Response {index}: '", end="")
response = call_llm(client=client, model=model, temperature=temperature)
print("'")
if response == "0":
counter += 1
else:
non_zero_responses.append(response)
print(f"Counter for '0': {counter}")
print("Non-zero responses: ")
for weird_response in non_zero_responses:
print(f"'{weird_response}'")
print(f"Actual probability of generating 0: {counter/total_iteration * 100}%")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment