Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brandonbryant12/1f28ea1ca526a5c264cbac5084d5007d to your computer and use it in GitHub Desktop.
Save brandonbryant12/1f28ea1ca526a5c264cbac5084d5007d to your computer and use it in GitHub Desktop.
const { OpenAI } = require('openai');
const apiKey = 'your-api-key'; // Replace with your Azure OpenAI API key (also used as bearer token)
const apiVersion = '2024-02-01'; // Valid as of March 2025
const apiBase = 'https://your-resource-name.openai.azure.com/'; // Replace with your Azure endpoint
const apiType = 'your-api-type'; // Replace with your specific apiType value (e.g., 'chat')
// Initialize the OpenAI client with Azure configuration
const openai = new OpenAI({
apiKey: apiKey, // Sets the api-key header
baseURL: `${apiBase}/openai`, // Base URL for Azure OpenAI
defaultQuery: { 'api-version': apiVersion, 'api-type': apiType }, // Add api-version and api-type as query params
defaultHeaders: {
'Authorization': `Bearer ${apiKey}`, // Add bearer token as requested
},
});
async function createChatCompletion(messages) {
try {
const completion = await openai.chat.completions.create({
messages: messages,
model: 'chat', // Placeholder; adjust if your apiType implies a specific model
});
return completion.choices[0].message.content;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// Example usage:
createChatCompletion([
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'How can I improve ankle mobility?' },
])
.then((result) => console.log('Response:', result))
.catch((error) => console.error('Error:', error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment