Skip to content

Instantly share code, notes, and snippets.

@luisgdelafuente
Last active January 25, 2024 11:03
Show Gist options
  • Save luisgdelafuente/4e3b0cd72a907047807a872af4e7843c to your computer and use it in GitHub Desktop.
Save luisgdelafuente/4e3b0cd72a907047807a872af4e7843c to your computer and use it in GitHub Desktop.
creating hallucinations with different models
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: gpt-4\n",
"Response: The expression given is 2 * ( 5 * 2 ), replacing '*' with the exponential\n",
"operator would make it 2 ^ ( 5 ^ 2 ). So let's simplify: First, calculate the\n",
"exponent in the parentheses: 5 ^ 2 = 25. Then use this result in the main\n",
"equation: 2 ^ 25 = 33554432. So, the numeric value of the expression 2 ^ ( 5 ^ 2\n",
") is 33554432.\n",
"\n",
"Model: gpt-3.5-turbo\n",
"Response: To replace \"*\" with the exponential operator, \"^\", the expression 2 * (5 * 2)\n",
"would become 2 ^ (5 ^ 2).\n",
"\n",
"Model: gpt-3.5-turbo-0301\n",
"Response: The exponential operator is '^'. So, 2 * ( 5 * 2 ) in exponential notation would\n",
"be: 2 * (5^2) which simplifies to: 2 * (25) and the final result is: 50\n",
"\n",
"Model: gpt-3.5-turbo-0613\n",
"Response: The numeric value of 2 * ( 5 * 2 ) replacing '*' by the exponential operator (^)\n",
"would be 50. The expression can be rewritten as 2 * (5^2), which simplifies to 2\n",
"* 25 = 50.\n",
"\n"
]
}
],
"source": [
"# Creating hallucinations for different models: see the huge gap from 3.5 to 4\n",
"\n",
"\n",
"import os\n",
"import openai\n",
"import textwrap\n",
"\n",
"# Get the API key\n",
"api_key = os.environ[\"OPENAI_API_KEY\"]\n",
"\n",
"# Set up the OpenAI API key\n",
"openai.api_key = api_key\n",
"\n",
"# Define the prompt\n",
"prompt = \"what is the numeric value of 2 * ( 5 * 2 ) replacing '*' by the exponential operator? \"\n",
"\n",
"# Define the list of models\n",
"models = [\"gpt-4\",\"gpt-3.5-turbo\", \"gpt-3.5-turbo-0301\", \"gpt-3.5-turbo-0613\"]\n",
"\n",
"# Maximum number of words for each response\n",
"max_words = 100\n",
"\n",
"# Generate a response for each model\n",
"for model in models:\n",
" try:\n",
" # Generate a response with the API\n",
" response = openai.ChatCompletion.create(\n",
" model=model, messages=[{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": prompt}]\n",
" )\n",
"\n",
" # Extract the response text from the API response\n",
" generated_response = response['choices'][0]['message']['content']\n",
"\n",
" # Split the response into words\n",
" words = generated_response.split()\n",
"\n",
" # Truncate the response to the maximum number of words\n",
" truncated_response = ' '.join(words[:max_words])\n",
"\n",
" # Print the response with text wrapping\n",
" wrapped_response = textwrap.fill(truncated_response, width=80) # Adjust the width as needed\n",
" print(f\"Model: {model}\\nResponse: {wrapped_response}\\n\")\n",
" except Exception as e:\n",
" print(f\"Error for model {model}: {e}\\n\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment