Last active
December 19, 2025 17:29
-
-
Save pedrommone/bce0b72b6b526bdcd75c7e8e4d3ab120 to your computer and use it in GitHub Desktop.
poc azure openai
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
| cd /Users/pedromaia/Code/app-devops/poc/azure-foundry-gpt-4.1-mini && npm test | |
| > azure-foundry-gpt-4.1-mini-poc@1.0.0 test | |
| > node test-azure-foundry.js | |
| ============================================================ | |
| Azure Foundry gpt-4.1-mini POC - Responses API | |
| ============================================================ | |
| π Testing Azure Foundry gpt-4.1-mini deployment with Responses API... | |
| Configuration: | |
| Endpoint: https://eastus.api.cognitive.microsoft.com | |
| Deployment: gpt-4.1-mini | |
| API Version: 2025-04-01-preview | |
| API URL: https://eastus.api.cognitive.microsoft.com/openai/responses?api-version=2025-04-01-preview | |
| β Success! Response received: | |
| Response: { | |
| "id": "resp_0fe63f687ef7b9e70069458b3f2cd481968373ecee5d843f36", | |
| "object": "response", | |
| "created_at": 1766165311, | |
| "status": "completed", | |
| "background": false, | |
| "content_filters": null, | |
| "error": null, | |
| "incomplete_details": null, | |
| "instructions": null, | |
| "max_output_tokens": 150, | |
| "max_tool_calls": null, | |
| "model": "gpt-4.1-mini", | |
| "output": [ | |
| { | |
| "id": "msg_0fe63f687ef7b9e70069458b3f7c18819689975800fe6f36e0", | |
| "type": "message", | |
| "status": "completed", | |
| "content": [ | |
| { | |
| "type": "output_text", | |
| "annotations": [], | |
| "logprobs": [], | |
| "text": "Sure! Hereβs a short joke for you:\n\nWhy donβt scientists trust atoms? \nBecause they make up everything!" | |
| } | |
| ], | |
| "role": "assistant" | |
| } | |
| ], | |
| "parallel_tool_calls": true, | |
| "previous_response_id": null, | |
| "prompt_cache_key": null, | |
| "prompt_cache_retention": null, | |
| "reasoning": { | |
| "effort": null, | |
| "summary": null | |
| }, | |
| "safety_identifier": null, | |
| "service_tier": "default", | |
| "store": true, | |
| "temperature": 0.7, | |
| "text": { | |
| "format": { | |
| "type": "text" | |
| }, | |
| "verbosity": "medium" | |
| }, | |
| "tool_choice": "auto", | |
| "tools": [], | |
| "top_logprobs": 0, | |
| "top_p": 1, | |
| "truncation": "disabled", | |
| "usage": { | |
| "input_tokens": 27, | |
| "input_tokens_details": { | |
| "cached_tokens": 0 | |
| }, | |
| "output_tokens": 25, | |
| "output_tokens_details": { | |
| "reasoning_tokens": 0 | |
| }, | |
| "total_tokens": 52 | |
| }, | |
| "user": null, | |
| "metadata": {} | |
| } | |
| π Message: Sure! Hereβs a short joke for you: | |
| Why donβt scientists trust atoms? | |
| Because they make up everything! | |
| π Usage: { | |
| "input_tokens": 27, | |
| "input_tokens_details": { | |
| "cached_tokens": 0 | |
| }, | |
| "output_tokens": 25, | |
| "output_tokens_details": { | |
| "reasoning_tokens": 0 | |
| }, | |
| "total_tokens": 52 | |
| } | |
| ============================================================ | |
| β All tests completed successfully! | |
| ============================================================ | |
| $ |
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
| #!/usr/bin/env node | |
| /** | |
| * POC: Test Azure Foundry gpt-4.1-mini Deployment | |
| * | |
| * This script demonstrates how to use the responses API endpoint format | |
| * to make requests to the Azure Foundry deployment. | |
| * | |
| * Usage: | |
| * 1. Copy .env.example to .env | |
| * 2. Update .env with your Azure Foundry credentials | |
| * 3. Run: npm install | |
| * 4. Run: npm test | |
| */ | |
| import dotenv from 'dotenv'; | |
| // Load environment variables | |
| dotenv.config(); | |
| // Configuration | |
| const AZURE_ENDPOINT = process.env.AZURE_ENDPOINT || 'https://eastus.api.cognitive.microsoft.com'; | |
| const AZURE_API_KEY = process.env.AZURE_API_KEY; | |
| const AZURE_DEPLOYMENT = process.env.AZURE_DEPLOYMENT || 'gpt-4.1-mini'; | |
| const AZURE_API_VERSION = process.env.AZURE_API_VERSION || '2025-04-01-preview'; | |
| // Validate required environment variables | |
| if (!AZURE_API_KEY) { | |
| console.error('β Error: AZURE_API_KEY is required'); | |
| console.error(' Please set AZURE_API_KEY in your .env file'); | |
| process.exit(1); | |
| } | |
| // Responses API endpoint URL | |
| const RESPONSES_API_URL = `${AZURE_ENDPOINT}/openai/responses?api-version=${AZURE_API_VERSION}`; | |
| /** | |
| * Test chat completion using responses API | |
| */ | |
| async function testChatCompletion() { | |
| console.log('π Testing Azure Foundry gpt-4.1-mini deployment with Responses API...\n'); | |
| console.log('Configuration:'); | |
| console.log(` Endpoint: ${AZURE_ENDPOINT}`); | |
| console.log(` Deployment: ${AZURE_DEPLOYMENT}`); | |
| console.log(` API Version: ${AZURE_API_VERSION}`); | |
| console.log(` API URL: ${RESPONSES_API_URL}\n`); | |
| try { | |
| // Responses API uses 'input' instead of 'messages' | |
| // Supported parameters: temperature, top_p, max_output_tokens, etc. | |
| const requestBody = { | |
| model: AZURE_DEPLOYMENT, | |
| input: [ | |
| { | |
| role: 'system', | |
| content: 'You are a helpful assistant.', | |
| }, | |
| { | |
| role: 'user', | |
| content: 'Hello! Can you tell me a short joke?', | |
| }, | |
| ], | |
| temperature: 0.7, | |
| max_output_tokens: 150, | |
| }; | |
| const response = await fetch(RESPONSES_API_URL, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'api-key': AZURE_API_KEY, | |
| }, | |
| body: JSON.stringify(requestBody), | |
| }); | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(`HTTP ${response.status}: ${errorText}`); | |
| } | |
| const data = await response.json(); | |
| console.log('β Success! Response received:\n'); | |
| console.log('Response:', JSON.stringify(data, null, 2)); | |
| // Extract message from responses API format | |
| // Responses API returns: output[0].content[0].text | |
| let message = 'No content'; | |
| if (data.output?.[0]?.content?.[0]?.text) { | |
| message = data.output[0].content[0].text; | |
| } else if (data.output?.[0]?.content) { | |
| message = data.output[0].content; | |
| } else if (data.choices?.[0]?.message?.content) { | |
| message = data.choices[0].message.content; | |
| } | |
| console.log('\nπ Message:', message); | |
| if (data.usage) { | |
| console.log('\nπ Usage:', JSON.stringify(data.usage, null, 2)); | |
| } | |
| return data; | |
| } catch (error) { | |
| console.error('β Error making request:', error.message); | |
| throw error; | |
| } | |
| } | |
| /** | |
| * Test streaming chat completion using responses API | |
| */ | |
| async function testStreamingChatCompletion() { | |
| console.log('\nπ Testing streaming chat completion with Responses API...\n'); | |
| try { | |
| const requestBody = { | |
| model: AZURE_DEPLOYMENT, | |
| input: [ | |
| { | |
| role: 'user', | |
| content: 'Write a haiku about artificial intelligence.', | |
| }, | |
| ], | |
| temperature: 0.7, | |
| stream: true, | |
| max_output_tokens: 200, | |
| }; | |
| const response = await fetch(RESPONSES_API_URL, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'api-key': AZURE_API_KEY, | |
| }, | |
| body: JSON.stringify(requestBody), | |
| }); | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(`HTTP ${response.status}: ${errorText}`); | |
| } | |
| console.log('π‘ Streaming response:\n'); | |
| let fullContent = ''; | |
| const reader = response.body.getReader(); | |
| const decoder = new TextDecoder(); | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| const chunk = decoder.decode(value); | |
| const lines = chunk.split('\n').filter(line => line.trim()); | |
| for (const line of lines) { | |
| if (line.startsWith('data: ')) { | |
| const data = line.slice(6); | |
| if (data === '[DONE]') continue; | |
| try { | |
| const parsed = JSON.parse(data); | |
| const content = parsed.output?.[0]?.content || parsed.choices?.[0]?.delta?.content || ''; | |
| if (content) { | |
| process.stdout.write(content); | |
| fullContent += content; | |
| } | |
| } catch (e) { | |
| // Skip invalid JSON | |
| } | |
| } | |
| } | |
| } | |
| console.log('\n\nβ Streaming complete!'); | |
| console.log(`Total length: ${fullContent.length} characters`); | |
| return fullContent; | |
| } catch (error) { | |
| console.error('β Error in streaming request:', error.message); | |
| throw error; | |
| } | |
| } | |
| /** | |
| * Test with custom prompt using responses API | |
| */ | |
| async function testCustomPrompt(prompt) { | |
| console.log(`\n㪠Testing custom prompt: "${prompt}"\n`); | |
| try { | |
| const requestBody = { | |
| model: AZURE_DEPLOYMENT, | |
| input: [ | |
| { | |
| role: 'user', | |
| content: prompt, | |
| }, | |
| ], | |
| temperature: 0.7, | |
| max_output_tokens: 500, | |
| }; | |
| const response = await fetch(RESPONSES_API_URL, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'api-key': AZURE_API_KEY, | |
| }, | |
| body: JSON.stringify(requestBody), | |
| }); | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(`HTTP ${response.status}: ${errorText}`); | |
| } | |
| const data = await response.json(); | |
| console.log('β Response received:\n'); | |
| let message = 'No content'; | |
| if (data.output?.[0]?.content?.[0]?.text) { | |
| message = data.output[0].content[0].text; | |
| } else if (data.output?.[0]?.content) { | |
| message = data.output[0].content; | |
| } else if (data.choices?.[0]?.message?.content) { | |
| message = data.choices[0].message.content; | |
| } | |
| console.log(message); | |
| if (data.usage) { | |
| console.log('\nπ Usage:', JSON.stringify(data.usage, null, 2)); | |
| } | |
| return data; | |
| } catch (error) { | |
| console.error('β Error:', error.message); | |
| throw error; | |
| } | |
| } | |
| // Main execution | |
| async function main() { | |
| console.log('='.repeat(60)); | |
| console.log('Azure Foundry gpt-4.1-mini POC - Responses API'); | |
| console.log('='.repeat(60)); | |
| console.log(); | |
| try { | |
| // Test 1: Basic chat completion | |
| await testChatCompletion(); | |
| // Test 2: Streaming (optional - uncomment to test) | |
| // await testStreamingChatCompletion(); | |
| // Test 3: Custom prompt (optional - uncomment to test) | |
| // await testCustomPrompt('Explain quantum computing in simple terms.'); | |
| console.log('\n' + '='.repeat(60)); | |
| console.log('β All tests completed successfully!'); | |
| console.log('='.repeat(60)); | |
| } catch (error) { | |
| console.error('\n' + '='.repeat(60)); | |
| console.error('β Tests failed'); | |
| console.error('='.repeat(60)); | |
| process.exit(1); | |
| } | |
| } | |
| // Run if executed directly | |
| if (import.meta.url === `file://${process.argv[1]}`) { | |
| main(); | |
| } | |
| export { testChatCompletion, testStreamingChatCompletion, testCustomPrompt }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment