Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RogerBarreto
Copy link
Owner

@RogerBarreto RogerBarreto commented Nov 22, 2024

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 boolean AIFunctions 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.

image

This pattern also follows the current behavior of the JsonSerializerOptions currently being used by the M.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

JsonSerializerOptions options = new(JsonSerializerDefaults.Web)

@stephentoub
Copy link

I'm not clear on what this is trying to fix. Can you elaborate on the scenario?

cc: @eiriktsarpalis

Copy link

@eiriktsarpalis eiriktsarpalis left a 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:

https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.cs#L234

@RogerBarreto
Copy link
Owner Author

I'm not clear on what this is trying to fix. Can you elaborate on the scenario?

@stephentoub Added more context to the PR description.

const int StackallocCharThreshold = StackallocByteThreshold / 2;

int bufferLength = GetValueLength(ref reader);
char[]? rentedBuffer = null;

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.

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 ".

@stephentoub
Copy link

I'm not clear on what this is trying to fix. Can you elaborate on the scenario?

@stephentoub Added more context to the PR description.

Thanks. It became much clearer when the PR changed to just be about the boolean converter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants