From 88ed8e1cd6c7f1b679efe9e80cf6f8c33e3e6217 Mon Sep 17 00:00:00 2001 From: Yves Maurer <14054229+ymaurer@users.noreply.github.com> Date: Thu, 25 May 2023 18:50:25 +0200 Subject: [PATCH] Added the option of specifying a proxy for the OpenAI API (#5246) # Added the option of specifying a proxy for the OpenAI API Fixes #5243 Co-authored-by: Yves Maurer <> --- .../models/llms/integrations/openai.ipynb | 17 ++++++++++++---- .../text_embedding/examples/openai.ipynb | 11 ++++++---- langchain/chat_models/azure_openai.py | 10 ++++++++++ langchain/chat_models/openai.py | 10 ++++++++++ langchain/embeddings/openai.py | 11 ++++++++++ langchain/llms/openai.py | 20 +++++++++++++++++++ 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/docs/modules/models/llms/integrations/openai.ipynb b/docs/modules/models/llms/integrations/openai.ipynb index 6feb11dc..87ea797b 100644 --- a/docs/modules/models/llms/integrations/openai.ipynb +++ b/docs/modules/models/llms/integrations/openai.ipynb @@ -21,7 +21,7 @@ }, "outputs": [ { - "name": "stdin", + "name": "stdout", "output_type": "stream", "text": [ " ········\n" @@ -127,11 +127,20 @@ "\n", "llm_chain.run(question)" ] + }, + { + "cell_type": "markdown", + "id": "58a9ddb1", + "metadata": {}, + "source": [ + "# if you are behind an explicit proxy, you can use the OPENAI_PROXY environment variable to pass through\n", + "os.environ[\"OPENAI_PROXY\"] = \"http://proxy.yourcompany.com:8080\"" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.11.1 64-bit", "language": "python", "name": "python3" }, @@ -145,11 +154,11 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.11.1" }, "vscode": { "interpreter": { - "hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03" + "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1" } } }, diff --git a/docs/modules/models/text_embedding/examples/openai.ipynb b/docs/modules/models/text_embedding/examples/openai.ipynb index 7d038342..9cb9c625 100644 --- a/docs/modules/models/text_embedding/examples/openai.ipynb +++ b/docs/modules/models/text_embedding/examples/openai.ipynb @@ -124,12 +124,15 @@ "id": "aaad49f8", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# if you are behind an explicit proxy, you can use the OPENAI_PROXY environment variable to pass through\n", + "os.environ[\"OPENAI_PROXY\"] = \"http://proxy.yourcompany.com:8080\"" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.11.1 64-bit", "language": "python", "name": "python3" }, @@ -143,11 +146,11 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.11.1" }, "vscode": { "interpreter": { - "hash": "7377c2ccc78bc62c2683122d48c8cd1fb85a53850a1b1fc29736ed39852c9885" + "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1" } } }, diff --git a/langchain/chat_models/azure_openai.py b/langchain/chat_models/azure_openai.py index 93d8e5b4..6a76d8e2 100644 --- a/langchain/chat_models/azure_openai.py +++ b/langchain/chat_models/azure_openai.py @@ -24,6 +24,7 @@ class AzureChatOpenAI(ChatOpenAI): - ``OPENAI_API_KEY`` - ``OPENAI_API_BASE`` - ``OPENAI_API_VERSION`` + - ``OPENAI_PROXY`` For exmaple, if you have `gpt-35-turbo` deployed, with the deployment name `35-turbo-dev`, the constructor should look like: @@ -46,6 +47,7 @@ class AzureChatOpenAI(ChatOpenAI): openai_api_version: str = "" openai_api_key: str = "" openai_organization: str = "" + openai_proxy: str = "" @root_validator() def validate_environment(cls, values: Dict) -> Dict: @@ -76,6 +78,12 @@ class AzureChatOpenAI(ChatOpenAI): "OPENAI_ORGANIZATION", default="", ) + openai_proxy = get_from_dict_or_env( + values, + "openai_proxy", + "OPENAI_PROXY", + default="", + ) try: import openai @@ -85,6 +93,8 @@ class AzureChatOpenAI(ChatOpenAI): openai.api_key = openai_api_key if openai_organization: openai.organization = openai_organization + if openai_proxy: + openai.proxy = {"http": openai_proxy, "https": openai_proxy} except ImportError: raise ImportError( "Could not import openai python package. " diff --git a/langchain/chat_models/openai.py b/langchain/chat_models/openai.py index dad92c60..d5cf669f 100644 --- a/langchain/chat_models/openai.py +++ b/langchain/chat_models/openai.py @@ -148,6 +148,8 @@ class ChatOpenAI(BaseChatModel): leave blank if not using a proxy or service emulator.""" openai_api_base: Optional[str] = None openai_organization: Optional[str] = None + # to support explicit proxy for OpenAI + openai_proxy: Optional[str] = None request_timeout: Optional[Union[float, Tuple[float, float]]] = None """Timeout for requests to OpenAI completion API. Default is 600 seconds.""" max_retries: int = 6 @@ -209,6 +211,12 @@ class ChatOpenAI(BaseChatModel): "OPENAI_API_BASE", default="", ) + openai_proxy = get_from_dict_or_env( + values, + "openai_proxy", + "OPENAI_PROXY", + default="", + ) try: import openai @@ -222,6 +230,8 @@ class ChatOpenAI(BaseChatModel): openai.organization = openai_organization if openai_api_base: openai.api_base = openai_api_base + if openai_proxy: + openai.proxy = {"http": openai_proxy, "https": openai_proxy} try: values["client"] = openai.ChatCompletion except AttributeError: diff --git a/langchain/embeddings/openai.py b/langchain/embeddings/openai.py index f0e2215e..bed27101 100644 --- a/langchain/embeddings/openai.py +++ b/langchain/embeddings/openai.py @@ -91,6 +91,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings): os.environ["OPENAI_API_BASE"] = "https://