-
Notifications
You must be signed in to change notification settings - Fork 20
Use Azure Open AI model in NodeJS OpenAI Sample #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c920fc1
Fix: Load dotenv in agent.ts before AgentApplication instantiates
rahuldevikar761 74a2b15
Add /api/health endpoint and always bind to 0.0.0.0
rahuldevikar761 aafeaa8
Fix: NODE_ENV=development takes priority over WEBSITE_SITE_NAME for a…
rahuldevikar761 2aacff7
feat: Add Azure OpenAI support to Node.js OpenAI sample
rahuldevikar761 393336b
fix host
rahuldevikar761 7bbee3a
fix: Address Copilot review comments
rahuldevikar761 7e5dcb3
fix: Resolve OpenAI type version mismatch
rahuldevikar761 cc3f10f
fix: Simplify environment detection logic
rahuldevikar761 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| /** | ||
| * OpenAI/Azure OpenAI Configuration | ||
| * | ||
| * This module configures the OpenAI SDK to work with either: | ||
| * - Standard OpenAI API (using OPENAI_API_KEY) | ||
| * - Azure OpenAI (using AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOYMENT) | ||
| * | ||
| * Azure OpenAI takes precedence if AZURE_OPENAI_API_KEY is set. | ||
| */ | ||
|
|
||
| // Note: We import AzureOpenAI from 'openai' which is a transitive dependency of @openai/agents | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const { AzureOpenAI } = require('openai'); | ||
| import { setDefaultOpenAIClient, setOpenAIAPI } from '@openai/agents'; | ||
|
|
||
| /** | ||
| * Determines if Azure OpenAI should be used based on environment variables. | ||
| * All three variables (API_KEY, ENDPOINT, DEPLOYMENT) must be set. | ||
| */ | ||
| export function isAzureOpenAI(): boolean { | ||
| return Boolean( | ||
| process.env.AZURE_OPENAI_API_KEY && | ||
| process.env.AZURE_OPENAI_ENDPOINT && | ||
| process.env.AZURE_OPENAI_DEPLOYMENT | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the model/deployment name to use. | ||
| * For Azure OpenAI, this is the deployment name (required). | ||
| * For standard OpenAI, this is the model name. | ||
| */ | ||
| export function getModelName(): string { | ||
| if (isAzureOpenAI()) { | ||
| const deployment = process.env.AZURE_OPENAI_DEPLOYMENT; | ||
| if (!deployment) { | ||
| throw new Error('AZURE_OPENAI_DEPLOYMENT is required when using Azure OpenAI'); | ||
| } | ||
| return deployment; | ||
| } | ||
| return process.env.OPENAI_MODEL || 'gpt-4o'; | ||
| } | ||
|
|
||
| /** | ||
| * Configures the OpenAI SDK with the appropriate client. | ||
| * Call this function early in your application startup. | ||
| */ | ||
| export function configureOpenAIClient(): void { | ||
| if (isAzureOpenAI()) { | ||
| console.log('[OpenAI Config] Using Azure OpenAI'); | ||
| console.log(`[OpenAI Config] Endpoint: ${process.env.AZURE_OPENAI_ENDPOINT}`); | ||
| console.log(`[OpenAI Config] Deployment: ${process.env.AZURE_OPENAI_DEPLOYMENT}`); | ||
|
|
||
| const azureClient = new AzureOpenAI({ | ||
| apiKey: process.env.AZURE_OPENAI_API_KEY, | ||
| endpoint: process.env.AZURE_OPENAI_ENDPOINT, | ||
| apiVersion: process.env.AZURE_OPENAI_API_VERSION || '2024-10-21', | ||
| deployment: process.env.AZURE_OPENAI_DEPLOYMENT, | ||
| }); | ||
|
|
||
| // Set the Azure client as the default for @openai/agents | ||
| // Using 'any' to bypass type version mismatch between openai package versions | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| setDefaultOpenAIClient(azureClient as any); | ||
|
|
||
rahuldevikar761 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Azure OpenAI requires Chat Completions API (not Responses API) | ||
| setOpenAIAPI('chat_completions'); | ||
| } else if (process.env.OPENAI_API_KEY) { | ||
| console.log('[OpenAI Config] Using standard OpenAI API'); | ||
| // Standard OpenAI uses OPENAI_API_KEY automatically | ||
| // No need to set client explicitly | ||
| } else { | ||
| console.warn('[OpenAI Config] WARNING: No OpenAI or Azure OpenAI credentials found!'); | ||
| console.warn('[OpenAI Config] Set OPENAI_API_KEY for standard OpenAI'); | ||
| console.warn('[OpenAI Config] Or set AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOYMENT for Azure OpenAI'); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.