Skip to content

Commit b3bcaca

Browse files
committed
Add simple embedding example using protocol method
1 parent 3e3bbfc commit b3bcaca

4 files changed

+98
-10
lines changed

examples/Chat/Example06_SimpleChatProtocol.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void Example06_SimpleChatProtocol()
1313
{
1414
ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
1515

16-
BinaryData input = BinaryData.FromString("""
16+
BinaryData input = BinaryData.FromBytes("""
1717
{
1818
"model": "gpt-4o",
1919
"messages": [
@@ -23,17 +23,17 @@ public void Example06_SimpleChatProtocol()
2323
}
2424
]
2525
}
26-
""");
26+
"""u8.ToArray());
2727

2828
using BinaryContent content = BinaryContent.Create(input);
2929
ClientResult result = client.CompleteChat(content);
3030
BinaryData output = result.GetRawResponse().Content;
3131

3232
using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
3333
string message = outputAsJson.RootElement
34-
.GetProperty("choices")[0]
35-
.GetProperty("message")
36-
.GetProperty("content")
34+
.GetProperty("choices"u8)[0]
35+
.GetProperty("message"u8)
36+
.GetProperty("content"u8)
3737
.GetString();
3838

3939
Console.WriteLine($"[ASSISTANT]:");

examples/Chat/Example06_SimpleChatProtocolAsync.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task Example06_SimpleChatProtocolAsync()
1414
{
1515
ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
1616

17-
BinaryData input = BinaryData.FromString("""
17+
BinaryData input = BinaryData.FromBytes("""
1818
{
1919
"model": "gpt-4o",
2020
"messages": [
@@ -24,17 +24,17 @@ public async Task Example06_SimpleChatProtocolAsync()
2424
}
2525
]
2626
}
27-
""");
27+
"""u8.ToArray());
2828

2929
using BinaryContent content = BinaryContent.Create(input);
3030
ClientResult result = await client.CompleteChatAsync(content);
3131
BinaryData output = result.GetRawResponse().Content;
3232

3333
using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
3434
string message = outputAsJson.RootElement
35-
.GetProperty("choices")[0]
36-
.GetProperty("message")
37-
.GetProperty("content")
35+
.GetProperty("choices"u8)[0]
36+
.GetProperty("message"u8)
37+
.GetProperty("content"u8)
3838
.GetString();
3939

4040
Console.WriteLine($"[ASSISTANT]:");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using NUnit.Framework;
2+
using OpenAI.Embeddings;
3+
using System;
4+
using System.ClientModel;
5+
using System.Text.Json;
6+
7+
namespace OpenAI.Examples;
8+
9+
public partial class EmbeddingExamples
10+
{
11+
[Test]
12+
public void Example04_SimpleEmbeddingProtocol()
13+
{
14+
EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
15+
16+
string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa,"
17+
+ " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
18+
+ " attractions. We highly recommend this hotel.";
19+
20+
BinaryData input = BinaryData.FromObjectAsJson(new {
21+
model = "text-embedding-3-small",
22+
input = description,
23+
encoding_format = "float"
24+
});
25+
26+
using BinaryContent content = BinaryContent.Create(input);
27+
ClientResult result = client.GenerateEmbeddings(content);
28+
BinaryData output = result.GetRawResponse().Content;
29+
30+
using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
31+
JsonElement vector = outputAsJson.RootElement
32+
.GetProperty("data"u8)[0]
33+
.GetProperty("embedding"u8);
34+
35+
Console.WriteLine($"Dimension: {vector.GetArrayLength()}");
36+
Console.WriteLine($"Floats: ");
37+
int i = 0;
38+
foreach (JsonElement element in vector.EnumerateArray())
39+
{
40+
Console.WriteLine($" [{i++,4}] = {element.GetDouble()}");
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NUnit.Framework;
2+
using OpenAI.Embeddings;
3+
using System;
4+
using System.ClientModel;
5+
using System.Text.Json;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenAI.Examples;
9+
10+
public partial class EmbeddingExamples
11+
{
12+
[Test]
13+
public async Task Example04_SimpleEmbeddingProtocolAsync()
14+
{
15+
EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
16+
17+
string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa,"
18+
+ " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
19+
+ " attractions. We highly recommend this hotel.";
20+
21+
BinaryData input = BinaryData.FromObjectAsJson(new
22+
{
23+
model = "text-embedding-3-small",
24+
input = description,
25+
encoding_format = "float"
26+
});
27+
28+
using BinaryContent content = BinaryContent.Create(input);
29+
ClientResult result = await client.GenerateEmbeddingsAsync(content);
30+
BinaryData output = result.GetRawResponse().Content;
31+
32+
using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
33+
JsonElement vector = outputAsJson.RootElement
34+
.GetProperty("data"u8)[0]
35+
.GetProperty("embedding"u8);
36+
37+
Console.WriteLine($"Dimension: {vector.GetArrayLength()}");
38+
Console.WriteLine($"Floats: ");
39+
int i = 0;
40+
foreach (JsonElement element in vector.EnumerateArray())
41+
{
42+
Console.WriteLine($" [{i++,4}] = {element.GetDouble()}");
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)