Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions research/code/Synthesize.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
],
"source": [
"!pip install openai==0.28"
"!pip install \"openai>=1.0.0\"\n"
]
},
{
Expand All @@ -53,13 +53,13 @@
"text": [
"Collecting openai\n",
" Downloading openai-1.30.5-py3-none-any.whl (320 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m320.7/320.7 kB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m320.7/320.7 kB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (2.0.3)\n",
"Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)\n",
"Collecting httpx<1,>=0.23.0 (from openai)\n",
" Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai) (2.7.1)\n",
"Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.1)\n",
"Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.4)\n",
Expand All @@ -73,10 +73,10 @@
"Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2024.2.2)\n",
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)\n",
" Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai)\n",
" Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
"Requirement already satisfied: pydantic-core==2.18.2 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (2.18.2)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
Expand All @@ -86,7 +86,7 @@
}
],
"source": [
"!pip install openai pandas"
"!pip install \"openai>=1.0.0\" pandas\n"
]
},
{
Expand All @@ -97,13 +97,15 @@
},
"outputs": [],
"source": [
"import openai\n",
"import os\n",
"from openai import OpenAI\n",
"import pandas as pd\n",
"import time\n",
"import random\n",
"\n",
"# Set up the OpenAI API key\n",
"openai.api_key = null"
"# Initialize the OpenAI client\n",
"# Make sure OPENAI_API_KEY is set in your environment variables\n",
"client = OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\", \"\"))\n"
]
},
{
Expand Down Expand Up @@ -164,42 +166,44 @@
" for feature in features:\n",
" for _ in range(num_reviews_per_combination):\n",
" prompt = random.choice(prompts).format(sentiment=sentiment, aspect=aspect)\n",
" response = openai.Completion.create(\n",
" # model=\"gpt-4\", # Use GPT-4\n",
" engine=\"davinci\", # Use GPT-3 (davinci)\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ],\n",
" max_tokens=100,\n",
" n=1,\n",
" stop=None,\n",
" temperature=0.7\n",
" )\n",
" review = response.choices[0].message[\"content\"].strip()\n",
" reviews.append((review, sentiment, aspect, feature))\n",
" time.sleep(1) # To avoid rate limits, adjust based on your API rate limits\n",
" try:\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\", # Updated to modern chat model\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ],\n",
" max_tokens=100,\n",
" n=1,\n",
" temperature=0.7\n",
" )\n",
" review = response.choices[0].message.content.strip()\n",
" reviews.append((review, sentiment, aspect, feature))\n",
" except Exception as e:\n",
" print(f\"Error generating review: {e}\")\n",
" time.sleep(1) # To avoid rate limits\n",
" return reviews\n",
"\n",
"# Generate a large dataset\n",
"total_reviews = 10000 # Target number of reviews\n",
"# Generate a small dataset for testing\n",
"total_reviews = 10 # Reduced for quick testing\n",
"reviews = []\n",
"\n",
"# Calculate the number of iterations needed\n",
"iterations = total_reviews // (len(aspects) * len(sentiments) * len(features) * num_reviews_per_combination)\n",
"iterations = max(1, total_reviews // (len(aspects) * len(sentiments) * len(features) * num_reviews_per_combination))\n",
"for _ in range(iterations):\n",
" batch_reviews = generate_reviews(prompts, aspects, sentiments, features, num_reviews_per_combination)\n",
" reviews.extend(batch_reviews)\n",
" break # Only run one iteration for testing\n",
"\n",
"# Create a DataFrame to store the synthetic reviews\n",
"df = pd.DataFrame(reviews, columns=['review', 'sentiment', 'aspect', 'feature'])\n",
"df = pd.DataFrame(reviews, columns=[\"review\", \"sentiment\", \"aspect\", \"feature\"])\n",
"\n",
"# Display the first few rows\n",
"print(df.head())\n",
"print(f\"Total reviews generated: {df.shape[0]}\")\n",
"\n",
"# Save to CSV\n",
"df.to_csv('synthetic_usability_reviews.csv', index=False)"
"df.to_csv(\"synthetic_usability_reviews.csv\", index=False)\n"
]
},
{
Expand All @@ -226,4 +230,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}