skip to Main Content

I encountered error when I run below codes (extracted from https://python.langchain.com/docs/tutorials/llm_chain/#using-language-models) in Jupyter Notebook:

import getpass
import os

os.environ["AZURE_OPENAI_API_KEY"] = "d957dadfasdfafdsfsafasfasfsafasf99f4b7"

from langchain_openai import AzureChatOpenAI

model = AzureChatOpenAI(
azure_endpoint=os.environ["https://testing123.cognitiveservices.azure.com/"],
azure_deployment=os.environ["testing123"],
openai_api_version=os.environ["2021-04-30"],
)

Error I got after run:

KeyError                                  Traceback (most recent call last)
Cell In[16], line 10
5 os.environ["AZURE_OPENAI_API_KEY"] = "d957dadfasdfafdsfsafasfasfsafasf99f4b7"
7 from langchain_openai import AzureChatOpenAI
9 model = AzureChatOpenAI(
---> 10     azure_endpoint=os.environ["https://testing123.cognitiveservices.azure.com/"],
11     azure_deployment=os.environ["testing123"],
12     openai_api_version=os.environ["2021-04-30"],
13 )

File <frozen os>:714, in __getitem__(self, key)

KeyError: 'https://testing123.cognitiveservices.azure.com/'

I have checked my Azure portal and end-point is exactly what I copied from. Don’t understand why key error existed.

2

Answers


  1. The errors says KeyError so let’s fix the key. Remove those scape and it should work.

    Also looks like you are not accessing the environment vars below so remove the os.environ and pass the string directly.

    import getpass
    import os
    
    os.environ["AZURE_OPENAI_API_KEY"] = "d957dadfasdfafdsfsafasfasfsafasf99f4b7"
    
    from langchain_openai import AzureChatOpenAI
    
    model = AzureChatOpenAI(
      azure_endpoint="https://testing123.cognitiveservices.azure.com/",
      azure_deployment="testing123",
      openai_api_version="2021-04-30",
    )
    
    Login or Signup to reply.
  2. The issue you’re facing comes from trying to use os.environ incorrectly. The os.environ dictionary is designed to access environment variables, which are stored as key-value pairs. However, it seems you’re passing an actual URL and deployment name, treating them as if they were environment variable keys, which they are not.

    If you’re passing your Azure endpoint and deployment information directly, you should use them as plain strings rather than trying to fetch them from os.environ.

    from langchain_openai import AzureChatOpenAI
    
    
    model = AzureChatOpenAI(
        azure_endpoint="https://testing123.cognitiveservices.azure.com/",  
        azure_deployment="testing123",  
        openai_api_version="2021-04-30"  
    )
    

    If you prefer to store the values as environment variables, you need to set them first and then use them in your code.

    First, set the environment variables:

    import os
    
    os.environ["AZURE_OPENAI_API_KEY"] = "your_api_key_here"
    os.environ["AZURE_OPENAI_ENDPOINT"] = "https://testing123.cognitiveservices.azure.com/"
    os.environ["AZURE_OPENAI_DEPLOYMENT"] = "testing123"
    os.environ["AZURE_OPENAI_API_VERSION"] = "2021-04-30"
    

    Then, access these variables in your code:

    from langchain_openai import AzureChatOpenAI
    
    
    model = AzureChatOpenAI(
        azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
        azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT"],
        openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"]
    )
    

    When using os.environ, make sure the keys refer to actual environment variable names that you’ve set, not the endpoint URL or deployment name directly unless you’ve saved them there. If you’re not using environment variables, you can simply pass the values as strings when initializing the model.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search