ruby-openai icon indicating copy to clipboard operation
ruby-openai copied to clipboard

Content retrieval (and anything calling .get) only works for JSON

Open DanL12186 opened this issue 1 year ago • 3 comments

Describe the bug Attempting to retrieve the content of files via the API doesn't work unless the files are in JSON format; you'll get a JSON::ParserError error, because get() in http.rb calls jsonparsel() which calls JSON.parse(response) for every request.

For example, when trying to retrieve an image so we can attach it via ActiveStorage in order to easily display it to the user:

>> client.files.content(id: "file-xyz123")
JSON::ParserError: 451: unexpected token at '?PNG

Simply returning the response if it's not JSON would resolve this.

To Reproduce Steps to reproduce the behavior:

  1. Have the API generate a file for you
  2. Call client.files.content(id: file_id)
  3. See error

Expected behavior It should retrieve non-JSON content like generated PNGs successfully

Desktop (please complete the following information):

  • OS: MacOS X 10.15.7
  • Browser N/A
  • Version N/A

DanL12186 avatar Jan 19 '24 20:01 DanL12186

Same issue. The API supports the parameter purpose="assistants" and accepts several formats.

UPLOAD FILE: https://platform.openai.com/docs/assistants/tools/passing-files-to-code-interpreter Accepted formats: https://platform.openai.com/docs/assistants/tools/supported-files

Here the library code: https://github.com/alexrudall/ruby-openai/blob/main/lib/openai/files.rb

leoplct avatar Mar 23 '24 10:03 leoplct

Having the same issue trying to send files. No matter the type of file I'm trying to send it tries to JSON parse it.

        file = entity.attachment.download
        temp_file = Tempfile.new(['email', '.html'])
        temp_file.write(file)
        temp_file.rewind

client.files.upload(parameters: { file: temp_file, purpose: "assistants" })

JSON::ParserError: unexpected token

thisismydesign avatar Jun 27 '24 13:06 thisismydesign

@thisismydesign Figure you've worked this out, but in case you're having issues, you can easily work around it just by using a built-in or gem-based HTTP library to make a call to the files endpoint

RestClient.get(
  "https://api.openai.com/v1/files/#{file_id}/content",
  {Authorization: "Bearer #{ACCESS_TOKEN}"}
)

DanL12186 avatar Jun 27 '24 14:06 DanL12186