Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a file to the request does not work #6

Open
CeleritasCelery opened this issue Mar 15, 2023 · 7 comments
Open

Adding a file to the request does not work #6

CeleritasCelery opened this issue Mar 15, 2023 · 7 comments

Comments

@CeleritasCelery
Copy link

CeleritasCelery commented Mar 15, 2023

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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

@jcs090218
Copy link
Member

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
Copy link
Member

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
Copy link
Author

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants