GenAINotes240208
GenAINotes240208
GenAINotes240208
Requirements
• Deploy an Azure OpenAI resource and an Azure OpenAI model
• Generate natural language responses by using Azure OpenAI
• Apply prompt engineering techniques by using Azure OpenAI
• Generate and improve code by using Azure OpenAI
• Generate images with DALL-E in Azure OpenAI
• Use Azure OpenAI on your data
• Be familiar with Azure services.
• Have experience developing applications by using C# or Python
Get Started
• The models we will be using in this course are:
o GPT-3.5-Turbo (GPT = Generative Pre-trained Transformer).
▪ This provides advanced language AI with Azure security.
• It uses a Large Language Model (LLM).
▪ They use conversation-in and message-out, as opposed to earlier
models, which were text-in and text-out.
▪ It can understand and generate natural language and code.
▪ The GPT-3.5-Turbo model is the most capable and cost-effective
model in the GPT-3.5 family. Microsoft recommends using this
instead of legacy GPT-3.5 and GPT-3 models.
▪ At the time of recording, this is about 10 times cheaper than GPT-4
(which can solve difficult problems with improved accuracy).
o DALL-E
▪ This creates original image using natural language.
▪ At the time of requirement, you need to use a
• US East location to use DALL-E-2, or
• Sweden Central location to use DALL-E-3.
• They are based on:
o Prompt (input), and
o Completion (output).
• You can use both the Completions API and Chat Completion API.
o The Completions API is there for legacy reasons – it is compatible with
GPT-3.
o The Chat Completion API is the preferred version, and is compatible with
GPT-4.
• The text models are priced in tokens, which are roughly syllables. On average,
they are 4 characters long.
o Pricing information: https://azure.microsoft.com/en-
gb/pricing/details/cognitive-services/openai-service/
o The pricing for GPT-3.5-Turbo is 0.15-0.2 US cents for 1,000 tokens.
o The pricing for GPT-4 is 3-12 US cents for 1,000 tokens.
• The image models are priced by image:
▪ The scope and limitations. Say what should happen if the prompt
is off-target.
• Do not perform actions that are not related to [task or topic
name].
▪ The posture and tone.
▪ The language and syntax of the output format
• You use the [insert desired syntax] in your output
• You will bold important words in your response.
• You can apply system messages for non-chat environments.
o Analyse the sentiment from the speech, on a scale of 1 to 5, 5 being the
highest. Explain why there is that rating.
▪ Sentiment is a computer term, saying whether something is
positive, negative, or neutral.
The GPT chat system is one of the best things I have ever seen.
January has been very long. I wish that it was over. Thankfully, spring is
around the corner.
• Other suggestions are in the “Using templates” section:
o Shakespeare writing assistance – How can I ask what the weather is like?
o IRS tax chatbot – How much can I put into an IRA?
o Marketing Writing Assistant - I want to have some marketing material for
my new course "Develop generative AI solutions with Azure OpenAI
Service"
o Xbox customer support agent – How do I reboot my XBox?
o Hiking recommendations chatbot – I am in Washington DC. I want to hike
within 50 miles.
o JSON formatter assistant –
Please convert this list into JSON.
---
Fruit: Apples, bananas, mango
Vegetables: Carrots, potato, broccoli
• Assistant message
o This is a combination of user prompts and assistant responses.
o This can help describe future answers.
o Describe a hamburger. Food.
o Describe a Gin and Tonic. Alcohol.
o Describe a printer. Electric device.
• Add clear syntax
o You can use a --- or “”” separator in between different sources of
information or steps.
o You can also use Markdown or XML language.
• Break down the task
Read this article.
---
(Article)
---
Extract the facts from the article, and put them into a bulleted list.
• You can incorporate previous responses into your next prompt.
Create a list of 10 colors and 10 objects, and then combine them into 40
different combinations.
---
CREATE TABLE [Person].[Person](
[BusinessEntityID] [int] NOT NULL,
[PersonType] [nchar](2) NOT NULL,
[NameStyle] [dbo].[NameStyle] NOT NULL,
[Title] [nvarchar](8) NULL,
[FirstName] [dbo].[Name] NOT NULL,
[MiddleName] [dbo].[Name] NULL,
[LastName] [dbo].[Name] NOT NULL)
---
Create a query in T-SQL which returns the full name of any person whose
last name is Smith.
• Use section dividers, such as ---.
o When working with Python, using “”” instead of ### may produce better
results.
class Program
{
static void Main(string[] args)
{
int n = 10; // Number of Fibonacci numbers to generate
Console.WriteLine("Fibonacci Series:");
for (int i = 0; i < n; i++)
{
Console.WriteLine(Fibonacci(i));
}
}
“””
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print("Fibonacci Series:")
for i in range(n):
print(fibonacci(i))
• Start with a comment, data or code:
# Write some code that returns the Fibonacci numbers.
# End
# C# version
• Specify a library, or suggest a library or API
<!-- Use Bootstrap to write a page with three buttons -->
• Reduce the temperature
o Setting it as 0, or 0.1 or 0.2, tends to give better results.
o Higher temperatures can give you really random results.
• Limit the size of the query if necessary by reducing max_tokens.
Responsible Generative AI
• Azure OpenAI Service monitors content and behavior against its Code of
Conduct, using:
o Content Classification, for both images and language, in both prompts
and completions (inputs and outputs).
o Abuse Pattern Capture,
o Human Review and Decision,
o Notification and Action.
• It filters against the following categories:
o Hate and fairness (non-discrimination),
o Sexual references,
o Self-harm and
o Violence.
• It categories these against four severity levels:
o Safe,
o Low,
o Medium and
o High.
• You can configure in the Azure OpenAI Studio – Content filters to allow:
o Only safe,
o Safe and Low,
o Safe, Low and Medium
o No filters, if you have been approved for modified content filtering.
• It also protects against:
o Jailbreak attempts (trying to bypass policies),
o Protected material text (such as song lyrics, articles, recipes and select
web content), and
o Protected material code (source code).
• If your call is successful, then:
o The HTTP response code will be 200 (“OK”), and
o finish_reason will be either “stop” or “length”.
• If at least one of the responses was successful, and another was filtered, then:
o The HTTP response code will be 200, and
o finish_reason will be “content_filter”.
• If the responses were filtered, then:
o The HTTP response code will be 400 (“Bad request”).
• If the call is still continuing, then
o finish_reason will be null.
ChatCompletions
• You create a ChatCompletion path using:
o An OpenAI Endpoint (string),
o An OpenAI key (string), and
o An API version (string).
▪ This follows the “YYYY-MM-DD” or “YYYY-MM-DD-preview”
format.
▪ It is taken from https://learn.microsoft.com/en-us/azure/ai-
services/openai/reference#chat-completions
▪ At the time of writing, the latest stable version was “2023-05-15”
and the latest preview version was “2023-12-01-preview”.
• You then create a request body. For GPT-3.5-Turbo, this includes:
o model (C#)/deployment (Python). The name of the model.
o messages. This should include any previous messages. Each message
needs:
▪ role – either:
• system: provides the behavior for the model.
• user: the input for chat completions.
• assistant: responses to system-instructed, user-prompted
input
▪ content – the text.
o n – the number of choices to generate. The default is 1.
o temperature – a number between 0 and 2. The default is 1.
▪ Higher values will make the output more random.
▪ Lower values will make it more focused and repeated.
o top_p – an alternative to sampling with “temperature”, called “nucleus
sampling”.
▪ Microsoft recommends altering temperature or top_p, but not
both.
o Max_tokens – the maximum number of tokens to be used.
Versions of code
▪ C#
o The current version at the time of writing is 1.0.0-beta.13
o You should be specific as to which version you use, not just “the latest”.
Otherwise, you may find that future code breaks.
o The transition from 1.0.0-beta.9 to 1.0.0-beta.10 was a “breaking change”
– code running in .9 did not work in .10.
o The code for DALL-E currently works in 1.0.0-beta.9.
▪ Python
o The current version at the time of writing is 1.11.1. Version 1.0.0 was
introduced in November 2023.
o The transition from 0.28.1 to 1.x was a “breaking change”.
o The code for DALL-E currently works in 0.28.1
Console.WriteLine(ChatCompletionsResponse.Choices[0].Message.Content);
Console.WriteLine(ChatCompletionsResponse.Choices[1].Message.Content);
• It saves it in the variable “ChatCompletionsResponse”.
• To run the program, use:
dotnet run Program.cs
Python Code
• Create a .env file with:
AZURE_OPENAI_KEY=
AZURE_OPENAI_ENDPOINT=
AZURE_API_VERSION=
AZURE_OPENAI_MODEL=
• Fill in the settings, and save the file.
• Create a program.py file.
• Install the library:
pip install openai
o If you want a specific version of openai, then you would add that version
to the end of the command, after two equal signs:
pip install openai==0.28.1
• To check which version of OpenAI you have, you would use:
pip show openai
• Fill in the details from the Azure Open AI Service.
o The AZURE_OPENAI_KEY is from the “Keys and Endpoint” section – Key 1.
o The AZURE_OPENAI_ENDPOINT is from the “Keys and Endpoint” section
– Endpoint.
o The AZURE_API_VERSION is taken from https://learn.microsoft.com/en-
us/azure/ai-services/openai/reference#chat-completions
o The AZURE_OPENAI_MODEL is the name of the model itself.
import os
from dotenv import load_dotenv
from openai import AzureOpenAI
load_dotenv()
• The first, second and last lines are used to get the environmental variables.
• The third line imports the Azure Open AI class.
client = AzureOpenAI(api_key = os.getenv("AZURE_OPENAI_KEY"),
api_version = os.getenv("AZURE_API_VERSION"),
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
)
model_name=os.getenv("AZURE_OPENAI_MODEL")
• This creates the client, using the key, endpoint and version.
• It also retrieves the model name.
response = client.chat.completions.create(
model=model_name,
max_tokens=200,
temperature=0.8,
n=1,
messages=[
{"role": "system", "content": "You are being
helpful."},
{"role": "user", "content": "Write a slogan for a
computer programmer."}
])
• This uses the client previously defined to create a ChatCompletion.
• It passes in:
o The model name,
o The maximum number of tokens to be used,
o The temperature (from 0 to 1),
o The messages, which include:
▪ A system message and
▪ A user message.
• It saves it in the variable “response”.
print(response.choices[0].message.content)
o The regions you can use are limited. At the time of writing, there were 16
regions where your Azure OpenAI resource could be:
▪ Canada East, East US, East US 2, North Central US, South Central
US, West US
▪ France Central, Norway East, Sweden Central, Switzerland North,
UK South, West Europe
▪ South India
▪ Brazil South
▪ Japan East, Australia East
• You can use the following chat models:
o gpt-35-turbo (0301),
o gpt-35-turbo-16k,
o gpt-4, and
o gpt-4-32k.
• To add your own data in the Azure OpenAI Studio, click on “Add your data
(preview) – “+ Add a data source”.
• You can use the data sources:
o Azure AI Search
o Azure Blob Storage
o Azure Cosmos DB for MongoDB vCore
o URL/web address
o Upload files.
▪ You can use Text files, Markdown files, HTML files, Microsoft Word
files, Microsoft PowerPoint files, and pdfs.
• The best citation titles are from Markdown “.md” files.
• Text contents are extracts from PDFs as a pre-processing
step.
▪ There is a limit of 16 megabytes per upload (but you can do
multiple uploads).
• In this example, we will upload PDF files, including a “Microsoft Copilot Studio”
document, which states “Microsoft Copilot Studio is the new name for Power
Virtual Agents.”
o This is the only reference to Power Virtual Agents in this document.
• You will need to:
o Create an azure Blob storage resource,
o Create an Azure AI Search resource, and
o Turn on Cross-origin resource sharing (CORS), to allow Azure OpenAI to
access the storage account and Azure AI Search.
o Check “I acknowledge that connecting to an Azure AI Search account will
incur usage to my account”.
▪ Pricing the Basic version is about $80 per month.
▪ The Free tier is not supported for “Use your own data”.
• If the data source has not been attached to the Chat playground, you can:
o Click on “Add your data (preview) – “+ Add a data source”.
o Select the data source as “Azure AI Search”.
o Select the relevant Subscription, Azure AI Search service and Azure AI
Search Index.
Python code
• Add into the .env file:
SEARCH_ENDPOINT =
SEARCH_KEY =
SEARCH_INDEX =
• Reference them in the main code:
azure_search_endpoint = os.getenv("SEARCH_ENDPOINT")
azure_search_key = os.getenv("SEARCH_KEY")
azure_search_index = os.getenv("SEARCH_INDEX")
• Add a changed base URL:
client = AzureOpenAI(
base_url=f"{azure_oai_endpoint}/openai/deployments/{azure_oai_model}/extensions
",
api_key=azure_oai_key,
api_version="2023-09-01-preview")
• Create the dataSources, using the Search references, into a dictionary item, use
key-value pairs:
extra_config = dict(dataSources = [{
"type": "AzureCognitiveSearch",
"parameters": {
"endpoint": os.getenv("SEARCH_ENDPOINT"),
"key": os.getenv("SEARCH_KEY"),
"indexName": os.getenv("SEARCH_INDEX"),
}
}]
)
• Incorporate this new dictionary in the ChatCompletions request:
,
extra_body= extra_config