将 Azure OpenAI 与 ServiceNow 集成:API 请求出现 404 错误

问题描述 投票:0回答:1

我们正在尝试将 Azure OpenAI 的 GPT-4.0-turbo 模型与我们的 ServiceNow 实例集成。我们的目标是在我们的环境中测试人工智能功能。我们已从 Azure 获取了 API 令牌和端点,并且我们的 Azure 联系人已将我们实例的 IP 地址列入白名单。

尽管有这样的设置,我们还是遇到了一个持续存在的问题。我们的 ServiceNow 脚本旨在向 OpenAI API 发出 POST 请求,但始终返回 404 错误,表明找不到资源。

这是我们脚本的相关部分:(为了安全起见,删除了端点和令牌)

var openaiApiUrl = 'https://**********.openai.azure.com/'; // Replace with your OpenAI endpoint URL
var openaiApiKey = 'b060f***************7d7e570e'; // Replace with your OpenAI API key

// Prepare the request body
var requestBody = {
    "model": "gpt-4.0-turbo", // Specify the model
    "messages": [ // Add your messages here
        {
            "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);

// Create and configure the HTTP request
var httpRequest = new GlideHTTPRequest(openaiApiUrl);
httpRequest.addHeader('Content-Type', 'application/json');
httpRequest.addHeader('Authorization', 'Bearer ' + openaiApiKey);

// Send the request and handle the response
var response = httpRequest.post(requestBodyString);

// Log the response for debugging
gs.info("Status Code: " + response.getStatusCode());
gs.info("Response Body: " + response.getBody());

我们收到的回复是: 状态代码:404 响应正文:{“error”:{“code”:“404”,“message”:“找不到资源”}}

我们正在寻求有关可能导致此问题的原因以及如何解决该问题的指导。任何见解或建议将不胜感激。

azure servicenow azure-openai
1个回答
0
投票

我已经这样修复了:

var resourceName = '****-open-ai-it-sn'; // *** is for security reason 
var deploymentName = '****-Open-AI-IT-SN-gpt4'; // *** is for security reason 
var apiKey = '50ebb8*******bbbfc495b7ecd'; 


var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod('post');
restMessage.setEndpoint('https://' + resourceName + '.openai.azure.com/openai/deployments/' + deploymentName + '/chat/completions?api-version=2023-07-01-preview');
restMessage.setRequestHeader('Content-Type', 'application/json');
restMessage.setRequestHeader('api-key', apiKey);


var requestBody = {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is Examination in norwegian?' },

]
};
restMessage.setRequestBody(JSON.stringify(requestBody));


var response = restMessage.execute();
var responseBody = response.getBody();
gs.info(responseBody);
© www.soinside.com 2019 - 2024. All rights reserved.