You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using System;using System.Text.Json;using Microsoft.SemanticKernel;using Microsoft.SemanticKernel.KernelExtensions;// Load the kernelIKernelkernel=new KernelBuilder().Build();// Choose an AI model
kernel.Config.AddOpenAICompletionBackend("dv","text-davinci-003","...API KEY...");// Create a semantic functionvarfixit= kernel.CreateSemanticFunction(@"I tried parsing this {{$format}} string and got an exception: {{$error}}.Fix the {{$format}} syntax to address the error.Value to fix: {{$input}}",
maxTokens:1024);// Setup the contextvarcontext= kernel.CreateNewContext();
context["format"]="JSON";
context["input"]="{ 'field': 'value";// Show the original broken JSON
Console.WriteLine($"Original value: {context}");try{// Try to parse the JSONvardata= JsonSerializer.Deserialize<dynamic>(context["input"]);}catch(Exceptione){// Show the error
Console.WriteLine($"Exception: {e.Message}");// Capture the error message and pass it to AI
context["error"]= e.Message;// Run the semantic function, ask AI to fix the problemvarresult=await fixit.InvokeAsync(context);// Show the new JSON, fixed by AI
Console.WriteLine(result.Result);}
Output:
Original value: { 'field': 'value
Exception: ''' is an invalid start of a property name. Expected a '"'. Path: $ | LineNumber: 0 | BytePositionInLine: 2.
' }
Corrected value: { "field": "value" }
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Output:
Beta Was this translation helpful? Give feedback.
All reactions