Integrating Azure OpenAI with ServiceNow: 404 Error on API Request

Afshin2
Tera Expert

We're attempting to integrate Azure OpenAI's GPT-4.0-turbo model with our ServiceNow instance. Our goal is to test AI functionalities within our environment. We have obtained an API token and endpoint from Azure, and our Azure contact has whitelisted our instance's IP address.

Despite this setup, we're encountering a persistent issue. Our ServiceNow script, intended to make a POST request to the OpenAI API, consistently returns a 404 error, indicating that the resource cannot be found.

Here's the relevant part of our script: (endpoint and token are removed for security)

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());

The response we receive is: Status Code: 404 Response Body: {"error":{"code":"404","message": "Resource not found"}}

We're seeking guidance on what might be causing this issue and how to resolve it. Any insights or suggestions would be greatly appreciated.

1 ACCEPTED SOLUTION

Afshin2
Tera Expert

Here is the fix


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);

View solution in original post

6 REPLIES 6

Tony Chatfield1
Kilo Patron

Hi, the vendor doc's appear to show endpoints that include /openai/deployments/YOUR_DEPLOYMENT_NAME

Azure OpenAI Service REST API reference - Azure OpenAI | Microsoft Learn

Have you tried any of the endpoints shown in the doc's

Hi, yes we tried all of them

 

Are transactions successful if you use Postman or similar?
As resource is not found, it implies an issue with your endpoint.

Afshin
Tera Expert

I will post the solution with my original private account