vim-ai icon indicating copy to clipboard operation
vim-ai copied to clipboard

Allow setting request "User Agent"

Open pedrohgmacedo opened this issue 9 months ago • 3 comments

I configured GROQ as a custom endpoint, but then I was hitting 403 errors. Debugging, I figured that python's urllib's default user agent is blocked for some reason. Setting the User Agent to curl i.e. "curl/7.54.1" fixes the problem. Can you allow the user to configure the User Agent?

pedrohgmacedo avatar May 14 '24 04:05 pedrohgmacedo

That is quite strange they reject the default user agent, what is the reason they do that? Anyway, I think it would be fine to add an option to the commands (e.g. options.request_user_agent)

madox2 avatar May 16 '24 20:05 madox2

That is quite strange they reject the default user agent, what is the reason they do that? Anyway, I think it would be fine to add an option to the commands (e.g. options.request_user_agent)

Don't know the reason. I just monkey patched the request headers, since every other providers also accept curl, but an option would be fine.

pedrohgmacedo avatar May 16 '24 21:05 pedrohgmacedo

Here's what I did:

diff --git a/py/utils.py b/py/utils.py
index 963377d..5fd0f90 100644
--- a/py/utils.py
+++ b/py/utils.py
@@ -203,6 +203,7 @@ def openai_request(url, data, options):
     enable_auth=options['enable_auth']
     headers = {
         "Content-Type": "application/json",
+        "User-Agent": "curl/8.7.1",
     }
     if enable_auth:
         (OPENAI_API_KEY, OPENAI_ORG_ID) = load_api_key()

pedrohgmacedo avatar May 16 '24 22:05 pedrohgmacedo