emacs-openai / openai

Elisp library for the OpenAI API
GNU General Public License v3.0
100 stars 17 forks source link

Adding a file to the request does not work #6

Open CeleritasCelery opened 1 year ago

CeleritasCelery commented 1 year ago

openai-image-variation-prompt does not currently work. it is just passing the filename as a parameter instead of attaching the image itself. I tried to fix this with the code below:

(openai-request "https://api.openai.com/v1/images/variations"
    :type "POST"
    :headers `(("Content-Type"  . "application/json")
               ("Authorization" . ,(concat "Bearer " openai-key)))
    :data (json-encode
           `(("n"               . ,openai-image-n)
             ("size"            . ,openai-image-size)
             ("response_format" . ,openai-image-response-format)
             ("user"            . ,openai-user)))
    :files `(("image" . ,image))
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (funcall callback data))))

However this returns an error from openai

Invalid Content-Type header (application/json), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data')

So it looks like it doesn't like mixing form data with json encoded data (and you can't json encode images). I tried to switch to using only form encoding using this:

(openai-request "https://api.openai.com/v1/images/variations"
  :type "POST"
  :headers `(("Authorization" . ,(concat "Bearer " openai-key)))
  :data `(("n"               . "1")
          ("size"            . ,openai-image-size)
          ("response_format" . ,openai-image-response-format)
          ("user"            . ,openai-user))
  :files `(("image" . ,image))
  :parser 'json-read
  :success (cl-function
            (lambda (&key data &allow-other-keys)
              (funcall callback data))))

But that got a different error from openai

400 - Additional properties are not allowed ('--form image' was unexpected)

I am not very familiar with HTTP so I am stuck on how to get this working. Any help would be appreciated.

jcs090218 commented 1 year ago

Yeah, I am going to rewrite the library using the better approach, see #5. Sorry for this isn't being 100% ready. 😓 I feel sorry since I created package for my own use, but I will soon make improvements next week! 😅

CeleritasCelery commented 1 year ago

I didn't realize this was still "beta". Amazing work! I was really impressed and I thought I would have to implement something from scratch.

jcs090218 commented 1 year ago

Thanks! It's not great yet, but I will continue to make improvements! I'm busy for the following two weeks, I will try to work on it the next weekend! :D

CeleritasCelery commented 1 year ago

Are you open to PR's or are you planning on doing a complete rewrite?

jcs090218 commented 1 year ago

I've made quick adjustments (#7) so now all request parameters are all exposed! I was hoping to expose response with class or structure, but unfortunately, I don't have the knowledge to do that now. 😅 I wasn't comfortable asking people to make PR before a rewrite, but now that I've made the adjustment, feel free to open PRs! :D

jcs090218 commented 1 year ago

Hi, I've recently tried the request but I'm encountering the same issue! After some time of google searching, it seems like there are still some issues with the image requests. I will mark this as won't fix for now since there is no way we can fix it on our side!

CeleritasCelery commented 1 year ago

What do you mean by "there are still some issues with the image requests"? Are you referring to a limitation in the request.el library? If so I don't see an issue for this in their tracker. However the image API works fine over curl, so it is not a limitation on the OpenAI side. For example this will work:

curl  https://api.openai.com/v1/images/variations \
     -H 'Authorization: Bearer <your key>' \
     -F "image=@</path/to/image.png>" \
     -F n=1 \
     -F size="256x256"