partners[azure]: fix having openai_api_base set for other packages (#22068)

This fix is for #21726. When having other packages installed that
require the `openai_api_base` environment variable, users are not able
to instantiate the AzureChatModels or AzureEmbeddings.

This PR adds a new value `ignore_openai_api_base` which is a bool. When
set to True, it sets `openai_api_base` to `None`

Two new tests were added for the `test_azure` and a new file
`test_azure_embeddings`

A different approach may be better for this. If you can think of better
logic, let me know and I can adjust it.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Chip Davis
2024-07-01 13:35:20 -05:00
committed by GitHub
parent b36e95caa9
commit 04bc5f1a95
4 changed files with 70 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
"""Test Azure OpenAI Chat API wrapper."""
import os
from langchain_openai import AzureChatOpenAI
@@ -32,3 +34,25 @@ def test_initialize_more() -> None:
ls_params = llm._get_ls_params()
assert ls_params["ls_provider"] == "azure"
assert ls_params["ls_model_name"] == "35-turbo-dev"
def test_initialize_azure_openai_with_openai_api_base_set() -> None:
os.environ["OPENAI_API_BASE"] = "https://api.openai.com"
llm = AzureChatOpenAI(
api_key="xyz",
azure_endpoint="my-base-url",
azure_deployment="35-turbo-dev",
openai_api_version="2023-05-15",
temperature=0,
openai_api_base=None,
)
assert llm.openai_api_key is not None
assert llm.openai_api_key.get_secret_value() == "xyz"
assert llm.azure_endpoint == "my-base-url"
assert llm.deployment_name == "35-turbo-dev"
assert llm.openai_api_version == "2023-05-15"
assert llm.temperature == 0
ls_params = llm._get_ls_params()
assert ls_params["ls_provider"] == "azure"
assert ls_params["ls_model_name"] == "35-turbo-dev"