Skip to content

Commit

Permalink
Jira plugin open api (microsoft#648)
Browse files Browse the repository at this point in the history
### Motivation and Context
Provide an example implementation of connecting the Semantic Kernel to
external plugins following the open api schema.

### Description
Jira Open API schema usage example
Renaming the azure key vault example, so all open api skill examples are
grouped together
Prevent RestApiOperationRunner.cs from wrapping a json response into a
json object.
  • Loading branch information
amsacha authored and awharrison-28 committed May 1, 2023
1 parent 2470615 commit 9546b8c
Show file tree
Hide file tree
Showing 10 changed files with 1,419 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using RepoUtils;

// ReSharper disable once InconsistentNaming
public static class Example22_OpenApiSkill
public static class Example22_OpenApiSkill_AzureKeyVault
{
public static async Task RunAsync()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Skills.OpenAPI.Authentication;
using Newtonsoft.Json;
using RepoUtils;

/// <summary>
/// This sample shows how to connect the Semantic Kernel to Jira as an Open Api plugin based on the Open Api schema.
/// This format of registering the skill and its operations, and subsequently executing those operations can be applied
/// to an Open Api plugin that follows the Open Api Schema.
/// </summary>
public static class Example23_OpenApiSkill_Jira
{
public static async Task RunAsync()
{
var kernel = new KernelBuilder().WithLogger(ConsoleLogger.Log).Build();
var contextVariables = new ContextVariables();

// Change <your-domain> to a jira instance you have access to with your authentication credentials
string serverUrl = "https://<your-domain>.atlassian.net/rest/api/latest/";
contextVariables.Set("server-url", serverUrl);

IDictionary<string, ISKFunction> jiraSkills;
var tokenProvider = new BasicAuthenticationProvider(() =>
{
string s = Env.Var("MY_EMAIL_ADDRESS") + ":" + Env.Var("JIRA_API_KEY");
return Task.FromResult(s);
});

// The bool useLocalFile can be used to toggle the ingestion method for the openapi schema between a file path and a URL
bool useLocalFile = true;
if (useLocalFile)
{
var apiSkillFile = "./../../../Skills/JiraSkill/openapi.json";
jiraSkills = await kernel.ImportOpenApiSkillFromFileAsync("jiraSkills", apiSkillFile, tokenProvider.AuthenticateRequestAsync);
}
else
{
var apiSkillRawFileURL = new Uri("https://raw.githubusercontent.com/microsoft/PowerPlatformConnectors/dev/certified-connectors/JIRA/apiDefinition.swagger.json");
jiraSkills = await kernel.ImportOpenApiSkillFromUrlAsync("jiraSkills", apiSkillRawFileURL, null, tokenProvider.AuthenticateRequestAsync);
}

// GetIssue Skill
{
// Set Properties for the Get Issue operation in the openAPI.swagger.json
contextVariables.Set("issueKey", "SKTES-2");

// Run operation via the semantic kernel
var result = await kernel.RunAsync(contextVariables, jiraSkills["GetIssue"]);

Console.WriteLine("\n\n\n");
var formattedContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(result.Result), Formatting.Indented);
Console.WriteLine("GetIssue jiraSkills response: \n{0}", formattedContent);
}

// AddComment Skill
{
// Set Properties for the AddComment operation in the openAPI.swagger.json
contextVariables.Set("issueKey", "SKTES-1");
contextVariables.Set("body", "Here is a rad comment");

// Run operation via the semantic kernel
var result = await kernel.RunAsync(contextVariables, jiraSkills["AddComment"]);

Console.WriteLine("\n\n\n");
var formattedContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(result.Result), Formatting.Indented);
Console.WriteLine("AddComment jiraSkills response: \n{0}", formattedContent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// of <see cref="IMemoryStore"/> has a single collection, and thus does not need to be named.
/// It also assumes that the JSON formatted data can be deserialized into <see cref="MemoryRecord"/> objects.
/// </summary>
public static class Example23_ReadOnlyMemoryStore
public static class Example24_ReadOnlyMemoryStore
{
public static async Task RunAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// If you use Semantic Kernel with other models, the tokenization logic is most probably different,
/// and you should not use the GPT tokenizer.
/// </summary>
public static class Example24_Tokenizer
public static class Example25_Tokenizer
{
public static void Run()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

// ReSharper disable once InconsistentNaming
public static class Example25_AADAuth
public static class Example26_AADAuth
{
public static async Task RunAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

// ReSharper disable once InconsistentNaming
public static class Example26_SemanticFunctionsUsingChatGPT
public static class Example27_SemanticFunctionsUsingChatGPT
{
public static async Task RunAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using RepoUtils;

// ReSharper disable once InconsistentNaming
public static class Example27_ActionPlanner
public static class Example28_ActionPlanner
{
public static async Task RunAsync()
{
Expand Down
15 changes: 9 additions & 6 deletions samples/dotnet/kernel-syntax-examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,25 @@ public static async Task Main()
await Example21_ChatGptPlugins.RunAsync();
Console.WriteLine("== DONE ==");

await Example22_OpenApiSkill.RunAsync();
await Example22_OpenApiSkill_AzureKeyVault.RunAsync();
Console.WriteLine("== DONE ==");

await Example23_ReadOnlyMemoryStore.RunAsync();
await Example23_OpenApiSkill_Jira.RunAsync();
Console.WriteLine("== DONE ==");

Example24_Tokenizer.Run();
await Example24_ReadOnlyMemoryStore.RunAsync();
Console.WriteLine("== DONE ==");

await Example25_AADAuth.RunAsync();
Example25_Tokenizer.Run();
Console.WriteLine("== DONE ==");

await Example26_SemanticFunctionsUsingChatGPT.RunAsync();
await Example26_AADAuth.RunAsync();
Console.WriteLine("== DONE ==");

await Example27_ActionPlanner.RunAsync();
await Example27_SemanticFunctionsUsingChatGPT.RunAsync();
Console.WriteLine("== DONE ==");

await Example28_ActionPlanner.RunAsync();
Console.WriteLine("== DONE ==");

await Example28_ChatWithPrompts.RunAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Jira Open API Schema

We have our own curated version of the Jira Open API schema because the one available online
at https://raw.githubusercontent.com/microsoft/PowerPlatformConnectors/dev/certified-connectors/JIRA/apiDefinition.swagger.json,
doesn't follow OpenAPI specification for all of its operations. For example CreateIssueV2, its body param does not describe properties
and so we can't build the body automatically.

0 comments on commit 9546b8c

Please sign in to comment.