quick_start/v1/02_ChatCompletion_api.ipynb (252 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OpenAI Chat Completion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Overview \n",
"OpenAI Chat Completion allows you to interact with language models in a conversational manner. \\\n",
"This feature enables you to create chatbots, virtual assistants, and other conversational AI applications by providing a series of messages as input and receiving a model-generated message as output.\n",
"\n",
"For more detailed examples, refer to the official [Azure OpenAI Chat Completion Documentation.](https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Import helper libraries and instantiate credentials and model"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import requests\n",
"import sys\n",
"import os\n",
"from openai import AzureOpenAI\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"client = AzureOpenAI(\n",
" azure_endpoint = os.getenv(\"AZURE_OPENAI_ENDPOINT\"), \n",
" api_key=os.getenv(\"AZURE_OPENAI_KEY\"), \n",
" api_version=\"2024-02-15-preview\"\n",
")\n",
"\n",
"CHAT_COMPLETIONS_MODEL = os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME') # model = \"deployment_name\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using Chat Completion\n",
"To use the chat completion feature, you need to provide a sequence of messages as input. \\\n",
"Each message should have a role (either \"system\", \"user\", or \"assistant\") and content.\n",
"Here is a simple example to demonstrate how to use chat completion:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Gianmarco Tamberi of Italy and Mutaz Essa Barshim of Qatar both won gold in the men's high jump at the 2020 Summer Olympics, after they decided to share the victory.\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"Answer the question as truthfully as possible, and if you're unsure of the answer, say \"Sorry, I don't know\".\n",
"\n",
"Q: Who won the 2020 Summer Olympics men's high jump?\n",
"A:\"\"\"\n",
"message_text = [{\"role\":\"system\",\"content\":prompt}]\n",
"\n",
"response = client.chat.completions.create(\n",
" model = CHAT_COMPLETIONS_MODEL,\n",
" messages = message_text,\n",
")\n",
"\n",
"response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use cases to explore\n",
"1. **Customer Support**\\\n",
"Create a virtual assistant to handle your queries.\n",
"\n",
"2. **Educational Tools** \\\n",
"Build an interactive tutor for various subjects like onboarding in your company.\n",
"\n",
"3. **Personal Assistant** \\\n",
"Develop a personal assistant for scheduling, reminders, and other tasks and integrate it later on with modern work solutions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analyzing Customer Feedback\n",
"This example demonstrates how to use the OpenAI Chat Completion feature to analyze customer feedback. \\\n",
"The task is to determine whether the provided feedback is positive or negative."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The first customer feedback is negative, and the second customer feedback is positive.'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"Decide whether the following customer feedback is positive or negative.\n",
"\n",
"Q: I was disappointed with the quality of the product. It was very cheaply made and did not meet my expectations at all.\n",
"Q: I was happy with this product, it is well made and great quality for the price.\n",
"\"\"\"\n",
"\n",
"message_text = [{\"role\":\"system\",\"content\":prompt}]\n",
"\n",
"response = client.chat.completions.create(\n",
" model = CHAT_COMPLETIONS_MODEL,\n",
" messages = message_text,\n",
")\n",
"\n",
"response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use cases to explore\n",
"\n",
"1. **Sentiment Analysis** \\\n",
"Use this approach to analyze customer feedback and categorize it into positive or negative sentiment.\n",
"\n",
"2. **Product Review Monitoring** \\\n",
"Monitor your product reviews to quickly identify and address negative feedback.\n",
"\n",
"3. **Customer Support** \\\n",
"Enhance customer support by automating the analysis of feedback and prioritizing responses to negative reviews."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extracting PII (Personally Identifiable Information) Data\n",
"\n",
"**Regulatory Compliance:** \\\n",
"With the rise of data privacy regulations such as GDPR, CCPA, and HIPAA, identifying and protecting PII has become critical. \\\n",
"Organizations must ensure that PII is handled appropriately to avoid legal repercussions and maintain customer trust.\n",
"\n",
"**Risk Management:** \\\n",
" Exposing PII can lead to identity theft, financial loss, and reputational damage. \\\n",
" Effective PII detection and protection are essential components of an organization's risk management strategy.\n",
"\n",
"This example demonstrates how to use the OpenAI Chat Completion feature to extract Personally Identifiable Information (PII) from a given text. \\\n",
"The task is to identify and list all PII data present in the provided statement.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"- Name: John Doe\\n- Age: 35 years old\\n- Address: 21 Main Street, New York, NY\\n- Employer: Microsoft\\n- Spouse's Name: Jane Doe\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"List all PII data from following statement:\n",
"John Doe is a 35-year old man and he lives at 21 Main Street, New York, NY. He is a software engineer and he works at Microsft. He has a wife named Jane Doe and they have two children\n",
"\"\"\"\n",
"\n",
"message_text = [{\"role\":\"system\",\"content\":prompt}]\n",
"\n",
"response = client.chat.completions.create(\n",
" model = CHAT_COMPLETIONS_MODEL,\n",
" messages = message_text,\n",
")\n",
"\n",
"response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use cases to explore\n",
"1. **Data Privacy** \\\n",
"Ensure compliance with data privacy regulations by identifying and protecting PII in text data.\n",
"\n",
"2. **Information Security** \\\n",
"Enhance information security by automatically detecting and handling PII in documents and communications.\n",
"\n",
"3. **Customer Data Management** \\\n",
"Manage customer data effectively by identifying and categorizing PII for secure storage and processing."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}