Getting started with Semantic Kernel
In just a few steps, you can build your first AI agent with Semantic Kernel in JavaScript. This guide will show you how to…
- Install the necessary packages
- Create a back-and-forth conversation with an AI
- Give an AI agent the ability to run your code
- Watch the AI create plans on the fly
Install the package
npm install --save semantic-kernel @semantic-kernel/openai
Initialize the Kernel
import { OpenAIChatCompletionService } from '@semantic-kernel/openai';
import { FunctionChoiceBehavior, kernel, kernelFunction } from 'semantic-kernel';
const sk = kernel().addService(
new OpenAIChatCompletionService({
model: 'gpt-3.5-turbo',
apiKey:
'YOUR_OPENAI_API_KEY',
})
);
Add your plugins
const temperature = kernelFunction(({ loc }) => (loc === 'Dublin' ? 10 : 24), {
name: 'temperature',
description: 'Returns the temperature for the given location',
schema: {
type: 'object',
properties: {
loc: { type: 'string', description: 'The location to return the temperature for' },
},
},
});
sk.addPlugin({
name: 'weather',
description: 'Weather plugin',
functions: [temperature],
});
Invoke prompt
const result = await sk.invokePrompt({
promptTemplate: 'Return the current temperature in Dublin',
executionSettings: {
functionChoiceBehavior: FunctionChoiceBehavior.Auto(),
},
});
Here is the full example:
import { OpenAIChatCompletionService } from '@semantic-kernel/openai';
import { FunctionChoiceBehavior, kernel, kernelFunction } from 'semantic-kernel';
const sk = kernel().addService(
new OpenAIChatCompletionService({
model: 'gpt-3.5-turbo',
apiKey:
'YOUR_OPENAI_API_KEY',
})
);
const temperature = kernelFunction(({ loc }) => (loc === 'Dublin' ? 10 : 24), {
name: 'temperature',
description: 'Returns the temperature for the given location',
schema: {
type: 'object',
properties: {
loc: { type: 'string', description: 'The location to return the temperature for' },
},
},
});
sk.addPlugin({
name: 'weather',
description: 'Weather plugin',
functions: [temperature],
});
const result = await sk.invokePrompt({
promptTemplate: 'Return the current temperature in Dublin',
executionSettings: {
functionChoiceBehavior: FunctionChoiceBehavior.Auto(),
},
});
// Prints the output after executing the plugin and the given prompt
console.log(result);