mirror of
https://github.com/james-m-jordan/openai-cookbook.git
synced 2025-05-09 19:32:38 +00:00

* Updating to use api version 2023-05-15 * Removing authoring references from azure samples as it is not yet present.
208 lines
6.2 KiB
Plaintext
208 lines
6.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Azure chat completions example (preview)\n",
|
|
"In this example we'll try to go over all operations needed to get chat completions working using the Azure endpoints. \\\n",
|
|
"This example focuses on chat completions but also touches on some other operations that are also available using the API. This example is meant to be a quick way of showing simple operations and is not meant as a tutorial."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import openai"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Setup\n",
|
|
"For the following sections to work properly we first have to setup some things. Let's start with the `api_base` and `api_version`. To find your `api_base` go to https://portal.azure.com, find your resource and then under \"Resource Management\" -> \"Keys and Endpoints\" look for the \"Endpoint\" value."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"openai.api_version = '2023-05-15'\n",
|
|
"openai.api_base = '' # Please add your endpoint here"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`."
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Setup: Portal\n",
|
|
"Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under \"Resource Management\" -> \"Keys and Endpoints\" look for one of the \"Keys\" values."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"openai.api_type = 'azure'\n",
|
|
"openai.api_key = '' # Please add your api key here\n"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### (Optional) Setup: Microsoft Active Directory Authentication\n",
|
|
"Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# from azure.identity import DefaultAzureCredential\n",
|
|
"\n",
|
|
"# default_credential = DefaultAzureCredential()\n",
|
|
"# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
|
|
"\n",
|
|
"# openai.api_type = 'azure_ad'\n",
|
|
"# openai.api_key = token.token"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Deployments\n",
|
|
"In this section we are going to create a deployment using the `gpt-35-turbo` model that we can then use to create chat completions."
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Deployments: Create manually\n",
|
|
"Let's create a deployment using the `gpt-35-turbo` model. Go to https://portal.azure.com, find your resource and then under \"Resource Management\" -> \"Model deployments\" create a new `gpt-35-turbo` deployment. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"deployment_id = '' # Fill in the deployment id from the portal here"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Create chat completion\n",
|
|
"Now let's send a sample chat completion to the deployment."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# For all possible arguments see https://platform.openai.com/docs/api-reference/chat-completions/create\n",
|
|
"response = openai.ChatCompletion.create(\n",
|
|
" deployment_id=deployment_id,\n",
|
|
" messages=[\n",
|
|
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
|
|
" {\"role\": \"user\", \"content\": \"Knock knock.\"},\n",
|
|
" {\"role\": \"assistant\", \"content\": \"Who's there?\"},\n",
|
|
" {\"role\": \"user\", \"content\": \"Orange.\"},\n",
|
|
" ],\n",
|
|
" temperature=0,\n",
|
|
")\n",
|
|
"\n",
|
|
"print(f\"{response.choices[0].message.role}: {response.choices[0].message.content}\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can also stream the response.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"response = openai.ChatCompletion.create(\n",
|
|
" deployment_id=deployment_id,\n",
|
|
" messages=[\n",
|
|
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
|
|
" {\"role\": \"user\", \"content\": \"Knock knock.\"},\n",
|
|
" {\"role\": \"assistant\", \"content\": \"Who's there?\"},\n",
|
|
" {\"role\": \"user\", \"content\": \"Orange.\"},\n",
|
|
" ],\n",
|
|
" temperature=0,\n",
|
|
" stream=True\n",
|
|
")\n",
|
|
"\n",
|
|
"for chunk in response:\n",
|
|
" delta = chunk.choices[0].delta\n",
|
|
"\n",
|
|
" if \"role\" in delta.keys():\n",
|
|
" print(delta.role + \": \", end=\"\", flush=True)\n",
|
|
" if \"content\" in delta.keys():\n",
|
|
" print(delta.content, end=\"\", flush=True)"
|
|
]
|
|
}
|
|
],
|
|
"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.3"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|