Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brandonbryant12/fcd346edd9ed3f6bf95a661d13b44420 to your computer and use it in GitHub Desktop.
Save brandonbryant12/fcd346edd9ed3f6bf95a661d13b44420 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch'); // Required for Node.js
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')
async function createChatCompletion(messages) {
const response = await fetch(
`${apiBase}/openai/chat/completions?api-version=${apiVersion}&api-type=${apiType}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': apiKey, // Standard Azure OpenAI API key header
'Authorization': `Bearer ${apiKey}`, // Bearer token using the same API key
},
body: JSON.stringify({
messages,
}),
}
);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content; // Assumes successful response
}
// 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