Files
semantic-kernel/node-api-dotnet/samples/NodeKernelSyntaxExamples/Example01_SummarizeText.js
Mark Wallace ba25909871 First node-api-dotnet example showing how to invoke a semantic function (#2600)
### Motivation and Context

Please help reviewers and future users, providing the following
information:
1. Why is this change required? Demonstrates how to call Semantic Kernel
from Node

### Description

First node-api-dotnet example showing how to invoke a semantic function

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
2023-09-04 09:02:31 +01:00

48 lines
1.4 KiB
JavaScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// @ts-check
import './bin/Microsoft.SemanticKernel.Core.js';
import './bin/Microsoft.SemanticKernel.Connectors.AI.OpenAI.js';
import dotnet from 'node-api-dotnet';
const SK = dotnet.Microsoft.SemanticKernel;
const Logging = dotnet.Microsoft.Extensions.Logging;
// The JS marshaller does not yet support extension methods.
const kernelBuilder = SK.OpenAIKernelBuilderExtensions.WithAzureTextCompletionService(
SK.Kernel.Builder,
process.env['AzureOpenAI__DeploymentName'] || '',
process.env['AzureOpenAI__Endpoint'] || '',
process.env['AzureOpenAI__ApiKey'] || '',
);
const kernel = kernelBuilder
.Build();
const skPrompt = `
{{$input}}
Give me the TLDR in 5 words.
`;
const textToSummarize = `
1) A robot may not injure a human being or, through inaction,
allow a human being to come to harm.
2) A robot must obey orders given it by human beings except where
such orders would conflict with the First Law.
3) A robot must protect its own existence as long as such protection
does not conflict with the First or Second Law.
`;
// The JS marshaller does not yet support extension methods.
const summarizeFunction = SK.InlineFunctionsDefinitionExtension
.CreateSemanticFunction(kernel, skPrompt);
const summary = await kernel.RunAsync(textToSummarize, [ summarizeFunction ]);
console.log(summary.toString());