Skip to content

Instantly share code, notes, and snippets.

@arafattehsin
Last active October 26, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arafattehsin/62d51e68122983c23486bf5d75b3731f to your computer and use it in GitHub Desktop.
Save arafattehsin/62d51e68122983c23486bf5d75b3731f to your computer and use it in GitHub Desktop.
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