Azure OpenAI与ServiceNow集成遇API请求404错误求助
问题排查:ServiceNow调用Azure OpenAI GPT-4.0-turbo返回404错误
核心原因分析
出现404"Resource not found"错误,主要是因为Azure OpenAI API需要完整的端点路径,当前脚本仅使用了基础资源地址,缺少部署名称、API版本等关键路径参数,同时可能存在模型名称与Azure部署名称不匹配的问题。
具体解决步骤
1. 补全Azure OpenAI完整API端点
Azure OpenAI的聊天补全API必须包含部署名称和API版本参数,正确的URL格式为:
https://{your-resource-name}.openai.azure.com/openai/deployments/{your-deployment-name}/chat/completions?api-version={valid-api-version}
{your-resource-name}:Azure OpenAI资源的名称{your-deployment-name}:你在Azure门户中创建的GPT-4.0-turbo部署的名称(注意:不是官方模型名){valid-api-version}:Azure支持的有效API版本,例如2024-02-15-preview、2023-12-01-preview
2. 修正请求体中的model字段值
请求体中的model必须与Azure OpenAI部署的名称完全一致,而非官方模型名gpt-4.0-turbo。可在Azure门户的OpenAI资源→「部署」页面中查看部署名称。
3. 验证请求头与参数正确性
- 确保
Authorization头格式正确:Bearer {your-api-key},API密钥需为Azure OpenAI资源的有效密钥(可在资源→「密钥和端点」页面获取) - 确认
Content-Type头设置为application/json,脚本中已配置此头,但需检查是否存在拼写错误
4. 测试端点可访问性
先通过Postman或curl测试完整端点是否正常响应,排除ServiceNow环境外的问题:
curl https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-name}/chat/completions?api-version=2024-02-15-preview \ -H "Content-Type: application/json" \ -H "Authorization: Bearer {your-api-key}" \ -d '{ "model": "{your-deployment-name}", "messages": [{"role": "user", "content": "Test message"}] }'
修改后的ServiceNow脚本示例
var openaiApiUrl = 'https://**********.openai.azure.com/openai/deployments/your-gpt4-turbo-deployment/chat/completions?api-version=2024-02-15-preview'; // 替换为完整端点 var openaiApiKey = 'b060f***************7d7e570e'; // 准备请求体 var requestBody = { "model": "your-gpt4-turbo-deployment", // 替换为Azure部署名称 "messages": [ { "role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." }, { "role": "user", "content": "Compose a poem that explains the concept of recursion in programming." } ] }; var requestBodyString = JSON.stringify(requestBody); // 创建并配置HTTP请求 var httpRequest = new GlideHTTPRequest(openaiApiUrl); httpRequest.addHeader('Content-Type', 'application/json'); httpRequest.addHeader('Authorization', 'Bearer ' + openaiApiKey); // 发送请求并处理响应 var response = httpRequest.post(requestBodyString); // 调试日志 gs.info("Status Code: " + response.getStatusCode()); gs.info("Response Body: " + response.getBody());
额外排查点
- 确认ServiceNow实例的出口IP已正确加入Azure OpenAI的IP白名单(部分环境可能存在多个出口IP,需全部添加)
- 检查Azure OpenAI资源是否处于正常运行状态,未被暂停或限制
- 验证API密钥是否未过期,且拥有该资源的调用权限
内容的提问来源于stack exchange,提问作者iCode18
相关产品推荐
相关产品推荐

