Skip to content

Commit

Permalink
feat(client): support reading the base url from an env variable (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Nov 15, 2023
1 parent 71d3172 commit 0733934
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ import httpx
from openai import OpenAI

client = OpenAI(
# Or use the `OPENAI_BASE_URL` env var
base_url="http://my.test.server.example.com:8083",
http_client=httpx.Client(
proxies="http://my.test.proxy.example.com",
Expand Down
4 changes: 4 additions & 0 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(
organization = os.environ.get("OPENAI_ORG_ID")
self.organization = organization

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
base_url = f"https://api.openai.com/v1"

Expand Down Expand Up @@ -307,6 +309,8 @@ def __init__(
organization = os.environ.get("OPENAI_ORG_ID")
self.organization = organization

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
base_url = f"https://api.openai.com/v1"

Expand Down
12 changes: 12 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
make_request_options,
)

from .utils import update_env

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
api_key = "My API Key"

Expand Down Expand Up @@ -399,6 +401,11 @@ class Model2(BaseModel):
assert isinstance(response, Model1)
assert response.foo == 1

def test_base_url_env(self) -> None:
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
client = OpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

@pytest.mark.parametrize(
"client",
[
Expand Down Expand Up @@ -932,6 +939,11 @@ class Model2(BaseModel):
assert isinstance(response, Model1)
assert response.foo == 1

def test_base_url_env(self) -> None:
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

@pytest.mark.parametrize(
"client",
[
Expand Down
17 changes: 16 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import os
import traceback
from typing import Any, TypeVar, cast
import contextlib
from typing import Any, TypeVar, Iterator, cast
from datetime import date, datetime
from typing_extensions import Literal, get_args, get_origin, assert_type

Expand Down Expand Up @@ -103,3 +105,16 @@ def _assert_list_type(type_: type[object], value: object) -> None:
inner_type = get_args(type_)[0]
for entry in value:
assert_type(inner_type, entry) # type: ignore


@contextlib.contextmanager
def update_env(**new_env: str) -> Iterator[None]:
old = os.environ.copy()

try:
os.environ.update(new_env)

yield None
finally:
os.environ.clear()
os.environ.update(old)

0 comments on commit 0733934

Please sign in to comment.