我是 ChainLang 和 Azure 的新手,想知道为什么在运行以下代码时遇到错误(摘自 https://python.langchain.com/docs/tutorials/llm_chain/#using-language-models) Jupyter 笔记本,谢谢。
Jupyter 笔记本中的代码:
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"\],
)
运行后出现错误:
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/'
我已检查我的 Azure 门户,端点正是我从中复制的内容。不明白为什么会出现关键错误。
错误显示
KeyError
,所以让我们修复密钥。删除那些景观\
,它应该可以工作
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"],
)