|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import "./bin/Microsoft.SemanticKernel.Core.js"; |
| 5 | +import "./bin/Microsoft.SemanticKernel.Connectors.AI.OpenAI.js"; |
| 6 | +import dotnet from "node-api-dotnet"; |
| 7 | +import readline from "node:readline"; |
| 8 | +import { colours } from "./consoleColours.js"; |
| 9 | + |
| 10 | +const SK = dotnet.Microsoft.SemanticKernel; |
| 11 | + |
| 12 | +// The JS marshaller does not yet support extension methods. |
| 13 | +const kernelBuilder = SK.OpenAIKernelBuilderExtensions.WithAzureChatCompletionService( |
| 14 | + SK.Kernel.Builder, |
| 15 | + process.env['OPENAI_DEPLOYMENT'] || '', |
| 16 | + process.env['OPENAI_ENDPOINT'] || '', |
| 17 | + process.env['OPENAI_KEY'] || '', |
| 18 | +); |
| 19 | + |
| 20 | +const kernel = kernelBuilder |
| 21 | + .Build(); |
| 22 | + |
| 23 | +const r1 = readline.createInterface({ |
| 24 | + input: process.stdin, |
| 25 | + output: process.stdout, |
| 26 | + prompt: 'SK> Hello. Ask me a question or say goodbye to exit.' |
| 27 | +}); |
| 28 | + |
| 29 | +let chatHistory: string[] = []; |
| 30 | + |
| 31 | +r1.prompt(); |
| 32 | +console.log(""); |
| 33 | +r1.on('line', async (userInput: string) => { |
| 34 | + if (userInput.toLowerCase() === 'goodbye') { |
| 35 | + console.log('SK> Goodbye!'); |
| 36 | + process.exit(0); |
| 37 | + } else { |
| 38 | + chatHistory.push(userInput); |
| 39 | + // The JS marshaller does not yet support extension methods. |
| 40 | + const chatFunction = SK.InlineFunctionsDefinitionExtension |
| 41 | + .CreateSemanticFunction(kernel, chatHistory.join("")); |
| 42 | + |
| 43 | + const reply = await kernel.RunAsync("", [chatFunction]); |
| 44 | + |
| 45 | + chatHistory.push(`${reply}`); |
| 46 | + console.log(colours.green, `Answer> ${reply}`); |
| 47 | + console.log(colours.white, `SK> Ask me another question or say goodbye to exit.`); |
| 48 | + } |
| 49 | +}).on('close', () => { |
| 50 | + console.log(colours.white, 'SK> Goodbye!'); |
| 51 | + process.exit(0); |
| 52 | +}); |
0 commit comments