Files
semantic-kernel/dotnet
SergeyMenshykh e0a439decd Support complex payload for OpenAPI skills (#680)
### Motivation and Context

This PR changes the way payload/body for POST and PUT operations is
resolved.

This change is required to support the following scenarios:
- A POST/PUT operation payload has more than one property with the same
name - { 'sender': { '**name**': 'val1'}, 'receiver': {'**name**':
'val2'} }
- A POST/PUT operation payload has a property of array of objects with
the same name - { 'receivers': [ {'**name**':'val1'},
{'**name**':'val2'} ] }
- A POST/PUT operation payload metadata is missing.

### Description
Curent RestApi functionality builds operation payload dynamically and
relies on:
- Payload metadata that describes the payload/body schema/shape and
usually obtained from OpenAPI documents.
- Arguments resolution mechanism that resolves payload parameters in SK
context.

As practice shows, in some cases (malformed OpenAPI document), the
payload metadata is missing which means that it's not possible to create
operation payload dynamically and as a result it's not possible to run
the operation. In other cases, if a payload has two of more parameters
with the same name or parameters of array type with object items it is
impossible to resolve them from SK context.

To support these scenarios, the RestApi functionality does not build the
payload dynamically anymore and instead relies on either AI or a caller
of the APi to create/supply it. To help AI generate the payload, a new
schema property will be added to the ParameterView class to describe the
payload structure/shape to AI.
2023-04-27 22:23:30 +00:00
..
2023-03-14 11:58:59 -07:00
2023-04-26 20:39:41 +00:00

Get Started with Semantic Kernel ⚡

OpenAI / Azure OpenAI API keys

To run the LLM prompts and semantic functions in the examples below, make sure you have an Open AI API Key or Azure Open AI service key.

Nuget package

Here is a quick example of how to use Semantic Kernel from a C# console app. First, let's create a new project, targeting .NET 6 or newer, and add the Microsoft.SemanticKernel nuget package to your project from the command prompt in Visual Studio:

dotnet add package Microsoft.SemanticKernel --prerelease

Running prompts with input parameters

Copy and paste the following code into your project, with your Azure OpenAI key in hand:

using Microsoft.SemanticKernel;

var kernel = Kernel.Builder.Build();

// Azure OpenAI
kernel.Config.AddAzureTextCompletionService(
    "davinci-azure",                     // Alias used by the kernel
    "text-davinci-003",                  // Azure OpenAI Deployment Name
    "https://contoso.openai.azure.com/", // Azure OpenAI Endpoint
    "...your Azure OpenAI Key..."        // Azure OpenAI Key
);

// Alternative using OpenAI
// kernel.Config.AddOpenAITextCompletionService("davinci-openai",
//     "text-davinci-003",               // OpenAI Model name
//     "...your OpenAI API Key..."       // OpenAI API Key
// );

var prompt = @"{{$input}}

One line TLDR with the fewest words.";

var summarize = kernel.CreateSemanticFunction(prompt);

string text1 = @"
1st Law of Thermodynamics - Energy cannot be created or destroyed.
2nd Law of Thermodynamics - For a spontaneous process, the entropy of the universe increases.
3rd Law of Thermodynamics - A perfect crystal at zero Kelvin has zero entropy.";

string text2 = @"
1. An object at rest remains at rest, and an object in motion remains in motion at constant speed and in a straight line unless acted on by an unbalanced force.
2. The acceleration of an object depends on the mass of the object and the amount of force applied.
3. Whenever one object exerts a force on another object, the second object exerts an equal and opposite on the first.";

Console.WriteLine(await summarize.InvokeAsync(text1));

Console.WriteLine(await summarize.InvokeAsync(text2));

// Output:
//   Energy conserved, entropy increases, zero entropy at 0K.
//   Objects move in response to forces.

Prompt chaining

The previous code shows how to invoke individual semantic functions, but you can also chain functions (aka prompt chaining) to process the initial input with multiple operations.

The following code for example, translates an initial text to math symbols and then generates a summary:

string translationPrompt = @"{{$input}}

Translate the text to math.";

string summarizePrompt = @"{{$input}}

Give me a TLDR with the fewest words.";

var translator = kernel.CreateSemanticFunction(translationPrompt);
var summarize = kernel.CreateSemanticFunction(summarizePrompt);

string inputText = @"
1st Law of Thermodynamics - Energy cannot be created or destroyed.
2nd Law of Thermodynamics - For a spontaneous process, the entropy of the universe increases.
3rd Law of Thermodynamics - A perfect crystal at zero Kelvin has zero entropy.";

// Run two prompts in sequence (prompt chaining)
var output = await kernel.RunAsync(inputText, translator, summarize);

Console.WriteLine(output);

// Output: ΔE = 0, ΔSuniv > 0, S = 0 at 0K.

Semantic Kernel Notebooks

The repository contains also a few C# Jupyter notebooks that demonstrates how to get started with the Semantic Kernel.

See here for the full list, with requirements and setup instructions.

  1. Getting started
  2. Loading and configuring Semantic Kernel
  3. Running AI prompts from file
  4. Creating Semantic Functions at runtime (i.e. inline functions)
  5. Using Context Variables to Build a Chat Experience
  6. Creating and Executing Plans
  7. Building Memory with Embeddings
  8. Creating images with DALL-E 2
  9. Chatting with ChatGPT and Images

Nuget packages

Semantic Kernel provides a set of nuget packages to allow extending the core with more features, such as connectors to services and Skills to perform specific actions. Unless you need to optimize which packages to include in your app, you will usually start by installing this meta-package first:

  • Microsoft.SemanticKernel

This meta package includes core packages and OpenAI connectors, allowing to run most samples and build apps with OpenAI and Azure OpenAI.

Packages included in Microsoft.SemanticKernel:

  1. Microsoft.SemanticKernel.Abstractions: contains common interfaces and classes used by the core and other SK components.
  2. Microsoft.SemanticKernel.Core: contains the core logic of SK, such as prompt engineering, semantic memory and semantic functions definition and orchestration.
  3. Microsoft.SemanticKernel.Connectors.AI.OpenAI: connectors to OpenAI and Azure OpenAI, allowing to run semantic functions, chats, image generation with GPT3, GPT3.5, GPT4, DALL-E2. Includes also GPT tokenizers.

Other SK packages available at nuget.org:

  1. Microsoft.SemanticKernel.Connectors.Memory.Qdrant: Qdrant connector for skills and semantic memory.
  2. Microsoft.SemanticKernel.Connectors.Memory.Sqlite: SQLite connector for skills and semantic memory
  3. Microsoft.SemanticKernel.Skills.Document: Document Skill: Word processing, OpenXML, etc.
  4. Microsoft.SemanticKernel.Skills.MsGraph: Microsoft Graph Skill: access your tenant data, schedule meetings, send emails, etc.
  5. Microsoft.SemanticKernel.Skills.OpenAPI: OpenAPI skill.
  6. Microsoft.SemanticKernel.Skills.Web: Web Skill: search the web, download files, etc.