From 93e55ebac4b32586cff3774796436da3139c4083 Mon Sep 17 00:00:00 2001 From: Rainer Date: Thu, 25 Jul 2024 08:02:52 +0200 Subject: [PATCH] Add Azure support --- labs/017-basics-nodejs/.env.template | 4 ++++ labs/017-basics-nodejs/src/main.ts | 31 +++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 labs/017-basics-nodejs/.env.template diff --git a/labs/017-basics-nodejs/.env.template b/labs/017-basics-nodejs/.env.template new file mode 100644 index 0000000..f614b45 --- /dev/null +++ b/labs/017-basics-nodejs/.env.template @@ -0,0 +1,4 @@ +OPENAI_API_KEY=... +AZURE_OPENAI_BASE_URL=... +AZURE_OPENAI_API_KEY=... +MODEL=gpt-4o diff --git a/labs/017-basics-nodejs/src/main.ts b/labs/017-basics-nodejs/src/main.ts index 5dabc28..e289105 100644 --- a/labs/017-basics-nodejs/src/main.ts +++ b/labs/017-basics-nodejs/src/main.ts @@ -6,7 +6,21 @@ import { readLine } from './inputHelpers.js'; dotenv.config({ path: '.env' }); -const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); +let openAIType: string; +do { + openAIType = await readLine('Do you want to use OpenAI (1) or Azure OpenAI (2)? '); +} while (openAIType !== '1' && openAIType !== '2'); + +let openai: OpenAI; +if (openAIType === '1') { + openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); +} else { + openai = new OpenAI({ + baseURL: process.env.AZURE_OPENAI_BASE_URL, + defaultQuery: { 'api-version': '2024-05-01-preview' }, + defaultHeaders: { 'api-key': process.env.AZURE_OPENAI_API_KEY }, + }); +} const systemPrompt = await fs.promises.readFile('system-prompt.md', { encoding: 'utf-8', @@ -35,10 +49,17 @@ while (true) { }); // get AI response - const response = await openai.chat.completions.create({ - messages, - model: 'gpt-4o-mini', - }); + const options: OpenAI.RequestOptions = {}; + if (openAIType === '2') { + options.path = `openai/deployments/${process.env.MODEL}/chat/completions`; + } + const response = await openai.chat.completions.create( + { + messages, + model: process.env.MODEL!, + }, + options + ); // add AI response to messages messages.push({