Adding AI services to Semantic Kernel
One of the main features of Semantic Kernel is its ability to add different AI services to the kernel. This allows you to easily swap out different AI services to compare their performance and to leverage the best model for your needs. In this article, we will provide sample code for adding different AI services to the kernel.
If you want to see any of these samples in a complete solution, you can check them out in the public documentation repository.
Language | Link to final solution |
---|---|
C# | Open example in GitHub |
Java | Open example in GitHub |
Python | Open solution in GitHub |
Azure OpenAI
With Azure OpenAI, you can deploy most of the OpenAI models to the cloud and use them in your Semantic Kernel project. Depending on the model that you want to use, however, you will either need to add the model as a text generation service or as a chat completion service.
The following table shows which service you should use for each model.
Model type | Model |
---|---|
Text generation | text-ada-001 |
Text generation | text-babbage-001 |
Text generation | text-curie-001 |
Text generation | text-davinci-001 |
Text generation | text-davinci-002 |
Text generation | text-davinci-003 |
Chat Completion | gpt-3.5-turbo |
Chat Completion | gpt-4 |
Chat completion deployments
To add an Azure OpenAI chat completion service to your Semantic Kernel project, you will need to use the AddAzureChatCompletionService method.
Kernel kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey)
.Build();
Text generation deployments
To add an Azure OpenAI text generation service to your Semantic Kernel project, you will need to use the AddAzureTextCompletionService method.
kernel = Kernel.CreateBuilder()
.AddAzureOpenAITextGeneration(textModelId, endpoint, apiKey)
.Build();
OpenAI
Similar to Azure OpenAI, depending on the model that you want to use from OpenAI, you will either need to add the model as a text generation service or as a chat completion service.
The following table shows which service you should use for each model.
Model type | Model |
---|---|
Text generation | text-ada-001 |
Text generation | text-babbage-001 |
Text generation | text-curie-001 |
Text generation | text-davinci-001 |
Text generation | text-davinci-002 |
Text generation | text-davinci-003 |
Chat Completion | gpt-3.5-turbo |
Chat Completion | gpt-4 |
Chat completion models
To add an OpenAI text generation service to your Semantic Kernel project, you will need to use the AddOpenAIChatCompletionService method.
kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(openAImodelId, openAIapiKey)
.Build();
Text generation models
To add an OpenAI text generation service to your Semantic Kernel project, you will need to use the AddOpenAITextCompletionService method.
kernel = Kernel.CreateBuilder()
.AddOpenAITextGeneration(openAItextModelId, openAIapiKey)
.Build();
Next steps
Now that you know how to add different AI services to your Semantic Kernel project, you can learn now to add telemetry and logging to the kernel.