|
| 1 | +// ------------------------------------------------------------------------- |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License. |
| 4 | +// ------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import { FoundryLocalManager, getOutputText } from '../src/index.js'; |
| 7 | +import type { StreamingEvent, FunctionToolDefinition, FunctionCallItem } from '../src/types.js'; |
| 8 | + |
| 9 | +async function main() { |
| 10 | + try { |
| 11 | + // Initialize the Foundry Local SDK |
| 12 | + console.log('Initializing Foundry Local SDK...'); |
| 13 | + const manager = FoundryLocalManager.create({ |
| 14 | + appName: 'ResponsesExample', |
| 15 | + logLevel: 'info' |
| 16 | + }); |
| 17 | + console.log('✓ SDK initialized'); |
| 18 | + |
| 19 | + // Load a model |
| 20 | + const modelAlias = 'MODEL_ALIAS'; // Replace with a valid model alias |
| 21 | + const catalog = manager.catalog; |
| 22 | + const model = await catalog.getModel(modelAlias); |
| 23 | + await model.load(); |
| 24 | + console.log(`✓ Model ${model.id} loaded`); |
| 25 | + |
| 26 | + // Start the web service (required for Responses API) |
| 27 | + manager.startWebService(); |
| 28 | + console.log(`✓ Web service running at ${manager.urls[0]}`); |
| 29 | + |
| 30 | + // Create a ResponsesClient |
| 31 | + const client = manager.createResponsesClient(model.id); |
| 32 | + client.settings.temperature = 0.7; |
| 33 | + client.settings.maxOutputTokens = 500; |
| 34 | + |
| 35 | + // ================================================================= |
| 36 | + // Example 1: Basic text response |
| 37 | + // ================================================================= |
| 38 | + console.log('\n--- Example 1: Basic text response ---'); |
| 39 | + const response = await client.create('What is the capital of France?'); |
| 40 | + |
| 41 | + console.log(`Status: ${response.status}`); |
| 42 | + console.log(`Response: ${getOutputText(response)}`); |
| 43 | + |
| 44 | + // ================================================================= |
| 45 | + // Example 2: Streaming response |
| 46 | + // ================================================================= |
| 47 | + console.log('\n--- Example 2: Streaming response ---'); |
| 48 | + process.stdout.write('Response: '); |
| 49 | + await client.createStreaming( |
| 50 | + 'Write a short haiku about code.', |
| 51 | + (event: StreamingEvent) => { |
| 52 | + if (event.type === 'response.output_text.delta') { |
| 53 | + process.stdout.write(event.delta); |
| 54 | + } |
| 55 | + } |
| 56 | + ); |
| 57 | + console.log('\n'); |
| 58 | + |
| 59 | + // ================================================================= |
| 60 | + // Example 3: Multi-turn with previous_response_id |
| 61 | + // ================================================================= |
| 62 | + console.log('--- Example 3: Multi-turn conversation ---'); |
| 63 | + client.settings.store = true; |
| 64 | + |
| 65 | + const turn1 = await client.create('My name is Alice. Remember it.'); |
| 66 | + console.log(`Turn 1 (ID: ${turn1.id}): done`); |
| 67 | + |
| 68 | + const turn2 = await client.create('What is my name?', { |
| 69 | + previous_response_id: turn1.id, |
| 70 | + }); |
| 71 | + console.log(`Turn 2: ${getOutputText(turn2)}`); |
| 72 | + |
| 73 | + // ================================================================= |
| 74 | + // Example 4: Tool calling |
| 75 | + // ================================================================= |
| 76 | + console.log('\n--- Example 4: Tool calling ---'); |
| 77 | + const tools: FunctionToolDefinition[] = [{ |
| 78 | + type: 'function', |
| 79 | + name: 'get_weather', |
| 80 | + description: 'Get the current weather for a location.', |
| 81 | + parameters: { |
| 82 | + type: 'object', |
| 83 | + properties: { |
| 84 | + location: { type: 'string', description: 'City name' }, |
| 85 | + }, |
| 86 | + required: ['location'], |
| 87 | + }, |
| 88 | + }]; |
| 89 | + |
| 90 | + const toolResponse = await client.create( |
| 91 | + 'What is the weather in Seattle?', |
| 92 | + { tools, tool_choice: 'required' } |
| 93 | + ); |
| 94 | + |
| 95 | + // Find the function call in the output |
| 96 | + const funcCall = toolResponse.output.find( |
| 97 | + (o): o is FunctionCallItem => o.type === 'function_call' |
| 98 | + ); |
| 99 | + |
| 100 | + if (funcCall) { |
| 101 | + console.log(`Tool call: ${funcCall.name}(${funcCall.arguments})`); |
| 102 | + |
| 103 | + // Simulate providing the tool result and continuing |
| 104 | + const finalResponse = await client.create([ |
| 105 | + { type: 'function_call_output', call_id: funcCall.call_id, output: '72°F, sunny' }, |
| 106 | + ], { previous_response_id: toolResponse.id, tools }); |
| 107 | + |
| 108 | + console.log(`Final: ${getOutputText(finalResponse)}`); |
| 109 | + } |
| 110 | + |
| 111 | + // ================================================================= |
| 112 | + // Example 5: Get & delete stored response |
| 113 | + // ================================================================= |
| 114 | + console.log('\n--- Example 5: Get & delete stored response ---'); |
| 115 | + const stored = await client.create('Hello!'); |
| 116 | + console.log(`Created: ${stored.id}`); |
| 117 | + |
| 118 | + const retrieved = await client.get(stored.id); |
| 119 | + console.log(`Retrieved: ${retrieved.id}, status: ${retrieved.status}`); |
| 120 | + |
| 121 | + const deleted = await client.delete(stored.id); |
| 122 | + console.log(`Deleted: ${deleted.deleted}`); |
| 123 | + |
| 124 | + // Cleanup |
| 125 | + manager.stopWebService(); |
| 126 | + await model.unload(); |
| 127 | + console.log('\n✓ Example completed successfully'); |
| 128 | + |
| 129 | + } catch (error) { |
| 130 | + console.error('Error:', error instanceof Error ? error.message : error); |
| 131 | + process.exit(1); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +main(); |
0 commit comments