Skip to content

Commit 5e19ec7

Browse files
committed
fix embedding example
1 parent 724ccd7 commit 5e19ec7

File tree

2 files changed

+277
-8
lines changed

2 files changed

+277
-8
lines changed
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Comparing text using embeddings"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Installation\n",
15+
"Install the Azure Open AI SDK using the below command."
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"metadata": {
22+
"dotnet_interactive": {
23+
"language": "csharp"
24+
},
25+
"polyglot_notebook": {
26+
"kernelName": "csharp"
27+
},
28+
"vscode": {
29+
"languageId": "polyglot-notebook"
30+
}
31+
},
32+
"outputs": [],
33+
"source": [
34+
"#r \"nuget: Azure.AI.OpenAI, 1.0.0-beta.14\"\n",
35+
"#r \"nuget:Microsoft.DotNet.Interactive.AIUtilities, 1.0.0-beta.24129.1\""
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 4,
41+
"metadata": {
42+
"dotnet_interactive": {
43+
"language": "csharp"
44+
},
45+
"polyglot_notebook": {
46+
"kernelName": "csharp"
47+
},
48+
"vscode": {
49+
"languageId": "polyglot-notebook"
50+
}
51+
},
52+
"outputs": [],
53+
"source": [
54+
"using Microsoft.DotNet.Interactive;"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"## Run this cell, it will prompt you for the apiKey, endPoint, and embedding deployment"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 8,
67+
"metadata": {
68+
"dotnet_interactive": {
69+
"language": "csharp"
70+
},
71+
"polyglot_notebook": {
72+
"kernelName": "csharp"
73+
},
74+
"vscode": {
75+
"languageId": "polyglot-notebook"
76+
}
77+
},
78+
"outputs": [],
79+
"source": [
80+
"var azureOpenAIKey = await Kernel.GetPasswordAsync(\"Provide your OPEN_AI_KEY\");\n",
81+
"\n",
82+
"// Your endpoint should look like the following https://YOUR_OPEN_AI_RESOURCE_NAME.openai.azure.com/\n",
83+
"var azureOpenAIEndpoint = await Kernel.GetInputAsync(\"Provide the OPEN_AI_ENDPOINT\");\n",
84+
"\n",
85+
"// Enter the deployment name you chose when you deployed the model.\n",
86+
"var deployment = await Kernel.GetInputAsync(\"Provide embedding deployment name\");"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"### Import namesapaces and create an instance of `OpenAiClient` using the `azureOpenAIEndpoint` and the `azureOpenAIKey`"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 7,
99+
"metadata": {
100+
"dotnet_interactive": {
101+
"language": "csharp"
102+
},
103+
"polyglot_notebook": {
104+
"kernelName": "csharp"
105+
},
106+
"vscode": {
107+
"languageId": "polyglot-notebook"
108+
}
109+
},
110+
"outputs": [],
111+
"source": [
112+
"using Azure;\n",
113+
"using Azure.AI.OpenAI;"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 10,
119+
"metadata": {
120+
"dotnet_interactive": {
121+
"language": "csharp"
122+
},
123+
"polyglot_notebook": {
124+
"kernelName": "csharp"
125+
},
126+
"vscode": {
127+
"languageId": "polyglot-notebook"
128+
}
129+
},
130+
"outputs": [],
131+
"source": [
132+
"OpenAIClient client = new (new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey.GetClearTextPassword()));"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"### Create some text"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 11,
145+
"metadata": {
146+
"dotnet_interactive": {
147+
"language": "csharp"
148+
},
149+
"polyglot_notebook": {
150+
"kernelName": "csharp"
151+
},
152+
"vscode": {
153+
"languageId": "polyglot-notebook"
154+
}
155+
},
156+
"outputs": [],
157+
"source": [
158+
"var firstSentence = \"The quick brown fox jumps over the lazy dog\";\n",
159+
"var secondSentence = \"The quick fox jumps over the black dog\";"
160+
]
161+
},
162+
{
163+
"cell_type": "markdown",
164+
"metadata": {},
165+
"source": [
166+
"### Create text embeddings using the `deployment`"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 14,
172+
"metadata": {
173+
"dotnet_interactive": {
174+
"language": "csharp"
175+
},
176+
"polyglot_notebook": {
177+
"kernelName": "csharp"
178+
},
179+
"vscode": {
180+
"languageId": "polyglot-notebook"
181+
}
182+
},
183+
"outputs": [],
184+
"source": [
185+
"var firstEmbeddings = (await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ firstSentence }))).Value.Data[0].Embedding.ToArray();\n",
186+
"var secondEmbeddings = (await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ secondSentence }))).Value.Data[0].Embedding.ToArray();"
187+
]
188+
},
189+
{
190+
"cell_type": "markdown",
191+
"metadata": {},
192+
"source": [
193+
"# calculate Cosine Similarity"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 16,
199+
"metadata": {
200+
"dotnet_interactive": {
201+
"language": "csharp"
202+
},
203+
"polyglot_notebook": {
204+
"kernelName": "csharp"
205+
},
206+
"vscode": {
207+
"languageId": "polyglot-notebook"
208+
}
209+
},
210+
"outputs": [
211+
{
212+
"data": {
213+
"text/html": [
214+
"<div class=\"dni-plaintext\"><pre>0.948887</pre></div><style>\r\n",
215+
".dni-code-hint {\r\n",
216+
" font-style: italic;\r\n",
217+
" overflow: hidden;\r\n",
218+
" white-space: nowrap;\r\n",
219+
"}\r\n",
220+
".dni-treeview {\r\n",
221+
" white-space: nowrap;\r\n",
222+
"}\r\n",
223+
".dni-treeview td {\r\n",
224+
" vertical-align: top;\r\n",
225+
" text-align: start;\r\n",
226+
"}\r\n",
227+
"details.dni-treeview {\r\n",
228+
" padding-left: 1em;\r\n",
229+
"}\r\n",
230+
"table td {\r\n",
231+
" text-align: start;\r\n",
232+
"}\r\n",
233+
"table tr { \r\n",
234+
" vertical-align: top; \r\n",
235+
" margin: 0em 0px;\r\n",
236+
"}\r\n",
237+
"table tr td pre \r\n",
238+
"{ \r\n",
239+
" vertical-align: top !important; \r\n",
240+
" margin: 0em 0px !important;\r\n",
241+
"} \r\n",
242+
"table th {\r\n",
243+
" text-align: start;\r\n",
244+
"}\r\n",
245+
"</style>"
246+
]
247+
},
248+
"metadata": {},
249+
"output_type": "display_data"
250+
}
251+
],
252+
"source": [
253+
"using Microsoft.DotNet.Interactive.AIUtilities;\n",
254+
"\n",
255+
"var similarityComparer = new CosineSimilarityComparer<float[]>(f => f);\n",
256+
"var similarity = similarityComparer.Score(firstEmbeddings, secondEmbeddings);\n",
257+
"similarity.Display();"
258+
]
259+
}
260+
],
261+
"metadata": {
262+
"language_info": {
263+
"name": "csharp"
264+
},
265+
"orig_nbformat": 4
266+
},
267+
"nbformat": 4,
268+
"nbformat_minor": 2
269+
}

Basic_Samples/Embeddings/dotnet/csharp/Get_embeddings.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 1,
21+
"execution_count": 2,
2222
"metadata": {
2323
"dotnet_interactive": {
2424
"language": "csharp"
@@ -47,7 +47,7 @@
4747
},
4848
{
4949
"cell_type": "code",
50-
"execution_count": 2,
50+
"execution_count": 1,
5151
"metadata": {
5252
"dotnet_interactive": {
5353
"language": "csharp"
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"cell_type": "code",
76-
"execution_count": 2,
76+
"execution_count": 5,
7777
"metadata": {
7878
"dotnet_interactive": {
7979
"language": "csharp"
@@ -87,13 +87,13 @@
8787
},
8888
"outputs": [],
8989
"source": [
90-
"var azureOpenAIKey = \"e1b9ff2e05d24a9ea3731705903f9c95\";\n",
90+
"var azureOpenAIKey = await Kernel.GetPasswordAsync(\"Provide your OPEN_AI_KEY\");\n",
9191
"\n",
9292
"// Your endpoint should look like the following https://YOUR_OPEN_AI_RESOURCE_NAME.openai.azure.com/\n",
93-
"var azureOpenAIEndpoint = \"https://dicolomb-west-us.openai.azure.com\";\n",
93+
"var azureOpenAIEndpoint = await Kernel.GetInputAsync(\"Provide the OPEN_AI_ENDPOINT\");\n",
9494
"\n",
9595
"// Enter the deployment name you chose when you deployed the model.\n",
96-
"var deployment = \"text-embedding-ada-002\";"
96+
"var deployment = await Kernel.GetInputAsync(\"Provide embedding deployment name\");"
9797
]
9898
},
9999
{
@@ -125,7 +125,7 @@
125125
},
126126
{
127127
"cell_type": "code",
128-
"execution_count": 4,
128+
"execution_count": 6,
129129
"metadata": {
130130
"dotnet_interactive": {
131131
"language": "csharp"
@@ -139,7 +139,7 @@
139139
},
140140
"outputs": [],
141141
"source": [
142-
"OpenAIClient client = new (new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));"
142+
"OpenAIClient client = new (new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey.GetClearTextPassword()));"
143143
]
144144
},
145145
{

0 commit comments

Comments
 (0)