Created
March 20, 2025 18:56
-
-
Save brandonbryant12/53e3627974b5a0c259de69dae131ed8b to your computer and use it in GitHub Desktop.
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
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 deploymentName = 'your-deployment-name'; // Replace with your deployment name | |
async function createChatCompletion(messages) { | |
const response = await fetch( | |
`${apiBase}/openai/deployments/${deploymentName}/chat/completions?api-version=${apiVersion}`, | |
{ | |
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