Skip to content

Commit 85fa89f

Browse files
author
Chris
committed
adding dotnet lessons, also adding simpler notebooks for ch07 and ch08 to make it easier for the learner
1 parent 8ac2255 commit 85fa89f

File tree

9 files changed

+327
-395
lines changed

9 files changed

+327
-395
lines changed

06-text-generation-apps/app-recipe.py

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525

2626
# engine
27-
engine = os.getenv("ENGINE")
2827

2928
# deployment_id
3029
deployment_name = os.getenv("DEPLOYMENT_NAME")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!meta
2+
3+
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}
4+
5+
#!csharp
6+
7+
#r "nuget: Azure.AI.OpenAI, 1.0.0-beta.8"
8+
9+
#!csharp
10+
11+
using Azure;
12+
using Azure.AI.OpenAI;
13+
using static System.Environment;
14+
15+
#!csharp
16+
17+
string endpoint = "<replace with endpoint>";
18+
string key = "<replace with API key>";
19+
20+
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
21+
22+
var chatCompletionsOptions = new ChatCompletionsOptions()
23+
{
24+
Messages =
25+
{
26+
new ChatMessage(ChatRole.System, "You are the president of France"),
27+
new ChatMessage(ChatRole.System, "You have just resigned"),
28+
new ChatMessage(ChatRole.User, "What tasks needs doing?")
29+
},
30+
MaxTokens = 100
31+
};
32+
33+
Response<ChatCompletions> response = client.GetChatCompletions("gpt-35-turbo", chatCompletionsOptions);
34+
35+
Console.WriteLine(response.Value.Choices[0].Message.Content);
36+
37+
Console.WriteLine();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"import openai\n",
11+
"\n",
12+
"openai.api_type = \"azure\"\n",
13+
"openai.api_version = os.getenv(\"AZURE_OPENAI_API_VERSION\",\"\").strip()\n",
14+
"\n",
15+
"API_KEY = os.getenv(\"AZURE_OPENAI_API_KEY\",\"\").strip()\n",
16+
"assert API_KEY, \"ERROR: Azure OpenAI Key is missing\"\n",
17+
"openai.api_key = API_KEY\n",
18+
"\n",
19+
"RESOURCE_ENDPOINT = os.getenv(\"OPENAI_API_BASE\",\"\").strip()\n",
20+
"assert RESOURCE_ENDPOINT, \"ERROR: Azure OpenAI Endpoint is missing\"\n",
21+
"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",
22+
"openai.api_base = RESOURCE_ENDPOINT\n",
23+
"deployment = \"gpt-35-turbo\" # replace with your deployment name"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# Create your first prompt\n",
33+
"text_prompt = \" My foot hurts, what can be wrong?\"\n",
34+
"\n",
35+
"response = openai.ChatCompletion.create(\n",
36+
" engine=deployment,\n",
37+
" messages = [\n",
38+
" {\"role\":\"system\", \"content\":\"I'm a doctor, specialist on surgery\"},\n",
39+
" {\"role\":\"user\",\"content\":text_prompt},])\n",
40+
"\n",
41+
"\n",
42+
"response['choices'][0]['message']['content']"
43+
]
44+
}
45+
],
46+
"metadata": {
47+
"kernelspec": {
48+
"display_name": "Python 3",
49+
"language": "python",
50+
"name": "python3"
51+
},
52+
"language_info": {
53+
"codemirror_mode": {
54+
"name": "ipython",
55+
"version": 3
56+
},
57+
"file_extension": ".py",
58+
"mimetype": "text/x-python",
59+
"name": "python",
60+
"nbconvert_exporter": "python",
61+
"pygments_lexer": "ipython3",
62+
"version": "3.10.11"
63+
},
64+
"orig_nbformat": 4
65+
},
66+
"nbformat": 4,
67+
"nbformat_minor": 2
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!meta
2+
3+
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}
4+
5+
#!csharp
6+
7+
#r "nuget: Azure.AI.OpenAI, 1.0.0-beta.9"
8+
9+
#!csharp
10+
11+
using Azure;
12+
using Azure.AI.OpenAI;
13+
14+
#!csharp
15+
16+
#r "nuget:Microsoft.DotNet.Interactive.AIUtilities, 1.0.0-beta.23557.4"
17+
18+
using Microsoft.DotNet.Interactive;
19+
using Microsoft.DotNet.Interactive.AIUtilities;
20+
21+
#!csharp
22+
23+
var azureOpenAIKey = "<replace with API key>";
24+
var azureOpenAIEndpoint = "<replace with endpoint>";
25+
var deployment = "<replace with deployment name, should be of type ADA embedding for Azure Open AI>";
26+
27+
#!csharp
28+
29+
OpenAIClient client = new (new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));
30+
31+
#!csharp
32+
33+
var source = "Car";
34+
var compareTo = "Vehicle";
35+
var parrot = "A bird";
36+
37+
var parrotEmbeddings = await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ parrot }));
38+
39+
// vector version
40+
var embeddings = await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ source }));
41+
42+
// vector version
43+
var embeddingsCompareTo = await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ compareTo }));
44+
45+
var sourceValue = embeddings.Value.Data[0].Display();
46+
Console.WriteLine(sourceValue);
47+
var compareToValue = embeddingsCompareTo.Value.Data[0].Display();
48+
Console.WriteLine(compareToValue);
49+
50+
#!csharp
51+
52+
var comparer = new CosineSimilarityComparer<float[]>(f => f);
53+
var carArray = embeddings.Value.Data[0].Embedding.ToArray(); // float []
54+
var vehicleArray = embeddingsCompareTo.Value.Data[0].Embedding.ToArray(); // float []
55+
var parrotArray = parrotEmbeddings.Value.Data[0].Embedding.ToArray(); // float []
56+
57+
var score = comparer.Score(carArray, vehicleArray);
58+
Console.WriteLine(score);
59+
60+
score = comparer.Score(carArray, parrotArray);
61+
Console.WriteLine(score);

0 commit comments

Comments
 (0)