7

I try to call AzureChatOpenAI() from langchain. Normally I would do:

model = AzureChatOpenAI(
openai_api_base=os.getenv("OPENAI_API_BASE"),
openai_api_version="2023-03-15-preview",
deployment_name=os.getenv("GPT_DEPLOYMENT_NAME"),
openai_api_key=os.getenv("OPENAI_API_KEY"),
openai_api_type="azure",

)

But I get the warnings

python3.9/site-packages/langchain/chat_models/azure_openai.py:155: UserWarning: As of openai>=1.0.0, Azure endpoints should be specified via the `azure_endpoint` param not `openai_api_base` (or alias `base_url`). Updating `openai_api_base` from https://xxxx.openai.azure.com/ to https://xxxx.openai.azure.com/openai.
  warnings.warn(

python3.9/site-packages/langchain/chat_models/azure_openai.py:162: UserWarning: As of openai>=1.0.0, if `deployment_name` (or alias `azure_deployment`) is specified then `openai_api_base` (or alias `base_url`) should not be. Instead use `deployment_name` (or alias `azure_deployment`) and `azure_endpoint`.
  warnings.warn(

python3.9/site-packages/langchain/chat_models/azure_openai.py:170: UserWarning: As of openai>=1.0.0, if `openai_api_base` (or alias `base_url`) is specified it is expected to be of the form https://example-resource.azure.openai.com/openai/deployments/example-deployment. Updating https://xxxx.openai.azure.com/ to https://xxxx.openai.azure.com/openai.
  warnings.warn(

But if I follow the instructions and change it to:

model = AzureChatOpenAI(
azure_endpoint=os.getenv("OPENAI_API_BASE"),
openai_api_version="2023-03-15-preview",
azure_deployment=os.getenv("GPT_DEPLOYMENT_NAME"),
openai_api_key=os.getenv("OPENAI_API_KEY"),
openai_api_type="azure",

)

I get the error

ValidationError: 1 validation error for AzureChatOpenAI
__root__
  base_url and azure_endpoint are mutually exclusive (type=value_error)

What am I doing wrong?

2
  • 1
    Please check what value you have set in the environment for OPENAI_API_BASE. You need to use AZURE_OPENAI_ENDPOINT, instead of OPENAI_API_BASE. The value for AZURE_OPENAI_ENDPOINT can be fetched from your azure subscription (portal); Under Resource=>Keys and Endpoints=>Endpoint. Set this value in your environment. The error base_url and azure_endpoint are mutually exclusive indicates that you are trying to set value for base_url via OPEN_API_BASE in azure_endpoint. Commented Nov 27, 2023 at 6:17
  • Did you get this resolved? Commented Nov 27, 2024 at 19:19

2 Answers 2

9

In my environment, I used package versions openai=0.27.0 and langchain=0.0.341.

I tried with the below code to call AzureChatOpenAI() from langchain using Python SDK.

Code:

from langchain.chat_models import AzureChatOpenAI
from langchain.schema import HumanMessage

OPENAI_API_BASE="<Your azure openai endpoint>"
GPT_DEPLOYMENT_NAME="<Your deployment name>"
OPENAI_API_KEY="<Your api key>"

model = AzureChatOpenAI(
openai_api_base=OPENAI_API_BASE,
openai_api_version="2023-07-01-preview",
azure_deployment=GPT_DEPLOYMENT_NAME,
openai_api_key=OPENAI_API_KEY,
openai_api_type="azure",
)

message = HumanMessage(
    content="Translate this sentence from English to Spanish.MS Dhoni as the greatest finisher in the history of the sport"
)    
print(model([message]))   

Output:

content='MS Dhoni como el mejor finalizador en la historia del deporte.'

enter image description here

Reference:

Azure OpenAI | 🦜️🔗 Langchain

Update:

I got a similar error when I had OPENAI_* and AZURE_* environment variables in openai version greater than 1.0.0.

You can refer to this GitHub page about this issue.

So try using AZURE_OPENAI_ENDPOINT in your environment.

Code:

from langchain.chat_models import AzureChatOpenAI
from langchain.schema import HumanMessage
import os

GPT_DEPLOYMENT_NAME="deploymentname1"

os.environ["AZURE_OPENAI_API_KEY"] = "2xxxxx1"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://xxxx.openai.azure.com/"


model = AzureChatOpenAI(
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    openai_api_version="2023-03-15-preview",
    azure_deployment=GPT_DEPLOYMENT_NAME,
)

message = HumanMessage(
    content="Translate this sentence from English to Spanish.MS Dhoni as the greatest finisher in the history of the sport"
)    
print(model([message]))  
Sign up to request clarification or add additional context in comments.

1 Comment

OP's issue is reproducible with openai 1.2.3 version
3

For langchain_openai==0.0.5 this setup seems to be working:

llm = AzureChatOpenAI( 
    openai_api_key=os.getenv("KEY"),
    azure_endpoint=os.getenv("ENDPOINT"),
    openai_api_version=os.getenv("API_VERSION"),
    deployment_name=os.getenv("MODEL_NAME"),
)

I intentionally didn't use the suggested environment variables from docs to be sure that only explicitly passed parameters are used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.