我可以访问 azureopenai,但最近我们转向 APIM 以拥有个人访问密钥。 以前我用这个脚本很好:
from openai import AzureOpenAI
AZURE_OPENAI_API_KEY = 'KEY'
azure_endpoint = "ENDPOINT"
deployment_name = "DEPLOYMENT_NAME"
client = AzureOpenAI(
api_key = AZURE_OPENAI_API_KEY,
api_version = "Version",
azure_endpoint =azure_endpoint,
)
def get_response_enrich():
completion = client.chat.completions.create(
model="datascience-nonprod",
messages=[
{"role": "system", "content": "name a country in europe"},
{"role": "user", "content": """ you're a helpful assistant"""}
],
temperature=0,
return completion.choices[0].message.content
但知道我们的网址看起来像
apim_url = azure_endpoint = "https://xy-apim-XXX.azure-api.net/"
并且相同的客户端调用返回:
“openai.NotFoundError:错误代码:404 - {‘statusCode’:404,‘消息’:‘找不到资源’}” 卷曲或请求调用工作正常
import requests
import json
# Set the parameters
apim_url = "https://xy-apim-XXX.azure-api.net/"
deployment_name = "DEPLOYMENT_NAME"
api_version = "Version"
subscription_key = ""
# Construct the URL and headers
url = f"{apim_url}/deployments/{deployment_name}/chat/completions?api-version={api_version}"
headers = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": subscription_key
}
知道这里出了什么问题吗?
谢谢。
需要将“default_headers”添加到客户端:
default_headers=
{
"Ocp-Apim-Subscription-Key": "YOUR_KEY"
}