neg-0 / gina

0 stars 0 forks source link

AzureOpenAI library doesn't work when authenticating with azureADTokenProvider #1

Open neg-0 opened 1 day ago

neg-0 commented 1 day ago

GINA backend service can authenticate with 2 methods: token or API key. Token is the more secure and preferred method.

Token:

const {
  DefaultAzureCredential,
  getBearerTokenProvider,
} = require("@azure/identity");

const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);

// Get environment variables
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME;
const azureOpenAIVersion = process.env.OPENAI_API_VERSION;
const assistantId = process.env.AZURE_OPENAI_ASSISTANT_ID;

const options = {
  azureADTokenProvider,
  deployment: azureOpenAIDeployment,
  apiVersion: azureOpenAIVersion,
  endpoint: azureOpenAIEndpoint,
};

const client = new AzureOpenAI(options);

The app successfully authenticates with Azure services BUT cannot use the OpenAI API services.

this.assistantResponse = await client.beta.assistants.retrieve(assistantId);

This line returns the error code

AuthenticationError: 401 status code (no body)
    at APIError.generate (/home/neg-0/src/gina/backend/node_modules/openai/error.js:48:20)
    at AzureOpenAI.makeStatusError (/home/neg-0/src/gina/backend/node_modules/openai/core.js:285:33)
    at AzureOpenAI.makeRequest (/home/neg-0/src/gina/backend/node_modules/openai/core.js:329:30)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Array.<anonymous> (/home/neg-0/src/gina/backend/src/bots/azureOpenAiConnector.js:48:38) {
  status: 401,
  headers: {
    'apim-request-id': 'adf4bb7a-3c54-47f5-82eb-0082c8d6882f',
    'content-length': '171',
    'content-type': 'application/json',
    date: 'Wed, 25 Sep 2024 03:44:25 GMT',
    'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
    'x-content-type-options': 'nosniff',
    'x-ms-client-request-id': 'Not-Set'
  },
  request_id: undefined,
  error: undefined,
  code: undefined,
  param: undefined,
  type: undefined
}