-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Added M.E.A.I. AIFunction Generation support #24
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
Changes from all commits
e101bac
e289da0
315af73
36cf256
904bbf0
6bfd9b7
1ebee3f
46a569b
d245705
8071875
234c61e
d8a5f36
4075971
28da355
8830a3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using CSharpToJsonSchema.Generators.Models; | ||
|
|
||
| namespace CSharpToJsonSchema.Generators; | ||
|
|
||
| internal static partial class Sources | ||
| { | ||
| public static string GenerateMeaiFunctionToolForMethods(InterfaceData @interface) | ||
| { | ||
| if(@interface.Methods.Count == 0 || [email protected]) | ||
| return string.Empty; | ||
| var extensionsClassName = @interface.Name; | ||
|
|
||
| return @$" | ||
| #nullable enable | ||
|
|
||
| namespace {@interface.Namespace} | ||
| {{ | ||
| public partial class {extensionsClassName} | ||
| {{ | ||
| public static implicit operator global::System.Collections.Generic.List<global::Microsoft.Extensions.AI.AITool>? ({@interface.Namespace}.{extensionsClassName} tools) | ||
| {{ | ||
| return tools.AsMeaiTools(); | ||
| }} | ||
|
|
||
| public global::System.Collections.Generic.List<global::Microsoft.Extensions.AI.AITool> AsMeaiTools() | ||
| {{ | ||
| var lst = new global::System.Collections.Generic.List<global::Microsoft.Extensions.AI.AITool>(); | ||
| var tools = this.AsTools(); | ||
| var calls = this.AsCalls(); | ||
| foreach (var tool in tools) | ||
| {{ | ||
| var call = calls[tool.Name]; | ||
| lst.Add(new global::CSharpToJsonSchema.MeaiFunction(tool, call)); | ||
| }} | ||
| return lst; | ||
| }} | ||
| }} | ||
| }}"; | ||
| } | ||
|
|
||
| public static string GenerateMeaiFunctionToolForInterface(InterfaceData @interface) | ||
| { | ||
| if([email protected]) | ||
| return string.Empty; | ||
| var extensionsClassName = @interface.Name.Substring(startIndex: 1) + "Extensions"; | ||
|
|
||
| return @$" | ||
| #nullable enable | ||
|
|
||
| namespace {@interface.Namespace} | ||
| {{ | ||
| public partial class {extensionsClassName} | ||
| {{ | ||
| public static global::System.Collections.Generic.List<global::Microsoft.Extensions.AI.AITool> AsMeaiTools(this {@interface.Name} service) | ||
| {{ | ||
| var lst = new global::System.Collections.Generic.List<global::Microsoft.Extensions.AI.AITool>(); | ||
| var tools = service.AsTools(); | ||
| var calls = service.AsCalls(); | ||
| foreach (var tool in tools) | ||
| {{ | ||
| var call = calls[tool.Name]; | ||
| lst.Add(new global::CSharpToJsonSchema.MeaiFunction(tool, call)); | ||
| }} | ||
| return lst; | ||
| }} | ||
| }} | ||
| }}"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| using System.Collections.ObjectModel; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace CSharpToJsonSchema; | ||
|
|
||
| /// <summary> | ||
| /// Represents a function that wraps a tool and provides functionality for executing the tool with specified arguments. | ||
| /// </summary> | ||
| public partial class MeaiFunction : AIFunction | ||
| { | ||
| private readonly Tool _tool; | ||
| private readonly Func<string, CancellationToken, Task<string>> _call; | ||
|
|
||
| private JsonElement _jsonSchema; | ||
|
|
||
| /// <summary> | ||
| /// Gets the JSON schema representing the parameters of the tool. | ||
| /// </summary> | ||
| public override JsonElement JsonSchema => _jsonSchema; | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the tool. | ||
| /// </summary> | ||
| public override string Name => _tool.Name; | ||
|
|
||
| /// <summary> | ||
| /// Gets the description of the tool. | ||
| /// </summary> | ||
| public override string Description => _tool.Description; | ||
|
|
||
| /// <summary> | ||
| /// Gets additional properties associated with the tool. | ||
| /// </summary> | ||
| public override IReadOnlyDictionary<string, object?> AdditionalProperties => new ReadOnlyDictionary<string, object?>(_tool.AdditionalProperties); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MeaiFunction"/> class. | ||
| /// </summary> | ||
| /// <param name="tool">The tool associated with this function.</param> | ||
| /// <param name="call">The function to execute the tool with input arguments.</param> | ||
| public MeaiFunction(Tool tool, Func<string, CancellationToken, Task<string>> call) | ||
| { | ||
| this._tool = tool; | ||
| this._call = call; | ||
|
|
||
| _jsonSchema = JsonSerializer.Deserialize<JsonElement>( | ||
| JsonSerializer.Serialize(tool.Parameters, OpenApiSchemaJsonContext.Default.OpenApiSchema), | ||
| OpenApiSchemaJsonContext.Default.JsonElement); | ||
|
|
||
| if (tool.Strict == true) | ||
| { | ||
| tool.AdditionalProperties.Add("Strict", true); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Invokes the tool with the given arguments asynchronously. | ||
| /// </summary> | ||
| /// <param name="arguments">The arguments to pass to the tool.</param> | ||
| /// <param name="cancellationToken">A cancellation token to cancel the operation, if needed.</param> | ||
| /// <returns>The result of the tool's execution as a deserialized object.</returns> | ||
| protected override async Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var json = GetArgsString(arguments); | ||
|
|
||
| var call = await _call(json, cancellationToken); | ||
|
|
||
| return JsonSerializer.Deserialize(call, OpenApiSchemaJsonContext.Default.JsonElement); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a collection of arguments into a JSON string. | ||
| /// </summary> | ||
| /// <param name="arguments">The arguments to be converted into a JSON string.</param> | ||
| /// <returns>A JSON string representation of the arguments.</returns> | ||
| private string GetArgsString(IEnumerable<KeyValuePair<string, object?>> arguments) | ||
| { | ||
| var jsonObject = new JsonObject(); | ||
|
|
||
| foreach (var args in arguments) | ||
| { | ||
| if (args.Value is JsonElement element) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.Array) | ||
| jsonObject[args.Key] = JsonArray.Create(element); | ||
| else if (element.ValueKind == JsonValueKind.Object) | ||
| jsonObject[args.Key] = JsonObject.Create(element); | ||
| } | ||
| else if (args.Value is JsonNode node) | ||
| { | ||
| jsonObject[args.Key] = node; | ||
| } | ||
| } | ||
|
|
||
| return jsonObject.ToJsonString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,10 @@ namespace CSharpToJsonSchema; | |||||||||||||||||
| [JsonSerializable(typeof(OpenApiSchema))] | ||||||||||||||||||
| [JsonSerializable(typeof(IDictionary<string, OpenApiSchema>))] | ||||||||||||||||||
| [JsonSerializable(typeof(IDictionary<string, string>))] | ||||||||||||||||||
| [JsonSerializable(typeof(Tool))] | ||||||||||||||||||
| [JsonSerializable(typeof(List<Tool>))] | ||||||||||||||||||
|
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing import for Tool class The code adds JSON serialization for 1
using System.Text.Json.Serialization;
2
+using System.Collections.Generic;
+// Add the appropriate namespace for the Tool class📝 Committable suggestion
Suggested change
|
||||||||||||||||||
| [JsonSerializable(typeof(JsonElement))] | ||||||||||||||||||
| [JsonSourceGenerationOptions(NumberHandling = JsonNumberHandling.Strict, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,WriteIndented = false)] | ||||||||||||||||||
| public partial class OpenApiSchemaJsonContext:JsonSerializerContext | ||||||||||||||||||
| { | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
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.
Handle non-array/object
JsonElementcases.If the
JsonElementrepresents a string, number, or boolean, it will be ignored. Consider adding a fallback to capture these value kinds:if (element.ValueKind == JsonValueKind.Array) jsonObject[args.Key] = JsonArray.Create(element); else if (element.ValueKind == JsonValueKind.Object) jsonObject[args.Key] = JsonObject.Create(element); +else + jsonObject[args.Key] = element.GetRawText();📝 Committable suggestion