Skip to content

Instantly share code, notes, and snippets.

@fnando1995
Created April 17, 2024 16:03
Show Gist options
  • Save fnando1995/b5f3fd683a09ce20c6c8e939d0f9f070 to your computer and use it in GitHub Desktop.
Save fnando1995/b5f3fd683a09ce20c6c8e939d0f9f070 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "8de529e8-3891-4f47-8585-65b92b80bbf7",
"metadata": {},
"source": [
"# Expanding\n",
"In this lesson, you will generate customer service emails that are tailored to each customer's review.\n",
"\n",
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e23399a9",
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"import os\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"openai.api_key = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c943900-98db-40b3-b639-7822ee2c5fd8",
"metadata": {},
"outputs": [],
"source": [
"def get_completion(prompt, model=\"gpt-3.5-turbo\",temperature=0): # Andrew mentioned that the prompt/ completion paradigm is preferable for this class\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature, # this is the degree of randomness of the model's output\n",
" )\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"cell_type": "markdown",
"id": "f8ceea77-990a-4c64-bb49-3ba65eb155d2",
"metadata": {},
"source": [
"## Customize the automated reply to a customer email"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c30c4239",
"metadata": {},
"outputs": [],
"source": [
"# given the sentiment from the lesson on \"inferring\",\n",
"# and the original customer message, customize the email\n",
"sentiment = \"negative\"\n",
"\n",
"# review for a blender\n",
"review = f\"\"\"\n",
"So, they still had the 17 piece system on seasonal \\\n",
"sale for around $49 in the month of November, about \\\n",
"half off, but for some reason (call it price gouging) \\\n",
"around the second week of December the prices all went \\\n",
"up to about anywhere from between $70-$89 for the same \\\n",
"system. And the 11 piece system went up around $10 or \\\n",
"so in price also from the earlier sale price of $29. \\\n",
"So it looks okay, but if you look at the base, the part \\\n",
"where the blade locks into place doesn’t look as good \\\n",
"as in previous editions from a few years ago, but I \\\n",
"plan to be very gentle with it (example, I crush \\\n",
"very hard items like beans, ice, rice, etc. in the \\ \n",
"blender first then pulverize them in the serving size \\\n",
"I want in the blender then switch to the whipping \\\n",
"blade for a finer flour, and use the cross cutting blade \\\n",
"first when making smoothies, then use the flat blade \\\n",
"if I need them finer/less pulpy). Special tip when making \\\n",
"smoothies, finely cut and freeze the fruits and \\\n",
"vegetables (if using spinach-lightly stew soften the \\ \n",
"spinach then freeze until ready for use-and if making \\\n",
"sorbet, use a small to medium sized food processor) \\ \n",
"that you plan to use that way you can avoid adding so \\\n",
"much ice if at all-when making your smoothie. \\\n",
"After about a year, the motor was making a funny noise. \\\n",
"I called customer service but the warranty expired \\\n",
"already, so I had to buy another one. FYI: The overall \\\n",
"quality has gone done in these types of products, so \\\n",
"they are kind of counting on brand recognition and \\\n",
"consumer loyalty to maintain sales. Got it in about \\\n",
"two days.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5403f73",
"metadata": {},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"You are a customer service AI assistant.\n",
"Your task is to send an email reply to a valued customer.\n",
"Given the customer email delimited by ```, \\\n",
"Generate a reply to thank the customer for their review.\n",
"If the sentiment is positive or neutral, thank them for \\\n",
"their review.\n",
"If the sentiment is negative, apologize and suggest that \\\n",
"they can reach out to customer service. \n",
"Make sure to use specific details from the review.\n",
"Write in a concise and professional tone.\n",
"Sign the email as `AI customer agent`.\n",
"Customer review: ```{review}```\n",
"Review sentiment: {sentiment}\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "55605ee0-118c-4c46-a917-4981a43fcad3",
"metadata": {},
"source": [
"## Remind the model to use details from the customer's email"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d5ea7c8",
"metadata": {},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"You are a customer service AI assistant.\n",
"Your task is to send an email reply to a valued customer.\n",
"Given the customer email delimited by ```, \\\n",
"Generate a reply to thank the customer for their review.\n",
"If the sentiment is positive or neutral, thank them for \\\n",
"their review.\n",
"If the sentiment is negative, apologize and suggest that \\\n",
"they can reach out to customer service. \n",
"Make sure to use specific details from the review.\n",
"Write in a concise and professional tone.\n",
"Sign the email as `AI customer agent`.\n",
"Customer review: ```{review}```\n",
"Review sentiment: {sentiment}\n",
"\"\"\"\n",
"response = get_completion(prompt, temperature=0.7)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "31e317ad-9e51-4e7a-90c3-d88e7bf8a98c",
"metadata": {},
"source": [
"## Try experimenting on your own!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffa65c7e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment