Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite openai.api_version. #4234

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,8 @@
"\n",
"os.environ[\"OPENAI_API_TYPE\"] = \"azure\"\n",
"os.environ[\"OPENAI_API_BASE\"] = \"https://<your-endpoint.openai.azure.com/\"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"your AzureOpenAI key\""
"os.environ[\"OPENAI_API_KEY\"] = \"your AzureOpenAI key\"\n",
"os.environ[\"OPENAI_API_VERSION\"] = \"2023-03-15-preview\""
]
},
{
Expand Down
20 changes: 12 additions & 8 deletions langchain/embeddings/openai.py
Expand Up @@ -77,8 +77,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
openai = OpenAIEmbeddings(openai_api_key="my-api-key")

In order to use the library with Microsoft Azure endpoints, you need to set
the OPENAI_API_TYPE, OPENAI_API_BASE, OPENAI_API_KEY and optionally and
API_VERSION.
the OPENAI_API_TYPE, OPENAI_API_BASE, OPENAI_API_KEY and OPENAI_API_VERSION.
The OPENAI_API_TYPE must be set to 'azure' and the others correspond to
the properties of your endpoint.
In addition, the deployment name must be passed as the model parameter.
Expand All @@ -90,6 +89,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
os.environ["OPENAI_API_TYPE"] = "azure"
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"

from langchain.embeddings.openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
Expand All @@ -106,7 +106,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
client: Any #: :meta private:
model: str = "text-embedding-ada-002"
deployment: str = model # to support Azure OpenAI Service custom deployment names
openai_api_version: str = "2022-12-01"
openai_api_version: str = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pranjaldoshi96 I made the default value "" according to other azure model implementations: https://github.com/zijie0/langchain/blob/96f4467b3c26eceae28cedd6a5180adbcda6cd9e/langchain/chat_models/azure_openai.py#L45

By making the default "", we are forcing user to define this variable either in env or in named parameter.

You remind me that this parameter should be optional with default OpenAI embeddings. So I also added openai_api_type check in validation.

# to support Azure OpenAI Service custom endpoints
openai_api_base: Optional[str] = None
# to support Azure OpenAI Service custom endpoints
Expand Down Expand Up @@ -144,11 +144,14 @@ def validate_environment(cls, values: Dict) -> Dict:
"OPENAI_API_TYPE",
default="",
)
openai_api_version = get_from_dict_or_env(
values,
"openai_api_version",
"OPENAI_API_VERSION",
)
if openai_api_type in ("azure", "azure_ad", "azuread"):
openai_api_version = get_from_dict_or_env(
values,
"openai_api_version",
"OPENAI_API_VERSION",
)
else:
openai_api_version = None
openai_organization = get_from_dict_or_env(
values,
"openai_organization",
Expand All @@ -163,6 +166,7 @@ def validate_environment(cls, values: Dict) -> Dict:
openai.organization = openai_organization
if openai_api_base:
openai.api_base = openai_api_base
if openai_api_version:
openai.api_version = openai_api_version
if openai_api_type:
openai.api_type = openai_api_type
Expand Down