#Uploading Documents through the OpenAI API (Ruby on rails)

1 messages · Page 1 of 1 (latest)

scarlet arch
#

Hello there, I wanted to upload documents (PDF,docx,xml...) for some context and be able to create assistants that could retrieve some context and reply as a chatbot in Ruby on Rails.

I found a similar post here #1207906187803107369 (Python)
But wanted some help with ror.

Currently using
I'm directly calling the APIs using HTTP request handlers. But there are a few issues with the file handling where the file gets uploaded and I get a file-id. But when I try to use the same with an assistant I just get a response saying

Thank you for uploading the file. Currently, the file is not accessible 
with the `myfiles_browser` tool, which I would typically use to review the content. However, you can either upload the file again ensuring it is in a supported format such as PDF, DOCX, or TXT,

A test-bed code for the file upload

uri = "https://api.openai.com/v1/files"
headers = {
  'Authorization' => "Bearer #{ENV['OPENAI_API_KEY']}",
  'Content-Type' => 'multipart/form-data'
}

file_path = 'tmp/testing_file.pdf'
file_content = File.open(file_path, "rb")


# puts "#{file_content}"

data = {
  'purpose' => 'assistants',
  'file' => file_content
}

response = HTTParty.post(uri, headers: headers, body: data)
puts "#{response}"

This is a smaller part of the entire workflow.

model-used: gpt-4-1106-preview
necessity: Building a chatbot

fringe rain
#

@scarlet archDid you ever find a solution? Hitting the same issue myself

scarlet arch
#

using ruby on rails?

#
file_data = params[:uploaded_file]

temp_file = file_data.tempfile.path

file_content = File.open(temp_file, "rb")

response = HTTParty.post(uri,
  headers: {
    'Authorization' => "Bearer #{ENV['OPENAI_API_KEY']}"
  },
  body: {
    purpose: 'assistants',
    file: file_content
  },
  multipart: true
)
file_id = response['id'] 

temp_file is the one which I get from the view. And in ror its stored as a multipart tempfile

#

There is some intermediate code to do some file checks, but this is the main part that worked for me.
Let me know if there are any flaws in this.

fringe rain
scarlet arch
meager wave
#

after you upload the file, then attach the file to the assistant, I've seen it need a couple minutes to process the file before the assistant can see it.

#

I imagine its doing some level of fine tuning to be able to process the file in the future.

fringe rain
fringe rain
#

@meager wave Update: Even with a 2 minute delay, it still had an issue. Any idea what the right amount of delay is?

fringe rain
meager wave
#

so thats close to nothing. less than a mb

#

i've attached 20 files to an assistant of various sizes, and querying the assistant for them a few minutes later.

#

I do always use txt files

#

you are first uploading the files to get a fileid, then attaching the fileid to the assistant, correct?

soft moon
#

Reliable way to get files on assistants for me has been either through knowledge update or asking the assistant to provide a downloadable answer in a format suitable for my purposes.

#

This through the API

meager wave
#

I've asked the assistant how many files it has access to, and to create summaries of each file (for each specific fileid) before running the chain of though script for our use case

soft moon
#

I don't know if this is also helpful, but hae you tried the rube-openai git found in the documented community libraries?

fringe rain
meager wave