quick_start/legacy/02_ChatCompletion_api.ipynb (144 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"import re\n",
"import requests\n",
"import sys\n",
"import os\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"openai.api_type = \"azure\"\n",
"openai.api_version = os.getenv(\"OPENAI_API_VERSION\")\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"openai.api_base = os.getenv(\"OPENAI_API_BASE\")\n",
"model = os.getenv(\"CHAT_COMPLETION_NAME\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Mutaz Essa Barshim of Qatar and Gianmarco Tamberi of Italy both won the gold medal in the men's high jump at the 2020 Summer Olympics.\""
]
},
"execution_count": 6,
"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",
"response = openai.ChatCompletion.create(\n",
" engine=model,\n",
" messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\",\"content\":prompt},])\n",
"\n",
"response['choices'][0]['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The first customer feedback is negative, as the customer was disappointed with the quality of the product. The second customer feedback is positive, as the customer was happy with the product and found it to be well made with great quality for the price.\n"
]
}
],
"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",
"response = openai.ChatCompletion.create(\n",
" engine=model,\n",
" messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\",\"content\":prompt},])\n",
"\n",
"print(response['choices'][0]['message']['content'])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The personally identifiable information (PII) in the statement is:\n",
"\n",
"- John Doe\n",
"- 35 years old\n",
"- 21 Main Street, New York, NY\n",
"- Software engineer\n",
"- Microsft\n",
"- Jane Doe\n"
]
}
],
"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",
"response = openai.ChatCompletion.create(\n",
" engine=model,\n",
" messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\",\"content\":prompt},])\n",
"\n",
"print(response['choices'][0]['message']['content'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}