Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example23_OpenApiSkill_Github.cs
T
01afd3f128 .Net: Update KernelSyntaxExamples env variables (#1832)
### Description
Updating KernelSyntaxExamples to use the same environment variable keys
as the Integration tests. This is a step toward making these tests
simpler to run, and even running them as automation in CI tests.

This is a first pass -- more cleanup to be done.

### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [X] The code builds clean without any errors or warnings
- [X] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [X] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄

---------

Co-authored-by: name <email>
Co-authored-by: Dmytro Struk <[email protected]>
2023-07-17 17:36:06 +00:00

85 lines
3.7 KiB
C#

// 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.Skills.OpenAPI.Authentication;
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RepoUtils;
/// <summary>
/// Import and run GitHub Functions using OpenAPI Skill.
/// To use this example, run:
/// dotnet user-secrets set "Github.PAT" "github_pat_..."
/// Make sure your GitHub PAT has read permissions set for Pull Requests.
/// Creating a PAT: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
/// </summary>
// ReSharper disable once InconsistentNaming
public static class Example23_OpenApiSkill_GitHub
{
public static async Task RunAsync()
{
var authenticationProvider = new BearerAuthenticationProvider(() => { return Task.FromResult(TestConfiguration.Github.PAT); });
Console.WriteLine("== Example22_c_OpenApiSkill_GitHub ==");
var firstPRNumber = await ListPullRequestsFromGitHubAsync(authenticationProvider);
await GetPullRequestFromGitHubAsync(authenticationProvider, firstPRNumber);
}
public static async Task<string> ListPullRequestsFromGitHubAsync(BearerAuthenticationProvider authenticationProvider)
{
var kernel = new KernelBuilder().WithLogger(ConsoleLogger.Log).Build();
var skill = await kernel.ImportOpenApiSkillFromFileAsync(
"GitHubSkill",
"../../../samples/apps/copilot-chat-app/webapi/Skills/OpenApiSkills/GitHubSkill/openapi.json",
new OpenApiSkillExecutionParameters { AuthCallback = authenticationProvider.AuthenticateRequestAsync });
// Add arguments for required parameters, arguments for optional ones can be skipped.
var contextVariables = new ContextVariables();
contextVariables.Set("owner", "microsoft");
contextVariables.Set("repo", "semantic-kernel");
// Run
var result = await kernel.RunAsync(contextVariables, skill["PullsList"]);
Console.WriteLine("Successful GitHub List Pull Requests skill response.");
var resultJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(result.Result);
var pullRequests = JArray.Parse((string)resultJson!["content"]);
if (pullRequests != null && pullRequests.First != null)
{
var number = pullRequests.First["number"];
return number?.ToString() ?? string.Empty;
}
Console.WriteLine("No pull requests found.");
return string.Empty;
}
public static async Task GetPullRequestFromGitHubAsync(BearerAuthenticationProvider authenticationProvider, string pullNumber)
{
var kernel = new KernelBuilder().WithLogger(ConsoleLogger.Log).Build();
var skill = await kernel.ImportOpenApiSkillFromFileAsync(
"GitHubSkill",
"../../../samples/apps/copilot-chat-app/webapi/Skills/OpenApiSkills/GitHubSkill/openapi.json",
new OpenApiSkillExecutionParameters { AuthCallback = authenticationProvider.AuthenticateRequestAsync });
// Add arguments for required parameters, arguments for optional ones can be skipped.
var contextVariables = new ContextVariables();
contextVariables.Set("owner", "microsoft");
contextVariables.Set("repo", "semantic-kernel");
contextVariables.Set("pull_number", pullNumber);
// Run
var result = await kernel.RunAsync(contextVariables, skill["PullsGet"]);
Console.WriteLine("Successful GitHub Get Pull Request skill response: {0}", result);
}
}