python-3.x ChatGPT助手知识文件上传JSON结构

jei2mxaa  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(125)

我正试图上传一个JSON文件与文章结构。这样GPT助手可以通过文件上传访问JSON文件。我试着给予一个.json,.txt和.pdf文件。
但我总是得到错误:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid file format. Supported formats: ['c', 'cpp', 'csv', 'docx', 'html', 'java', 'json', 'md', 'pdf', 'php', 'pptx', 'py', 'rb', 'tex', 'txt', 'css', 'jpeg', 'jpg', 'js', 'gif', 'png', 'tar', 'ts', 'xlsx', 'xml', 'zip']", 'type': 'invalid_request_error', 'param': None, 'code': None}}

字符串
这是我尝试上传的文章结构

{
"article": {
"title": "",
"introduction": "",
"paragraphs": [
{
"paragraphTitle": "",
"subParagraphs": [
{
"subParagraphTitle": "",
"subParagraphPoints": ["", ""]
},
{
"subParagraphTitle": "",
"subParagraphPoints": ["", ""]
}
]
},
{
"paragraphTitle": "",
"subParagraphs": [
{
"subParagraphTitle": "",
"subParagraphPoints": ["", ""]
},
{
"subParagraphTitle": "",
"subParagraphPoints": ["", ""]
}
]
}
],
"conclusion": ""
}
  }


这是我的代码

def upload_file(path):
    
    file = client.files.create(
        file=open(path, "rb"), purpose="assistants" 
        
    )
    return file

file_path = testfiles\article_structure.txt

file = upload_file(path=file_path)


还有一个奇怪的行为:
如果我将.txt文件的内容更改为一个简单的字符串,例如“Hello World”,该文件将被接受。但当我将JSON文章结构放入其中时。我再次收到上面提到的错误。

taor4pac

taor4pac1#

好了伙计们,
找到问题了。
正如你在我的JSON结构中看到的,我有空值(“”)作为占位符。但这正是导致问题的原因。
如果我添加值,则文件被接受。

{
    "article": {
      "title": "This is a title",
      "introduction": "",
      "paragraphs": [
        {
          "paragraphTitle": "This is prargraph title 1",
          "subParagraphs": [
            {
              "subParagraphTitle": "This is subprargraph title 1.1",
              "subParagraphPoints": [
                "",
                ""
              ]
            },
            {
              "subParagraphTitle": "This is subprargraph title 1.2",
              "subParagraphPoints": [
                "",
                ""
              ]
            }
          ]
        },
        {
          "paragraphTitle": "This is prargraph title 2",
          "subParagraphs": [
            {
              "subParagraphTitle": "This is subprargraph title 2.1",
              "subParagraphPoints": [
                "",
                ""
              ]
            },
            {
              "subParagraphTitle": "This is subprargraph title 2.2",
              "subParagraphPoints": [
                "",
                ""
              ]
            }
          ]
        }
      ],
      "conclusion": ""
    }
  }

字符串

相关问题