Problem with initializing AzureChatOpenAI()
Asked Answered
C

2

6

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?

Cheyney answered 25/11, 2023 at 17:10 Comment(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.Phelps
A
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]))  
Alternant answered 28/11, 2023 at 7:20 Comment(1)
OP's issue is reproducible with openai 1.2.3 versionPhotomultiplier
P
2

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.

Peggypegma answered 8/2 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.