Yesterday I talked about the Smart Paste UI component which should help to automatically fill out a form using data from the user's clipboard. Although the feature worked, the result was weird as an 'of type string' was added to the filled in fields.
Let’s debug the component to understand what is going (wr)on(g).
We start by creating a subclass of type SmartPasteInference
:
class CustomSmartPasteInference : SmartPasteInference | |
{ | |
public override ChatParameters BuildPrompt(SmartPasteRequestData data) | |
{ | |
var prompt = base.BuildPrompt(data); | |
return prompt; | |
} | |
} |
We register this class in the DI container before the AddSmartComponents()
:
builder.Services.AddSingleton<SmartPasteInference, CustomSmartPasteInference>(); | |
builder.Services.AddSmartComponents() | |
.WithInferenceBackend<OpenAIInferenceBackend>(); |
Add a breakpoint to the BuildPrompt
method to inspect the default prompt and parameters. Run the application in debug mode and hit the 'Paste from clipboard button'
Once the breakpoint is hit, we can see the debugger values:
If we drill down in the Messages property, we see the following 2 messages:
That already explains where the ‘of type string’ is coming from. It seems that Llama3 handles these values literally. Let’s try to update the generated prompt to get a better result.
I took the original source code and tweaked it a little bit to omit the type information:
class CustomSmartPasteInference : SmartPasteInference | |
{ | |
public override ChatParameters BuildPrompt(SmartPasteRequestData data) | |
{ | |
string text = "\nCurrent date: " + DateTime.Today.ToString("D", CultureInfo.InvariantCulture) + "\n\nEach response line matches the following format:\nFIELD identifier^^^value\n\nGive a response with the following lines only, with values inferred from USER_DATA:\n" + ToSimplifiedOutputExamples(data.FormFields) + "\nEND_RESPONSE\n\nDo not explain how the values were determined.\nFor fields without any corresponding information in USER_DATA, use value value NO_DATA."; | |
string text2 = "\nUSER_DATA: " + data.ClipboardContents + "\n"; | |
var prompt= new ChatParameters | |
{ | |
Messages = new List<ChatMessage>(2) | |
{ | |
new ChatMessage(ChatMessageRole.System, text), | |
new ChatMessage(ChatMessageRole.User, text2) | |
}, | |
Temperature = 0f, | |
TopP = 1f, | |
MaxTokens = 2000, | |
FrequencyPenalty = 0.1f, | |
PresencePenalty = 0f, | |
StopSequences = new List<string>(1) { "END_RESPONSE" } | |
}; | |
return prompt; | |
} | |
private static string ToSimplifiedOutputExamples(FormField[] fields) | |
{ | |
StringBuilder stringBuilder = new StringBuilder(); | |
foreach (FormField formField in fields) | |
{ | |
stringBuilder.AppendLine(); | |
stringBuilder.Append("FIELD " + formField.Identifier + "^^^"); | |
if (!string.IsNullOrEmpty(formField.Description)) | |
{ | |
stringBuilder.Append("The " + formField.Description); | |
} | |
string[] allowedValues = formField.AllowedValues; | |
if (allowedValues != null && allowedValues.Length > 0) | |
{ | |
stringBuilder.Append(" (multiple choice, with allowed values: "); | |
bool flag = true; | |
allowedValues = formField.AllowedValues; | |
foreach (string text in allowedValues) | |
{ | |
if (flag) | |
{ | |
flag = false; | |
} | |
else | |
{ | |
stringBuilder.Append(","); | |
} | |
stringBuilder.Append("\"" + text + "\""); | |
} | |
stringBuilder.Append(")"); | |
} | |
} | |
return stringBuilder.ToString(); | |
} | |
} |
This time our prompt looks like this;
And this is how the result looks like if we paste the following bug description:
I've found a pretty serious issue where the application crashes whenever you try to save your profile. It doesn't matter what browser or operating system you're using, it happens every time. Basically, you just log in, go to the profile page, fill out the form, and hit save. Instead of saving your profile like it should, the whole application crashes and you get this "500 Internal Server Error" message. This is a high priority issue because it prevents users from updating their profiles.
Much better!