Last active
October 26, 2023 11:26
-
-
Save arafattehsin/62d51e68122983c23486bf5d75b3731f to your computer and use it in GitHub Desktop.
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
public async Task<Riddle> GetRiddle() | |
{ | |
try | |
{ | |
string skPrompt = @"Ask an interesting new riddle with its answer in a below json format. It should only contain a JSON message and nothing else. It should not have anything racist, unethical or targeted to certain group of people. | |
{ | |
""Question"": """", | |
""Answer"": """" | |
} | |
Some historic riddles: | |
{{$history}} | |
"; | |
var aiRequestSettings = new OpenAIRequestSettings() | |
{ | |
MaxTokens = 200, | |
Temperature = 0.7, | |
}; | |
// Set-up the prompt configuration and adding the AI Request Settings | |
var promptConfig = new PromptTemplateConfig(); | |
promptConfig.ModelSettings.Add(aiRequestSettings); | |
var promptTemplate = new PromptTemplate( | |
skPrompt, | |
promptConfig, | |
kernel | |
); | |
var functionConfig = new SemanticFunctionConfig(promptConfig, promptTemplate); | |
var guessPlugin = kernel.RegisterSemanticFunction("GuessPlugin", "GetRiddle", functionConfig); | |
// Set-up the context (SKContext) | |
var context = kernel.CreateNewContext(); | |
context.Variables.Add("history", riddleHistory); | |
var result = await guessPlugin.InvokeAsync(context); | |
var riddleObject = JsonSerializer.Deserialize<Riddle>(result.GetValue<string>().Trim()); | |
riddleHistory = $"{riddleHistory}, {result.GetValue<string>().Trim()}"; | |
return riddleObject; | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception(ex.Message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment