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

Implement message files

Open ignacio-chiazzo opened this issue 5 months ago • 1 comments

This API allows returning the files of a given message like the following example:

  files = messages.files(thread_id: thread_id, id: message_id)

Full example

You can also try a complete test by adding ACCESS_TOKEN and FILE_PATH and running the following script:

ACCESS_TOKEN = "YOUR-ACCESS-TOKEN"
FILE_PATH = "YOUR-FILE-PATH"

client = OpenAI::Client.new(access_token: ACCESS_TOKEN)
messages = OpenAI::Messages.new(client: client)

# submit a file
files = OpenAI::Files.new(client: client)
uploaded_file = files.upload(parameters: {file: FILE_PATH , purpose: "assistants"})

# create a thread
threads = OpenAI::Threads.new(client: client)
tread = threads.create()
thread_id = tread.dig("id")
new_thread = threads.retrieve(id: thread_id)

# create a message
messages = OpenAI::Messages.new(client: client)
message = messages.create(thread_id: thread_id, parameters: {
  "role" => "user",
  "content"=> "What's the content of this file?",
  "file_ids" => [uploaded_file.dig("id")]
})
message_id =  message["id"]
message_retrieved = messages.retrieve(thread_id: thread_id, id: message_id)

# NEW! ******** Return the message files  ******** 
message_file = messages.files(thread_id: thread_id, id: message_id)
# =>
# {"object"=>"list",
#  "data"=>
#   [{"id"=>"file-1234", "object"=>"thread.message.file", "created_at"=>1705882474, "message_id"=>"msg_gm5678"}],
#  "first_id"=>"file-1234",
#  "last_id"=>"file-1234",
#  "has_more"=>false}

message_file = messages.file(thread_id: thread_id, id: message_id, file_id: message_file.dig("id"))

The official documentation is here

  • [x] Have you followed the guidelines in our Contributing document?
  • [x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • [x] Have you added an explanation of what your changes do and why you'd like us to include them?

ignacio-chiazzo avatar Jan 22 '24 00:01 ignacio-chiazzo

@alexrudall can I get a review?

ignacio-chiazzo avatar Feb 03 '24 20:02 ignacio-chiazzo

Thanks for your work on this @ignacio-chiazzo - closing as OpenAI have deprecated MessageFiles for v2 of the beta

alexrudall avatar Apr 27 '24 21:04 alexrudall