-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding dotnet lessons, also adding simpler notebooks for ch07 and ch0…
…8 to make it easier for the learner
- Loading branch information
Chris
committed
Nov 24, 2023
1 parent
8ac2255
commit bff5889
Showing
9 changed files
with
329 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
07-building-chat-applications/dotnet/notebook-azure-openai.dib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!meta | ||
|
||
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}} | ||
|
||
#!csharp | ||
|
||
#r "nuget: Azure.AI.OpenAI, 1.0.0-beta.8" | ||
|
||
#!csharp | ||
|
||
using Azure; | ||
using Azure.AI.OpenAI; | ||
using static System.Environment; | ||
|
||
#!csharp | ||
|
||
string endpoint = "<replace with endpoint>"; | ||
string key = "<replace with API key>"; | ||
|
||
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key)); | ||
|
||
var chatCompletionsOptions = new ChatCompletionsOptions() | ||
{ | ||
Messages = | ||
{ | ||
new ChatMessage(ChatRole.System, "You are the president of France"), | ||
new ChatMessage(ChatRole.System, "You have just resigned"), | ||
new ChatMessage(ChatRole.User, "What tasks needs doing?") | ||
}, | ||
MaxTokens = 100 | ||
}; | ||
|
||
Response<ChatCompletions> response = client.GetChatCompletions("gpt-35-turbo", chatCompletionsOptions); | ||
|
||
Console.WriteLine(response.Value.Choices[0].Message.Content); | ||
|
||
Console.WriteLine(); |
68 changes: 68 additions & 0 deletions
68
07-building-chat-applications/notebook-simple-azure-openai.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import openai\n", | ||
"\n", | ||
"openai.api_type = \"azure\"\n", | ||
"openai.api_version = os.getenv(\"AZURE_OPENAI_API_VERSION\",\"\").strip()\n", | ||
"\n", | ||
"API_KEY = os.getenv(\"AZURE_OPENAI_API_KEY\",\"\").strip()\n", | ||
"assert API_KEY, \"ERROR: Azure OpenAI Key is missing\"\n", | ||
"openai.api_key = API_KEY\n", | ||
"\n", | ||
"RESOURCE_ENDPOINT = os.getenv(\"OPENAI_API_BASE\",\"\").strip()\n", | ||
"assert RESOURCE_ENDPOINT, \"ERROR: Azure OpenAI Endpoint is missing\"\n", | ||
"assert \"openai.azure.com\" in RESOURCE_ENDPOINT.lower(), \"ERROR: Azure OpenAI Endpoint should be in the form: \\n\\n\\t<your unique endpoint identifier>.openai.azure.com\"\n", | ||
"openai.api_base = RESOURCE_ENDPOINT\n", | ||
"deployment = \"gpt-35-turbo\" # replace with your deployment name" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create your first prompt\n", | ||
"text_prompt = \" My foot hurts, what can be wrong?\"\n", | ||
"\n", | ||
"response = openai.ChatCompletion.create(\n", | ||
" engine=deployment,\n", | ||
" messages = [\n", | ||
" {\"role\":\"system\", \"content\":\"I'm a doctor, specialist on surgery\"},\n", | ||
" {\"role\":\"user\",\"content\":text_prompt},])\n", | ||
"\n", | ||
"\n", | ||
"response['choices'][0]['message']['content']" | ||
] | ||
} | ||
], | ||
"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.10.11" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
61 changes: 61 additions & 0 deletions
61
08-building-search-applications/dotnet/notebook-azure-openai.dib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!meta | ||
|
||
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}} | ||
|
||
#!csharp | ||
|
||
#r "nuget: Azure.AI.OpenAI, 1.0.0-beta.9" | ||
|
||
#!csharp | ||
|
||
using Azure; | ||
using Azure.AI.OpenAI; | ||
|
||
#!csharp | ||
|
||
#r "nuget:Microsoft.DotNet.Interactive.AIUtilities, 1.0.0-beta.23557.4" | ||
|
||
using Microsoft.DotNet.Interactive; | ||
using Microsoft.DotNet.Interactive.AIUtilities; | ||
|
||
#!csharp | ||
|
||
var azureOpenAIKey = "<replace with API key>"; | ||
var azureOpenAIEndpoint = "<replace with endpoint>"; | ||
var deployment = "<replace with deployment name, should be of type ADA embedding for Azure Open AI>"; | ||
|
||
#!csharp | ||
|
||
OpenAIClient client = new (new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey)); | ||
|
||
#!csharp | ||
|
||
var source = "Car"; | ||
var compareTo = "Vehicle"; | ||
var parrot = "A bird"; | ||
|
||
var parrotEmbeddings = await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ parrot })); | ||
|
||
// vector version | ||
var embeddings = await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ source })); | ||
|
||
// vector version | ||
var embeddingsCompareTo = await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ compareTo })); | ||
|
||
var sourceValue = embeddings.Value.Data[0].Display(); | ||
Console.WriteLine(sourceValue); | ||
var compareToValue = embeddingsCompareTo.Value.Data[0].Display(); | ||
Console.WriteLine(compareToValue); | ||
|
||
#!csharp | ||
|
||
var comparer = new CosineSimilarityComparer<float[]>(f => f); | ||
var carArray = embeddings.Value.Data[0].Embedding.ToArray(); // float [] | ||
var vehicleArray = embeddingsCompareTo.Value.Data[0].Embedding.ToArray(); // float [] | ||
var parrotArray = parrotEmbeddings.Value.Data[0].Embedding.ToArray(); // float [] | ||
|
||
var score = comparer.Score(carArray, vehicleArray); | ||
Console.WriteLine(score); | ||
|
||
score = comparer.Score(carArray, parrotArray); | ||
Console.WriteLine(score); |
Oops, something went wrong.