Skip to content

Commit 1e2e8d4

Browse files
Implementation of a typescript console chat application based on the c# example.
1 parent 7e8baec commit 1e2e8d4

11 files changed

+222
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Alternatively, you can use the [Semantic Kernel Tools](https://marketplace.visua
1616
- [C# Azure Functions](sk-csharp-azure-functions): The Hello World C# Azure Functions starter for the Semantic Kernel.
1717
- [Python Hello World](sk-python-hello-world): The Hello World Python console application starter for the Semantic Kernel.
1818
- [Python Azure Functions](sk-python-azure-functions): The Hello World Python Azure Functions starter for the Semantic Kernel.
19+
- [Typescript Console Chat](sk-typescript-console-chat): A console application based chat starter for the Semantic Kernel.
1920

2021
## Contributing
2122

sk-starters.sln

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 17.5.33530.505
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk-csharp-azure-functions", "sk-csharp-azure-functions\sk-csharp-azure-functions.csproj", "{A3C12DC4-9448-4DFC-A8BA-6841F6236156}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk-csharp-chatgpt-plugin", "sk-csharp-chatgpt-plugin\azure-function\sk-chatgpt-azure-function.csproj", "{2E21E690-D091-4116-BD9B-2B2049C84DFF}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk-chatgpt-azure-function", "sk-csharp-chatgpt-plugin\azure-function\sk-chatgpt-azure-function.csproj", "{2E21E690-D091-4116-BD9B-2B2049C84DFF}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "semantic-functions-generator", "sk-csharp-chatgpt-plugin\semantic-functions-generator\semantic-functions-generator.csproj", "{6F9921F1-CDD6-4173-8AE7-6416FF4EB847}"
1111
EndProject
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk-csharp-console-chat", "s
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk_csharp_apim_demo", "sk_csharp_apim_demo\sk_csharp_apim_demo.csproj", "{FC9DF14A-6CCB-42FA-BE38-C61F87C06094}"
1717
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk-typescript-console-chat", "sk-typescript-console-chat\sk-typescript-console-chat.csproj", "{9FEE5A32-69C2-4DEF-9179-BBCD80AB8317}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{FC9DF14A-6CCB-42FA-BE38-C61F87C06094}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{FC9DF14A-6CCB-42FA-BE38-C61F87C06094}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{FC9DF14A-6CCB-42FA-BE38-C61F87C06094}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{9FEE5A32-69C2-4DEF-9179-BBCD80AB8317}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{9FEE5A32-69C2-4DEF-9179-BBCD80AB8317}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{9FEE5A32-69C2-4DEF-9179-BBCD80AB8317}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{9FEE5A32-69C2-4DEF-9179-BBCD80AB8317}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
{
5+
"parser": "@typescript-eslint/parser",
6+
"parserOptions": {
7+
"ecmaVersion": "latest",
8+
"sourceType": "module"
9+
},
10+
"plugins": [ "@typescript-eslint" ],
11+
"root": true,
12+
"rules": {
13+
// Add rules here
14+
}
15+
}

sk-typescript-console-chat/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**/bin
2+
**/obj
3+
**/pkg
4+
*.js
5+
*.map
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
export const colours = {
5+
black: "\x1b[30m",
6+
red: "\x1b[31m",
7+
green: "\x1b[32m",
8+
yellow: "\x1b[33m",
9+
blue: "\x1b[34m",
10+
magenta: "\x1b[35m",
11+
cyan: "\x1b[36m",
12+
white: "\x1b[37m",
13+
gray: "\x1b[90m",
14+
crimson: "\x1b[38m"
15+
};

sk-typescript-console-chat/package-lock.json

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "module",
3+
"scripts": {
4+
"build": "tsc --build"
5+
},
6+
"dependencies": {
7+
"node-api-dotnet": "^0.4.4",
8+
"typescript": "^5.2.2"
9+
},
10+
"devDependencies": {
11+
"@types/node": "^20.6.0"
12+
}
13+
}

sk-typescript-console-chat/readme.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
## Example: Using .NET Semantic Kernel to call Azure OpenAI
3+
The `consoleChat.js` script dynamically loads the [`Microsoft.SemanticKernel`](https://github.com/microsoft/semantic-kernel) .NET assembly and uses it
4+
to call Azure OpenAI.
5+
6+
This application allows you to have a conversation with Azure OpenAI. It will ask you to enter your question
7+
and return the response. You can continue the conversation by entering another question. When finished, type `goodbye`
8+
to end the conversation.
9+
10+
To run this example, first set the following environment variables, as System variables, referencing your
11+
[Azure OpenAI deployment](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart):
12+
- `OPENAI_ENDPOINT`
13+
- `OPENAI_DEPLOYMENT`
14+
- `OPENAI_KEY`
15+
16+
Then run the following commands in sequence:
17+
18+
| Command | Explanation
19+
|----------------------------------|--------------------------------------------------
20+
| `dotnet build` | Install [`SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) nuget packages into the project and generate type definitions.
21+
| `npm install` | Install [`node-api-dotnet`](https://www.npmjs.com/package/node-api-dotnet) npm package into the project.
22+
| `npm run build` | Transpile the typescript to javascript.
23+
| `node consoleChat.js` | Run consoleChat JS code that uses the above packages to call the Azure OpenAI service.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<OutDir>bin</OutDir>
6+
<NodeApiAssemblyJSModuleType>esm</NodeApiAssemblyJSModuleType>
7+
<GenerateNodeApiTypeDefinitionsForReferences>true</GenerateNodeApiTypeDefinitionsForReferences>
8+
<RestorePackagesPath>$(MSBuildThisFileDirectory)/pkg</RestorePackagesPath>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.4.4" />
12+
<PackageReference Include="Microsoft.SemanticKernel" Version="0.24.230912.2-preview" />
13+
</ItemGroup>
14+
</Project>
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig to read more about this file */
4+
/* Language and Environment */
5+
"target": "es2017", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
6+
/* Modules */
7+
"module": "ESNext", /* Specify what module code is generated. */
8+
"types": [ "node" ], /* Specify type package names to be included without being referenced in a source file. */
9+
/* Emit */
10+
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
11+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
12+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
13+
/* Type Checking */
14+
"strict": true, /* Enable all strict type-checking options. */
15+
/* Completeness */
16+
"skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
17+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
18+
},
19+
"exclude": ["./bin/*.d.ts"] // Don't typecheck the type files for the SemanticKernel assemblies because some of the types used are not supported by node-dotnet-api conversion. Specifically the c# record type.
20+
}

0 commit comments

Comments
 (0)