-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
M.E.AI - Add support for string JsonElement parsing to primitive #1
base: main
Are you sure you want to change the base?
Conversation
I'm not clear on what this is trying to fix. Can you elaborate on the scenario? cc: @eiriktsarpalis |
src/Libraries/Microsoft.Extensions.AI/Functions/AIFunctionFactory.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/JsonStringBooleanConverter.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/JsonStringBooleanConverter.cs
Outdated
Show resolved
Hide resolved
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.
The reported JSON schema for custom converters is always true
. Please also update the AIJsonUtilities.CreateSchema
method so that the schema returned when this converter is encountered matches that of boolean values, i.e. { "type" : "boolean" }
This can be done by adding a check in the TransformSchemaNode
delegate:
@stephentoub Added more context to the PR description. |
const int StackallocCharThreshold = StackallocByteThreshold / 2; | ||
|
||
int bufferLength = GetValueLength(ref reader); | ||
char[]? rentedBuffer = null; |
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.
Valid boolean values are at most 5 characters long. You can avoid pooling buffers if you just check the length of the input before stack allocating a buffer of length 5.
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.
I don't know if we care, but if there was whitespace padding around the true/false, it could be longer than 5 chars. bool.Parse trims and thus will happily successfully parse e.g. " true ".
Thanks. It became much clearer when the PR changed to just be about the boolean converter. |
Inspired in the change for Semantic Kernel
This change covers the lack of a property on
jsonSerializationOptions.BooleanHandler = JsonBooleanHandling.AllowReadingFromString
which allows SLMs to call functions when the JsonElement is provided as of kind ==string
when converting to booleanAIFunctions
arguments.This change improves function calling experience when using local models that send JSON string argument values (i.e:
"brightness": "20"
or"switch": "true"
) instead of the expected JSON type ("brightness": 20
,"switch": true
) for calling functions even when the schema is correctly providing the expected type.i.e: Ollama running a Llama 3.2 SLM.
This pattern also follows the current behavior of the
JsonSerializerOptions
currently being used by theM.E.AI
JsonSerializerDefaults.Web
.That by default converts
string
as numbers, similar would be expected for booleans.https://github.com/dotnet/extensions/blob/c08e5ac737d0830ff83c1c5ae8b084e6fce2b538/src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs#L31