Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

More OpenAPI examples #7

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.TextCompletion;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning;
using Microsoft.SemanticKernel.Planning.Stepwise;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Skills.OpenAPI.Authentication;
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
Expand All @@ -20,7 +24,49 @@ public static class Example_00_03_OpenApiSkill_PlayFab
{
public static async Task RunAsync()
{
// Simple example
await SkillImportExample();

// Example semantic skill for generating PlayFab segments
string[] questions = new string[]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string[]

var

{
"How do I create a segment for Android players in Canada?",
"How do I create a segment for players with high risk of churn who have spent over $100?"
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the two distinct list of questions into one?
(and then have something like the action-planner picking one of your skills to do the job)


foreach (string q in questions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question

{
try
{
Console.WriteLine("Question: " + q);
await JsonExample(q);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}


// Examples for native skill calling PlayFab APIs
questions = new string[]
{
"Get the details for all my PlayFab segments",
"Do I have a segment that filters for Canadian players?",
};

foreach (string q in questions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q

question

{
try
{
Console.WriteLine("Question: " + q);
await PlannerExample(q);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}

private static async Task SkillImportExample()
Expand Down Expand Up @@ -49,6 +95,75 @@ private static async Task SkillImportExample()
}
}

private static async Task PlannerExample(string question)
{
var kernel = new KernelBuilder()
.WithLogger(ConsoleLogger.Logger)
.WithAzureChatCompletionService(TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.Endpoint, TestConfiguration.AzureOpenAI.ApiKey, alsoAsTextCompletion: true, setAsDefault: true)
.Build();

SKContext context = kernel.CreateNewContext();

using HttpClient httpClient = new();

var playfabApiSkills = await GetPlayFabSkill(kernel, httpClient);

var plannerConfig = new StepwisePlannerConfig();
plannerConfig.ExcludedFunctions.Add("TranslateMathProblem");
plannerConfig.MinIterationTimeMs = 1500;
plannerConfig.MaxTokens = 1000;
plannerConfig.MaxIterations = 10;

var settings = new CompleteRequestSettings { Temperature = 0.1, MaxTokens = 500 };

StepwisePlanner planner = new(kernel, plannerConfig);
Plan plan = planner.CreatePlan(question);

SKContext? result = await plan.InvokeAsync(context, settings);

Console.WriteLine("Result: " + result);
if (result.Variables.TryGetValue("stepCount", out string? stepCount))
{
Console.WriteLine("Steps Taken: " + stepCount);
}

if (result.Variables.TryGetValue("skillCount", out string? skillCount))
{
Console.WriteLine("Skills Used: " + skillCount);
}
}

private static async Task JsonExample(string question)
{
var kernel = new KernelBuilder()
.WithLogger(ConsoleLogger.Logger)
.WithAzureChatCompletionService(TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.Endpoint, TestConfiguration.AzureOpenAI.ApiKey, alsoAsTextCompletion: true, setAsDefault: true)
.Build();

SKContext context = kernel.CreateNewContext();

string miniJson = await GetMinifiedOpenApiJson();

string FunctionDefinition = @"
You are an AI assistant for generating PlayFab segment API requests. You have access to the full OpenAPI 3.0.1 specification.
If you do not know how to answer the question, reply with 'I cannot answer this'.

Api Spec:
{{$apiSpec}}

Example: How do I create a segment for Canadian players?
Answer: To create a segment for Canadian players, you would use the `CreateSegment` API and include a LocationSegmentFilter definition with the country set to Canada.

Question:
{{$input}}".Replace("{{$apiSpec}}", miniJson, StringComparison.OrdinalIgnoreCase);

var playfabJsonFunction = kernel.CreateSemanticFunction(FunctionDefinition, temperature: 0.1, topP: 1);

var result = await playfabJsonFunction.InvokeAsync(question);

Console.WriteLine("Result: " + result);
}

private static async Task<IDictionary<string, ISKFunction>> GetPlayFabSkill(IKernel kernel, HttpClient httpClient)
{
IDictionary<string, ISKFunction> playfabApiSkills;
Expand All @@ -62,15 +177,38 @@ private static async Task<IDictionary<string, ISKFunction>> GetPlayFabSkill(IKer
bool useLocalFile = true;
if (useLocalFile)
{
var playfabApiFile = "../../../Skills/PlayFabApiSkill/openapi.json";
playfabApiSkills = await kernel.ImportOpenApiSkillFromFileAsync("PlayFabApiSkill", playfabApiFile, new OpenApiSkillExecutionParameters(httpClient, authCallback: titleSecretKeyProvider.AuthenticateRequestAsync)); ;
var playfabApiFile = "../../../Skills/PlayFabApiSkill/openapi_updated.json";
var parameters = new OpenApiSkillExecutionParameters(httpClient, authCallback: titleSecretKeyProvider.AuthenticateRequestAsync, serverUrlOverride: new Uri(TestConfiguration.PlayFab.Endpoint));

playfabApiSkills = await kernel.ImportAIPluginAsync("PlayFabApiSkill", playfabApiFile, parameters);
}
else
{
var playfabApiRawFileUrl = new Uri(TestConfiguration.PlayFab.SwaggerEndpoint);
playfabApiSkills = await kernel.ImportOpenApiSkillFromUrlAsync("PlayFabApiSkill", playfabApiRawFileUrl, new OpenApiSkillExecutionParameters(httpClient, authCallback: titleSecretKeyProvider.AuthenticateRequestAsync)); ;
var parameters = new OpenApiSkillExecutionParameters(httpClient, authCallback: titleSecretKeyProvider.AuthenticateRequestAsync, serverUrlOverride: new Uri(TestConfiguration.PlayFab.Endpoint));

playfabApiSkills = await kernel.ImportAIPluginAsync("PlayFabApiSkill", playfabApiRawFileUrl, parameters);
}

return playfabApiSkills;
}

private static async Task<string> GetMinifiedOpenApiJson()
{
var playfabApiFile = "../../../Skills/PlayFabApiSkill/openapi_updated.json";

var pluginJson = string.Empty;

if (!File.Exists(playfabApiFile))
{
throw new FileNotFoundException($"Invalid URI. The specified path '{playfabApiFile}' does not exist.");
}

using (var sr = File.OpenText(playfabApiFile))
{
pluginJson = await sr.ReadToEndAsync().ConfigureAwait(false); //must await here to avoid stream reader being disposed before the string is read
}

return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(pluginJson), Formatting.None);
}
}
Loading
Loading