You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the feature or improvement you're requesting
Currently, the URL for an OpenAI completion (openai.Completion.create) is predefined to support 'azure' and 'openai' backends.
Would it be possible to have a custom api_type (openai.api_type = "custom") where the URL would then be possible to define while creating the OpenAI completion engine?
Additionally, there should be a possibility to set extra headers, and omit the standard authorization header, if not possible already (I understand this may be doable already)
Also found an issue #541, which is probably related
Then, when you do ChatCompletion (for example) it queries ALT_URL/chat/completions, and passes the api key and everything.
Unfortunately, that does not work since the backend URL does not end with '/chat/completions', but rather uses different URI. Also the authorization is handled through separate header, and the openai - API type Authorization header (sent by default) results in an authentication failure.
@mkaalto Ok, that makes some sense. Still if a service is providing almost OpenAI compatible API, they might as well make it fully compatible. Almost compatible is still not compatible :)
I'm teaching a class, and I've writtena reverse proxy. I would like students to be forced to log in with a custom header to the proxy. Other than that extra header, the proxy is fully compatible.
So there's a use case of why we might need to customize headers in the client call.
You can specify a custom base URL by passing the base_url argument to the constructor, e.g. OpenAI(base_url='...').
If you need to use custom paths then you can write your own event hook to do so, for example:
importhttpxfromopenaiimportOpenAIdefupdate_base_url(request: httpx.Request) ->None:
# rewrite the path segment to what the proxy expectsifrequest.url.path=="/v1/chat/completions":
request.url=request.url.copy_with(path="/my/custom/path")
client=OpenAI(
base_url="https://www.my.base.url",
http_client=httpx.Client(
event_hooks={
"request": [update_base_url],
}
),
)
completion=client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say hi!",
}
],
)
@nowucca your request is also supported in v1, you can pass in custom default headers to the client constructor, e.g. OpenAI(default_headers={'X-Foo': 'true'}) or on a per-method basis with the extra_headers argument.
You can specify a custom base URL by passing the base_url argument to the constructor, e.g. OpenAI(base_url='...').
If you need to use custom paths then you can write your own event hook to do so, for example:
importhttpxfromopenaiimportOpenAIdefupdate_base_url(request: httpx.Request) ->None:
# rewrite the path segment to what the proxy expectsifrequest.url.path=="/v1/chat/completions":
request.url=request.url.copy_with(path="/my/custom/path")
client=OpenAI(
base_url="https://www.my.base.url",
http_client=httpx.Client(
event_hooks={
"request": [update_base_url],
}
),
)
completion=client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say hi!",
}
],
)
@nowucca your request is also supported in v1, you can pass in custom default headers to the client constructor, e.g. OpenAI(default_headers={'X-Foo': 'true'}) or on a per-method basis with the extra_headers argument.
For people happened to use httpx.AsyncClient, the code should be like
asyncdefupdate_base_url(request: httpx.Request) ->httpx.Request:
# rewrite the path segment to what the proxy expectsifrequest.url.path=="/v1/chat/completions":
request.url=request.url.copy_with(path="/custom/path/completion")
returnrequest
Activity
mkaalto commentedon Jul 24, 2023
Additionally, there should be a possibility to set extra headers, and omit the standard authorization header, if not possible already (I understand this may be doable already)
Also found an issue #541, which is probably related
ichernev commentedon Aug 17, 2023
just use
Then, when you do
ChatCompletion(for example) it queriesALT_URL/chat/completions, and passes the api key and everything.mkaalto commentedon Aug 17, 2023
Unfortunately, that does not work since the backend URL does not end with '/chat/completions', but rather uses different URI. Also the authorization is handled through separate header, and the openai - API type Authorization header (sent by default) results in an authentication failure.
ichernev commentedon Aug 18, 2023
@mkaalto Ok, that makes some sense. Still if a service is providing almost OpenAI compatible API, they might as well make it fully compatible. Almost compatible is still not compatible :)
nowucca commentedon Aug 23, 2023
I'm teaching a class, and I've writtena reverse proxy. I would like students to be forced to log in with a custom header to the proxy. Other than that extra header, the proxy is fully compatible.
So there's a use case of why we might need to customize headers in the client call.
RobertCraigie commentedon Nov 6, 2023
This is fixed in v1 which was just released!
You can specify a custom base URL by passing the
base_urlargument to the constructor, e.g.OpenAI(base_url='...').If you need to use custom paths then you can write your own event hook to do so, for example:
@nowucca your request is also supported in v1, you can pass in custom default headers to the client constructor, e.g.
OpenAI(default_headers={'X-Foo': 'true'})or on a per-method basis with theextra_headersargument.mkaalto commentedon Nov 7, 2023
Tested, this solves the problem. Thank you very much!
qinwenshi commentedon May 13, 2024
For people happened to use httpx.AsyncClient, the code should be like
Then we can use it like this
feat(client): support reading the base url from an env variable (#547)