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

Override base url and headers with gateway data

Open vincentklg opened this issue 2 years ago • 3 comments

So in my company we use an api gateway (Gravitee) to provide a central point for all apis (internal and external).

I read in the docs that the base url can be changed, but it looked to me that this is only intended for OpenAI on Azure. I also didn't find a way to easily override the headers of all requests. As we use our own api key internally and let the gateway transform the headers.

Question 1: How can we change the base url to our gateway? Question 2: How can we use different headers for all requests?

I have not that much experience with python, so very likely that I overlooked something. All help is greatly appreciated :)

vincentklg avatar Feb 02 '23 12:02 vincentklg

Hi @vincentklg,

I think I can help you out a bit. You should be able to overwrite the base url with openai.api_base = <your api base>.

I don't think there's a way to consistently overwrite the headers. What kind of overwriting did you want to do?

hallacy avatar Feb 06 '23 18:02 hallacy

Hi @vincentklg,

I think I can help you out a bit. You should be able to overwrite the base url with openai.api_base = <your api base>.

I don't think there's a way to consistently overwrite the headers. What kind of overwriting did you want to do?

I want to add a token parameter in the headers for authentication through the gateway

curiosity2 avatar Mar 21 '23 09:03 curiosity2

Found the solution:

response = openai.ChatCompletion.create(
    headers = {"Token":"Here is token"}, # Put the extra parameters here.
    model = "gpt-3.5-turbo",
    messages = [{"role": "user", "content": "Hello!"}]
)

curiosity2 avatar Mar 21 '23 10:03 curiosity2

Cool! Glad that seems to be working

hallacy avatar Mar 30 '23 04:03 hallacy