Skip to content

microsoft/semantic-kernel

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

…request settings to be selected during semantic function execution (#3227)

### Motivation and Context

Fix following issues for Semantic functions:

1. Allow semantic functions to dynamically retrieve the AI service and
request settings to be used when executing the function

Previously semantic functions execution has the following limitations:

1. The AI service was set on the semantic function when it was created.
This meant that an instance of a semantic function could not be used
with multiple different Kernel instances.
1. The AI request settings were set on the semantic function when it was
created. This meant that different request settings could not be
selected when the function was executed.

### Description

This PR add's a new abstraction called `IAIServiceSelector`. An instance
of this is added to a semantic function when it is created. The default
implementation works as follows:

1. If the semantic function only has a single associated model request
setting instance with no service id then the default AI service and the
model request settings are used. This is consistent with the previous
behaviour.
1. If the semantic function only has a single associated model request
setting instance with a service id then the named AI service and the
model request settings are used. If the named AI service is not
available an exception will be thrown. This is consistent with the
previous behaviour.
1. If the semantic function only has multiple associated model request
setting instances then:
    1. The model request setting instances are considered in order
1. The first model request setting instance with no service id is
considered the default
1. For each model request setting instance that has a service id, if the
service exists then it will be used with the associated model request
settings
1. If no matching service can be found and default request settings are
provided the default service will be used
1. If no matching service can be found and no default request settings
are provided an exception will be thrown

### 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 πŸ˜„
3601022

Git stats

Files

Permalink
Failed to load latest commit information.

Semantic Kernel

Python package Nuget package dotnet Docker dotnet Windows License: MIT Discord

Semantic Kernel is an SDK that integrates Large Language Models (LLMs) like OpenAI, Azure OpenAI, and Hugging Face with conventional programming languages like C#, Python, and Java. Semantic Kernel achieves this by allowing you to define plugins that can be chained together in just a few lines of code.

What makes Semantic Kernel special, however, is its ability to automatically orchestrate plugins with AI. With Semantic Kernel planners, you can ask an LLM to generate a plan that achieves a user's unique goal. Afterwards, Semantic Kernel will execute the plan for the user.

Please star the repo to show your support for this project!

Orchestrating plugins with planner

Getting started with Semantic Kernel

The Semantic Kernel SDK is available in C#, Python, and Java. To get started, choose your preferred language below. See the Feature Matrix to see a breakdown of feature parity between our currently supported languages.

Java logo

The quickest way to get started with the basics is to get an API key from either OpenAI or Azure OpenAI and to run one of the C#, Python, and Java console applications/scripts below.

For C#:

  1. Create a new console app.
  2. Add the semantic kernel nuget Microsoft.SemanticKernel.
  3. Copy the code from here into the app Program.cs file.
  4. Replace the configuration placeholders for API key and other params with your key and settings.
  5. Run with F5 or dotnet run

For Python:

  1. Install the pip package: python -m pip install semantic-kernel.
  2. Create a new script e.g. hello-world.py.
  3. Store your API key and settings in an .env file as described here.
  4. Copy the code from here into the hello-world.py script.
  5. Run the python script.

For Java:

  1. Clone and checkout the experimental Java branch: git clone -b experimental-java https://github.com/microsoft/semantic-kernel.git
  2. Follow the instructions here

Learning how to use Semantic Kernel

The fastest way to learn how to use Semantic Kernel is with our C# and Python Jupyter notebooks. These notebooks demonstrate how to use Semantic Kernel with code snippets that you can run with a push of a button.

Once you've finished the getting started notebooks, you can then check out the main walkthroughs on our Learn site. Each sample comes with a completed C# and Python project that you can run locally.

  1. πŸ“– Overview of the kernel
  2. πŸ”Œ Understanding AI plugins
  3. πŸ‘„ Creating semantic functions
  4. πŸ’½ Creating native functions
  5. ⛓️ Chaining functions together
  6. πŸ€– Auto create plans with planner
  7. πŸ’‘ Create and run a ChatGPT plugin

Finally, refer to our API references for more details on the C# and Python APIs:

Chat Copilot: see what's possible with Semantic Kernel

If you're interested in seeing a full end-to-end example of how to use Semantic Kernel, check out our Chat Copilot reference application. Chat Copilot is a chatbot that demonstrates the power of Semantic Kernel. By combining plugins, planners, and personas, we demonstrate how you can build a chatbot that can maintain long-running conversations with users while also leveraging plugins to integrate with other services.

Chat Copilot answering a question

You can run the app yourself by downloading it from its GitHub repo.

Visual Studio Code extension: design semantic functions with ease

The Semantic Kernel extension for Visual Studio Code makes it easy to design and test semantic functions. The extension provides an interface for designing semantic functions and allows you to test them with a push of a button with your existing models and data.

Semantic Kernel extension for Visual Studio Code

In the above screenshot, you can see the extension in action:

  • Syntax highlighting for semantic functions
  • Code completion for semantic functions
  • LLM model picker
  • Run button to test the semantic function with your input data

Check out our other repos!

If you like Semantic Kernel, you may also be interested in other repos the Semantic Kernel team supports:

Repo Description
Chat Copilot A reference application that demonstrates how to build a chatbot with Semantic Kernel.
Semantic Kernel Docs The home for Semantic Kernel documentation that appears on the Microsoft learn site.
Semantic Kernel Starters Starter projects for Semantic Kernel to make it easier to get started.
Semantic Memory A service that allows you to create pipelines for ingesting, storing, and querying knowledge.

Join the community

We welcome your contributions and suggestions to SK community! One of the easiest ways to participate is to engage in discussions in the GitHub repository. Bug reports and fixes are welcome!

For new features, components, or extensions, please open an issue and discuss with us before sending a PR. This is to avoid rejection as we might be taking the core in a different direction, but also to consider the impact on the larger ecosystem.

To learn more and get started:

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT license.