Skip to content

Custom backend URL support #547

Closed
Closed

Description

@mkaalto

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?

Additional context

The feature would probably be implemented at https://github.com/openai/openai-python/blob/v0.27.8/openai/api_resources/abstract/engine_api_resource.py#L22 (method `class_url´)

Activity

  1. mkaalto commented on Jul 24, 2023

    @mkaalto
    Author

    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

  2. ichernev commented on Aug 17, 2023

    @ichernev

    just use

    openai.api_base = "ALT_URL"
    

    Then, when you do ChatCompletion (for example) it queries ALT_URL/chat/completions, and passes the api key and everything.

  3. mkaalto commented on Aug 17, 2023

    @mkaalto
    Author

    just use

    openai.api_base = "ALT_URL"
    

    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.

  4. ichernev commented on Aug 18, 2023

    @ichernev

    @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 :)

  5. nowucca commented on Aug 23, 2023

    @nowucca

    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.

  6. added
    enhancement
    on Sep 6, 2023
  7. added
    fixed in v1
    on Nov 6, 2023
  8. RobertCraigie commented on Nov 6, 2023

    @RobertCraigie
    Contributor

    This is fixed in v1 which was just released!

    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:

    import httpx
    from openai import OpenAI
    
    def update_base_url(request: httpx.Request) -> None:
        # rewrite the path segment to what the proxy expects
        if request.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.

    completion = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {
                "role": "user",
                "content": "Say hi!",
            }
        ],
        extra_headers={'X-Foo': 'true'},
    )
  9. mkaalto commented on Nov 7, 2023

    @mkaalto
    Author

    Tested, this solves the problem. Thank you very much!

  10. qinwenshi commented on May 13, 2024

    @qinwenshi

    This is fixed in v1 which was just released!

    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:

    import httpx
    from openai import OpenAI
    
    def update_base_url(request: httpx.Request) -> None:
        # rewrite the path segment to what the proxy expects
        if request.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.

    completion = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {
                "role": "user",
                "content": "Say hi!",
            }
        ],
        extra_headers={'X-Foo': 'true'},
    )

    For people happened to use httpx.AsyncClient, the code should be like

    async def update_base_url(request: httpx.Request) -> httpx.Request:
        # rewrite the path segment to what the proxy expects
        if request.url.path == "/v1/chat/completions":
            request.url = request.url.copy_with(path="/custom/path/completion")
        return request

    Then we can use it like this

    httpx.AsyncClient(event_hooks={
                "request": [update_base_url],
            })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfixed in v1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions