This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 3
More OpenAPI examples #7
Draft
mojanas
wants to merge
5
commits into
main
Choose a base branch
from
mojanas/pf-api-planner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b512082
Push changes for simple pf segments api planner
mojanas d5754b8
Merge branch 'main' into mojanas/pf-api-planner
mojanas 8446260
Better openapi spec + another example
mojanas 2013497
Remove old apispec changes
mojanas 2198c1b
Fix swagger
mojanas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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[] | ||
{ | ||
"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?" | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make the two distinct list of questions into one? |
||
|
||
foreach (string q in questions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
try | ||
{ | ||
Console.WriteLine("Question: " + q); | ||
await PlannerExample(q); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
} | ||
} | ||
|
||
private static async Task SkillImportExample() | ||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var