openai[patch]: fix ChatOpenAI model's openai proxy (#19559)

Due to changes in the OpenAI SDK, the previous method of setting the
OpenAI proxy in ChatOpenAI no longer works. This PR fixes this issue,
making the previous way of setting the OpenAI proxy in ChatOpenAI
effective again.

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
Shuqian
2024-03-28 14:16:55 +08:00
committed by GitHub
parent b15c7fdde6
commit 332996b4b2
2 changed files with 41 additions and 0 deletions

View File

@@ -380,12 +380,31 @@ class ChatOpenAI(BaseChatModel):
"default_query": values["default_query"],
}
openai_proxy = values["openai_proxy"]
if not values.get("client"):
if openai_proxy and not values["http_client"]:
try:
import httpx
except ImportError as e:
raise ImportError(
"Could not import httpx python package. "
"Please install it with `pip install httpx`."
) from e
values["http_client"] = httpx.Client(proxy=openai_proxy)
sync_specific = {"http_client": values["http_client"]}
values["client"] = openai.OpenAI(
**client_params, **sync_specific
).chat.completions
if not values.get("async_client"):
if openai_proxy and not values["http_async_client"]:
try:
import httpx
except ImportError as e:
raise ImportError(
"Could not import httpx python package. "
"Please install it with `pip install httpx`."
) from e
values["http_async_client"] = httpx.AsyncClient(proxy=openai_proxy)
async_specific = {"http_client": values["http_async_client"]}
values["async_client"] = openai.AsyncOpenAI(
**client_params, **async_specific