Skip to content

Commit

Permalink
Update example to latest SK package
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin committed Sep 23, 2023
1 parent 663ede9 commit eed77b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions examples/semantic-kernel/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,41 @@ import dotnet from 'node-api-dotnet';
import './bin/Microsoft.SemanticKernel.Core.js';
import './bin/Microsoft.SemanticKernel.Connectors.AI.OpenAI.js';

// The PromptTemplateEngine assembly must be explicitly loaded here, because
// SK KernelBuilder uses Assembly.Load() to load it, and that is not detected
// by the JS exporter.
import './bin/Microsoft.SemanticKernel.TemplateEngine.PromptTemplateEngine.js';

const SK = dotnet.Microsoft.SemanticKernel;
const Logging = dotnet.Microsoft.Extensions.Logging;

/** @type {dotnet.Microsoft.Extensions.Logging.ILogger} */
const logger = {
Log(logLevel, eventId, state, exception, formatter) {
console.log(`LOG (${Logging.LogLevel[logLevel]}): ${formatter(state, exception)}`);
console.log(`LOG (${Logging.LogLevel[logLevel || 0]}): ${formatter(state, exception)}`);
},
IsEnabled(logLevel) { return true; },
BeginScope(state) { return { dispose() { } }; },
};

const kernel = SK.Kernel.Builder
.WithLogger(logger)
.Build();
/** @type {dotnet.Microsoft.Extensions.Logging.ILoggerFactory} */
const loggerFactory = {
CreateLogger(categoryName) { return logger; },
AddProvider(provider) { },
dispose() {}
};

// The JS marshaller does not yet support extension methods.
SK.KernelConfigOpenAIExtensions.AddAzureTextCompletionService(
kernel.Config,
const kernelBuilder = SK.OpenAIKernelBuilderExtensions.WithAzureChatCompletionService(
SK.Kernel.Builder.WithLoggerFactory(loggerFactory),
process.env['OPENAI_DEPLOYMENT'] || '',
process.env['OPENAI_ENDPOINT'] || '',
process.env['OPENAI_KEY'] || '',
);
const kernel = kernelBuilder.Build();

const skPrompt = `
{{$input}}
const skPrompt = `{{$input}}
Give me the TLDR in 5 words.
Give me the TLDR in 10 words.
`;

const textToSummarize = `
Expand All @@ -49,8 +56,9 @@ does not conflict with the First or Second Law.
`;

// The JS marshaller does not yet support extension methods.
const tldrFunction = SK.InlineFunctionsDefinitionExtension
const summaryFunction = SK.InlineFunctionsDefinitionExtension
.CreateSemanticFunction(kernel, skPrompt);

const summary = await tldrFunction.InvokeAsync(textToSummarize);
const summary = await SK.SKFunctionExtensions.InvokeAsync(summaryFunction, textToSummarize);

console.log(summary.toString());
2 changes: 1 addition & 1 deletion examples/semantic-kernel/semantic-kernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" Version="0.14.547.1-preview" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.24.230918.1-preview" />
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.4.*-*" />
</ItemGroup>

Expand Down

0 comments on commit eed77b8

Please sign in to comment.