Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
### Bugs Fixed

- ([#49](https://github.com/openai/openai-dotnet/issues/49)) Fixed a bug with extensible enums implementing case-insensitive equality but case-sensitive hash codes. (commit_hash)
- ([#57](https://github.com/openai/openai-dotnet/issues/57)) Fixed a bug with requests URIs with query string parameter potentially containing a malformed double question mark (`??`) on .NET Framework (net481).
- ([#57](https://github.com/openai/openai-dotnet/issues/57)) Fixed a bug with requests URIs with query string parameter potentially containing a malformed double question mark (`??`) on .NET Framework (net481). (commit_hash)

### Other Changes

Expand Down
10 changes: 5 additions & 5 deletions examples/Chat/Example06_SimpleChatProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Example06_SimpleChatProtocol()
{
ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

BinaryData input = BinaryData.FromString("""
BinaryData input = BinaryData.FromBytes("""
{
"model": "gpt-4o",
"messages": [
Expand All @@ -23,17 +23,17 @@ public void Example06_SimpleChatProtocol()
}
]
}
""");
"""u8.ToArray());

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

using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
string message = outputAsJson.RootElement
.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetProperty("choices"u8)[0]
.GetProperty("message"u8)
.GetProperty("content"u8)
.GetString();

Console.WriteLine($"[ASSISTANT]:");
Expand Down
10 changes: 5 additions & 5 deletions examples/Chat/Example06_SimpleChatProtocolAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task Example06_SimpleChatProtocolAsync()
{
ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

BinaryData input = BinaryData.FromString("""
BinaryData input = BinaryData.FromBytes("""
{
"model": "gpt-4o",
"messages": [
Expand All @@ -24,17 +24,17 @@ public async Task Example06_SimpleChatProtocolAsync()
}
]
}
""");
"""u8.ToArray());

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

using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
string message = outputAsJson.RootElement
.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetProperty("choices"u8)[0]
.GetProperty("message"u8)
.GetProperty("content"u8)
.GetString();

Console.WriteLine($"[ASSISTANT]:");
Expand Down
43 changes: 43 additions & 0 deletions examples/Embeddings/Example04_SimpleEmbeddingProtocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NUnit.Framework;
using OpenAI.Embeddings;
using System;
using System.ClientModel;
using System.Text.Json;

namespace OpenAI.Examples;

public partial class EmbeddingExamples
{
[Test]
public void Example04_SimpleEmbeddingProtocol()
{
EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa,"
+ " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
+ " attractions. We highly recommend this hotel.";

BinaryData input = BinaryData.FromObjectAsJson(new {
model = "text-embedding-3-small",
input = description,
encoding_format = "float"
});

using BinaryContent content = BinaryContent.Create(input);
ClientResult result = client.GenerateEmbeddings(content);
BinaryData output = result.GetRawResponse().Content;

using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
JsonElement vector = outputAsJson.RootElement
.GetProperty("data"u8)[0]
.GetProperty("embedding"u8);

Console.WriteLine($"Dimension: {vector.GetArrayLength()}");
Console.WriteLine($"Floats: ");
int i = 0;
foreach (JsonElement element in vector.EnumerateArray())
{
Console.WriteLine($" [{i++,4}] = {element.GetDouble()}");
}
}
}
45 changes: 45 additions & 0 deletions examples/Embeddings/Example04_SimpleEmbeddingProtocolAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using NUnit.Framework;
using OpenAI.Embeddings;
using System;
using System.ClientModel;
using System.Text.Json;
using System.Threading.Tasks;

namespace OpenAI.Examples;

public partial class EmbeddingExamples
{
[Test]
public async Task Example04_SimpleEmbeddingProtocolAsync()
{
EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa,"
+ " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
+ " attractions. We highly recommend this hotel.";

BinaryData input = BinaryData.FromObjectAsJson(new
{
model = "text-embedding-3-small",
input = description,
encoding_format = "float"
});

using BinaryContent content = BinaryContent.Create(input);
ClientResult result = await client.GenerateEmbeddingsAsync(content);
BinaryData output = result.GetRawResponse().Content;

using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
JsonElement vector = outputAsJson.RootElement
.GetProperty("data"u8)[0]
.GetProperty("embedding"u8);

Console.WriteLine($"Dimension: {vector.GetArrayLength()}");
Console.WriteLine($"Floats: ");
int i = 0;
foreach (JsonElement element in vector.EnumerateArray())
{
Console.WriteLine($" [{i++,4}] = {element.GetDouble()}");
}
}
}