You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenAI C# SDK上传JSON文件并在Prompt中引用失败的问题求助

使用OpenAI C# SDK上传JSON文件并在Prompt中引用失败的问题求助

大家好,我最近在尝试用OpenAI的C# SDK上传JSON文件,然后在对话prompt里引用它的内容,但折腾了好一阵都没成功,遇到了两个头疼的问题,想请教下各位大佬:

问题1:放在UserChatMessage中抛出400错误

如果我把JSON文件作为文件附件加到UserChatMessage里,直接就会抛出异常,提示只支持PDF格式的base64数据:

System.ClientModel.ClientResultException: 'HTTP 400 (invalid_request_error: invalid_value) Parameter: messages[1].content[0].file.file_data Invalid file data: 'messages[1].content[0].file.file_data'. Expected a base64-encoded data URL with an application/pdf MIME type (e.g. 'data:application/pdf;base64,SGVsbG8sIFdvcmxkIQ=='), but got unsupported MIME type 'application/json'.'

问题2:放在SystemChatMessage中无报错但GPT无法访问文件

后来我改成把文件附件加到SystemChatMessage里,这次没有抛出异常了,但当我在prompt里询问是否能访问这个文件时,GPT回复说在对话里没找到名为property.json的文件:

I don’t see a file named property.json uploaded in this conversation. If you’d like me to review or analyze a file, please upload it, and I’ll be happy to help!

我的测试代码

这是我用来测试的单元测试代码:

[TestMethod]
public async Task TestJsonUpload1()
{
    var openAiApiKey = RprConfigurationManager.GetAppSetting<string>("GenAiOpenAiApiKey");
    ChatClient client = new(model: "gpt-4.1", apiKey: openAiApiKey);

    var json = """
    {
      "propertyId": 37037566,
      "propertyMode": "Residential",
      "propertyType": "Single Family",
      "address": "10900 Fox Hedge Rd, Matthews, NC 28105",
      "zip": "28105",
      "centroid": {
        "latitude": 35.051999,
        "longitude": -80.752768
      },
      "transactionType": "For Sale",
      "listingStatus": "Closed",
      "zoning": "R-3",
      "listDate": "2024-11-07T00:00:00Z",
      "offMarketDate": "2025-02-20T00:00:00Z",
      "pendingDate": "2025-01-25T00:00:00Z",
      "yearBuilt": 1999,
      "bedrooms": 5,
      "bathrooms": 3,
      "livingArea": "3,760 sq ft",
      "homeownersAssociationFeatures": {
        "Dues": "$1,175",
        "Payments Per Year": "1"
      }
    }
    """;

    var completionOptions = new ChatCompletionOptions
    {
        MaxOutputTokenCount = 350,
        Temperature = .4f,
        TopP = .8f
    };

    var chatMessages = new List<ChatMessage>
    {
        new SystemChatMessage([ChatMessageContentPart.CreateFilePart(BinaryData.FromString(json), "application/json", "property.json")]),
        new SystemChatMessage("Do you have access to the property.json message that I uploaded?"),
    };

    ChatCompletion completion = await client.CompleteChatAsync(chatMessages, completionOptions);
    var message = completion.Content.Any() ? completion.Content[0].Text : null;
    Console.WriteLine(message);
}

我现在的疑问是:

  • 是不是我使用的gpt-4.1模型不支持在对话中引用JSON文件?
  • 还是我上传文件的方式本身就不对?有没有正确的方法能让我成功上传JSON文件,并且让GPT在对话中可以访问到它的内容?

麻烦各位帮忙看看,谢谢啦!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 07:40:30