Skip to content

Commit

Permalink
Add Azure support
Browse files Browse the repository at this point in the history
  • Loading branch information
rstropek committed Jul 25, 2024
1 parent 0f3e993 commit 93e55eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions labs/017-basics-nodejs/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OPENAI_API_KEY=...
AZURE_OPENAI_BASE_URL=...
AZURE_OPENAI_API_KEY=...
MODEL=gpt-4o
31 changes: 26 additions & 5 deletions labs/017-basics-nodejs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 93e55eb

Please sign in to comment.