首页 新闻 会员 周边

Azure.AI.OpenAI.ChatResponseMessage json 序列化问题

0
悬赏园豆:30 [待解决问题]

下面是序列化 ChatResponseMessage 的代码

var message = AzureOpenAIModelFactory.ChatResponseMessage(ChatRole.Assistant, "OK");
var json = JsonSerializer.Serialize(
    message,
    new JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        WriteIndented = true
    });
Console.WriteLine(json);

输出的 json 却丢了 role 属性(类型是 ChatRole)的值

{
  "role": {},
  "content": "OK",
  "toolCalls": [],
  "functionCall": null,
  "azureExtensionsContext": null
}

请问如何解决这个问题?

问题补充:

Console.WriteLine(message.Role); 的输出是 assistant

dudu的主页 dudu | 高人七级 | 园豆:30966
提问于:2024-02-17 12:45
< >
分享
所有回答(1)
0

在 github 的这个 issue How to deserialize OpenAI ChatMessage correctly?评论中知道了原因:

the issue is that the generated ChatRole type has a private, non-property field that doesn't work nicely by default with System.Text.Json.Serialization. I suspect deeper inspection will reveal that Role on a ChatMessage gets serialized with an empty value, which definitely makes deserialization incorrect later on.

dudu | 园豆:30966 (高人七级) | 2024-02-17 20:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册