diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.py index 84f4294bf5b..ab494a2b7c4 100644 --- a/libs/partners/openai/langchain_openai/chat_models/azure.py +++ b/libs/partners/openai/langchain_openai/chat_models/azure.py @@ -185,12 +185,17 @@ class AzureChatOpenAI(ChatOpenAI): "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } - values["client"] = openai.AzureOpenAI(**client_params).chat.completions - values["async_client"] = openai.AsyncAzureOpenAI( - **client_params - ).chat.completions + if not values.get("client"): + sync_specific = {"http_client": values["http_client"]} + values["client"] = openai.AzureOpenAI( + **client_params, **sync_specific + ).chat.completions + if not values.get("async_client"): + async_specific = {"http_client": values["http_async_client"]} + values["async_client"] = openai.AsyncAzureOpenAI( + **client_params, **async_specific + ).chat.completions return values @property diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 46f0abc7e5f..b3409fadb72 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -313,7 +313,12 @@ class ChatOpenAI(BaseChatModel): # Configure a custom httpx client. See the # [httpx documentation](https://www.python-httpx.org/api/#client) for more details. http_client: Union[Any, None] = None - """Optional httpx.Client.""" + """Optional httpx.Client. Only used for sync invocations. Must specify + http_async_client as well if you'd like a custom client for async invocations. + """ + http_async_client: Union[Any, None] = None + """Optional httpx.AsyncClient. Only used for async invocations. Must specify + http_client as well if you'd like a custom client for sync invocations.""" class Config: """Configuration for this pydantic object.""" @@ -369,14 +374,17 @@ class ChatOpenAI(BaseChatModel): "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } if not values.get("client"): - values["client"] = openai.OpenAI(**client_params).chat.completions + sync_specific = {"http_client": values["http_client"]} + values["client"] = openai.OpenAI( + **client_params, **sync_specific + ).chat.completions if not values.get("async_client"): + async_specific = {"http_client": values["http_async_client"]} values["async_client"] = openai.AsyncOpenAI( - **client_params + **client_params, **async_specific ).chat.completions return values diff --git a/libs/partners/openai/langchain_openai/embeddings/azure.py b/libs/partners/openai/langchain_openai/embeddings/azure.py index 4afc0b4136c..684689d92ac 100644 --- a/libs/partners/openai/langchain_openai/embeddings/azure.py +++ b/libs/partners/openai/langchain_openai/embeddings/azure.py @@ -139,10 +139,17 @@ class AzureOpenAIEmbeddings(OpenAIEmbeddings): "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } - values["client"] = openai.AzureOpenAI(**client_params).embeddings - values["async_client"] = openai.AsyncAzureOpenAI(**client_params).embeddings + if not values.get("client"): + sync_specific = {"http_client": values["http_client"]} + values["client"] = openai.AzureOpenAI( + **client_params, **sync_specific + ).embeddings + if not values.get("async_client"): + async_specific = {"http_client": values["http_async_client"]} + values["async_client"] = openai.AsyncAzureOpenAI( + **client_params, **async_specific + ).embeddings return values @property diff --git a/libs/partners/openai/langchain_openai/embeddings/base.py b/libs/partners/openai/langchain_openai/embeddings/base.py index 746123ec0c1..b64388055bd 100644 --- a/libs/partners/openai/langchain_openai/embeddings/base.py +++ b/libs/partners/openai/langchain_openai/embeddings/base.py @@ -123,7 +123,12 @@ class OpenAIEmbeddings(BaseModel, Embeddings): retry_max_seconds: int = 20 """Max number of seconds to wait between retries""" http_client: Union[Any, None] = None - """Optional httpx.Client.""" + """Optional httpx.Client. Only used for sync invocations. Must specify + http_async_client as well if you'd like a custom client for async invocations. + """ + http_async_client: Union[Any, None] = None + """Optional httpx.AsyncClient. Only used for async invocations. Must specify + http_client as well if you'd like a custom client for sync invocations.""" class Config: """Configuration for this pydantic object.""" @@ -218,12 +223,17 @@ class OpenAIEmbeddings(BaseModel, Embeddings): "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } if not values.get("client"): - values["client"] = openai.OpenAI(**client_params).embeddings + sync_specific = {"http_client": values["http_client"]} + values["client"] = openai.OpenAI( + **client_params, **sync_specific + ).embeddings if not values.get("async_client"): - values["async_client"] = openai.AsyncOpenAI(**client_params).embeddings + async_specific = {"http_client": values["http_async_client"]} + values["async_client"] = openai.AsyncOpenAI( + **client_params, **async_specific + ).embeddings return values @property diff --git a/libs/partners/openai/langchain_openai/llms/azure.py b/libs/partners/openai/langchain_openai/llms/azure.py index f307d37d54b..d0571f98ba2 100644 --- a/libs/partners/openai/langchain_openai/llms/azure.py +++ b/libs/partners/openai/langchain_openai/llms/azure.py @@ -160,10 +160,17 @@ class AzureOpenAI(BaseOpenAI): "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } - values["client"] = openai.AzureOpenAI(**client_params).completions - values["async_client"] = openai.AsyncAzureOpenAI(**client_params).completions + if not values.get("client"): + sync_specific = {"http_client": values["http_client"]} + values["client"] = openai.AzureOpenAI( + **client_params, **sync_specific + ).completions + if not values.get("async_client"): + async_specific = {"http_client": values["http_async_client"]} + values["async_client"] = openai.AsyncAzureOpenAI( + **client_params, **async_specific + ).completions return values diff --git a/libs/partners/openai/langchain_openai/llms/base.py b/libs/partners/openai/langchain_openai/llms/base.py index 6c88f6eef42..59e5faf69c6 100644 --- a/libs/partners/openai/langchain_openai/llms/base.py +++ b/libs/partners/openai/langchain_openai/llms/base.py @@ -149,7 +149,12 @@ class BaseOpenAI(BaseLLM): # Configure a custom httpx client. See the # [httpx documentation](https://www.python-httpx.org/api/#client) for more details. http_client: Union[Any, None] = None - """Optional httpx.Client.""" + """Optional httpx.Client. Only used for sync invocations. Must specify + http_async_client as well if you'd like a custom client for async invocations. + """ + http_async_client: Union[Any, None] = None + """Optional httpx.AsyncClient. Only used for async invocations. Must specify + http_client as well if you'd like a custom client for sync invocations.""" class Config: """Configuration for this pydantic object.""" @@ -209,12 +214,17 @@ class BaseOpenAI(BaseLLM): "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } if not values.get("client"): - values["client"] = openai.OpenAI(**client_params).completions + sync_specific = {"http_client": values["http_client"]} + values["client"] = openai.OpenAI( + **client_params, **sync_specific + ).completions if not values.get("async_client"): - values["async_client"] = openai.AsyncOpenAI(**client_params).completions + async_specific = {"http_client": values["http_async_client"]} + values["async_client"] = openai.AsyncOpenAI( + **client_params, **async_specific + ).completions return values