rksoftware

Visual Studio とか C# とかが好きです

GitHub Copilot Extensions の AI からの返答をへテキストにするコードのメモ

public class CopilotLlmResponseData
{
    private static JsonSerializerOptions options { get; } = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

    public static List<CopilotLlmResponseData> Parse(string response) =>
        response?.Split("data:")
        .Select(a =>
        {
            try
            {
                var t =
                System.Text.Json.JsonSerializer.Deserialize<CopilotLlmResponseData>(a.Trim(), options) ?? new CopilotLlmResponseData();
                return t;
            }
            catch {; }
            return new CopilotLlmResponseData();
        })
        .ToList() ?? new List<CopilotLlmResponseData>();

    public static string Text(List<CopilotLlmResponseData> data) =>
        string.Join("", data.SelectMany(a => a?.Choices?.Select(b => b.Delta?.Content) ?? []));

    public static string Text(string data) => Text(Parse(data));

    public List<Choice>? Choices { get; set; }
    public int Created { get; set; }
    public string? Id { get; set; }
    public string? Model { get; set; }
    public string? System_fingerprint { get; set; }
}

public class Choice
{
    public int Index { get; set; }
    public Content_Filter_Offsets? Content_filter_offsets { get; set; }
    public Content_Filter_Results? Content_filter_results { get; set; }
    public Delta? Delta { get; set; }
}

public class Content_Filter_Offsets
{
    public int Check_offset { get; set; }
    public int Start_offset { get; set; }
    public int End_offset { get; set; }
}

public class Content_Filter_Results
{
    public Error? Error { get; set; }
    public Hate? Hate { get; set; }
    public Self_Harm? Self_harm { get; set; }
    public Sexual? Sexual { get; set; }
    public Violence? Violence { get; set; }
}

public class Error