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 <>
This commit is contained in:
Yves Maurer 2023-05-25 18:50:25 +02:00 committed by GitHub
parent 9c0cb90997
commit 88ed8e1cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 8 deletions

View File

@ -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"
}
}
},

View File

@ -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"
}
}
},

View File

@ -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. "

View File

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

View File

@ -91,6 +91,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
os.environ["OPENAI_API_BASE"] = "https://<your-endpoint.openai.azure.com/"
os.environ["OPENAI_API_KEY"] = "your AzureOpenAI key"
os.environ["OPENAI_API_VERSION"] = "2023-03-15-preview"
os.environ["OPENAI_PROXY"] = "http://your-corporate-proxy:8080"
from langchain.embeddings.openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
@ -112,6 +113,8 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
openai_api_base: Optional[str] = None
# to support Azure OpenAI Service custom endpoints
openai_api_type: Optional[str] = None
# to support explicit proxy for OpenAI
openai_proxy: Optional[str] = None
embedding_ctx_length: int = 8191
openai_api_key: Optional[str] = None
openai_organization: Optional[str] = None
@ -148,6 +151,12 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
"OPENAI_API_TYPE",
default="",
)
openai_proxy = get_from_dict_or_env(
values,
"openai_proxy",
"OPENAI_PROXY",
default="",
)
if openai_api_type in ("azure", "azure_ad", "azuread"):
default_api_version = "2022-12-01"
else:
@ -176,6 +185,8 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
openai.api_version = openai_api_version
if openai_api_type:
openai.api_type = openai_api_type
if openai_proxy:
openai.proxy = {"http": openai_proxy, "https": openai_proxy}
values["client"] = openai.Embedding
except ImportError:
raise ImportError(

View File

@ -147,6 +147,8 @@ class BaseOpenAI(BaseLLM):
openai_api_key: Optional[str] = None
openai_api_base: Optional[str] = None
openai_organization: Optional[str] = None
# to support explicit proxy for OpenAI
openai_proxy: Optional[str] = None
batch_size: int = 20
"""Batch size to use when passing multiple documents to generate."""
request_timeout: Optional[Union[float, Tuple[float, float]]] = None
@ -218,6 +220,12 @@ class BaseOpenAI(BaseLLM):
"OPENAI_API_BASE",
default="",
)
openai_proxy = get_from_dict_or_env(
values,
"openai_proxy",
"OPENAI_PROXY",
default="",
)
openai_organization = get_from_dict_or_env(
values,
"openai_organization",
@ -232,6 +240,8 @@ class BaseOpenAI(BaseLLM):
openai.api_base = openai_api_base
if openai_organization:
openai.organization = openai_organization
if openai_proxy:
openai.proxy = {"http": openai_proxy, "https": openai_proxy}
values["client"] = openai.Completion
except ImportError:
raise ImportError(
@ -627,6 +637,8 @@ class OpenAIChat(BaseLLM):
"""Holds any model parameters valid for `create` call not explicitly specified."""
openai_api_key: Optional[str] = None
openai_api_base: Optional[str] = None
# to support explicit proxy for OpenAI
openai_proxy: Optional[str] = None
max_retries: int = 6
"""Maximum number of retries to make when generating."""
prefix_messages: List = Field(default_factory=list)
@ -669,6 +681,12 @@ class OpenAIChat(BaseLLM):
"OPENAI_API_BASE",
default="",
)
openai_proxy = get_from_dict_or_env(
values,
"openai_proxy",
"OPENAI_PROXY",
default="",
)
openai_organization = get_from_dict_or_env(
values, "openai_organization", "OPENAI_ORGANIZATION", default=""
)
@ -680,6 +698,8 @@ class OpenAIChat(BaseLLM):
openai.api_base = openai_api_base
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. "