AI-Studio/app/MindWork AI Studio/Provider/OpenAI/AnnotationConverter.cs
Thorsten Sommer 38ec098430
Some checks are pending
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Improved the OpenAI provider (#548)
2025-09-03 10:08:04 +02:00

62 lines
2.3 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace AIStudio.Provider.OpenAI;
/// <summary>
/// Custom JSON converter for the annotation class to handle polymorphic deserialization.
/// </summary>
/// <remarks>
/// We use this converter for chat completion API and responses API annotation deserialization.
/// </remarks>
public sealed class AnnotationConverter : JsonConverter<Annotation>
{
public override Annotation? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var doc = JsonDocument.ParseValue(ref reader);
var root = doc.RootElement;
if (!root.TryGetProperty("type", out var typeElement))
return null;
var type = typeElement.GetString();
var rawText = root.GetRawText();
Annotation? annotation;
switch (type)
{
case "url_citation":
// Let's check the responses API data type first:
var responsesAnnotation = JsonSerializer.Deserialize<ResponsesAnnotatingUrlCitationData>(rawText, options);
// If it fails, let's try the chat completion API data type:
if(responsesAnnotation is null || string.IsNullOrWhiteSpace(responsesAnnotation.Title) || string.IsNullOrWhiteSpace(responsesAnnotation.URL))
{
// Try chat completion API data type:
var chatCompletionAnnotation = JsonSerializer.Deserialize<ChatCompletionAnnotatingURL>(rawText, options);
// If both fail, we return the unknown type:
if(chatCompletionAnnotation is null)
annotation = new AnnotatingUnknown(type);
else
annotation = chatCompletionAnnotation;
}
else
annotation = responsesAnnotation;
break;
default:
annotation = new AnnotatingUnknown(type ?? "unknown");
break;
}
return annotation;
}
public override void Write(Utf8JsonWriter writer, Annotation value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}