Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with initializing AzureChatOpenAI()

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?

like image 396
Dom R. Avatar asked Jul 22 '26 22:07

Dom R.


1 Answers

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]))  
like image 65
Venkatesan Avatar answered Jul 25 '26 03:07

Venkatesan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!