-
-
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 11 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,61 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace CSharpToJsonSchema; | ||
|
|
||
| public partial class MeaiFunction : AIFunction | ||
| { | ||
| private readonly Tool _tool; | ||
| private readonly Func<string, CancellationToken, Task<string>> _call; | ||
|
|
||
| private JsonElement _jsonSchema; | ||
| public override JsonElement JsonSchema => _jsonSchema; | ||
| public override string Name => _tool.Name; | ||
| public override string Description => _tool.Description; | ||
|
|
||
| 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); | ||
| } | ||
| protected override async Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| var json = GetArgsString(arguments); | ||
|
|
||
| var call = await _call(json, cancellationToken); | ||
|
|
||
| return JsonSerializer.Deserialize(call, OpenApiSchemaJsonContext.Default.JsonElement); | ||
| }catch(Exception e) | ||
| { | ||
| return e.Message; | ||
| } | ||
| } | ||
|
|
||
| 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 | ||||||||||||||||||
| { | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -110,39 +110,49 @@ public static OpenApiSchema ConvertToSchema<T>(JsonSerializerOptions? jsonOption | |||||
| public static OpenApiSchema ConvertToSchema(JsonTypeInfo type, string descriptionString) | ||||||
| { | ||||||
| var typeInfo = type; | ||||||
|
|
||||||
| var dics = JsonSerializer.Deserialize(descriptionString,OpenApiSchemaJsonContext.Default.IDictionaryStringString); | ||||||
|
|
||||||
| var dics = JsonSerializer.Deserialize(descriptionString, | ||||||
| OpenApiSchemaJsonContext.Default.IDictionaryStringString); | ||||||
| List<string> required = new List<string>(); | ||||||
| var x = ConvertToCompatibleSchemaSubset(typeInfo.GetJsonSchemaAsNode(exporterOptions:new JsonSchemaExporterOptions() | ||||||
| { | ||||||
| TransformSchemaNode = (a, b) => | ||||||
| var x = ConvertToCompatibleSchemaSubset(typeInfo.GetJsonSchemaAsNode( | ||||||
| exporterOptions: new JsonSchemaExporterOptions() | ||||||
| { | ||||||
| if (a.PropertyInfo == null) | ||||||
| return b; | ||||||
| var propName = ToCamelCase(a.PropertyInfo.Name); | ||||||
| if (dics.ContainsKey(propName)) | ||||||
| TransformSchemaNode = (a, b) => | ||||||
| { | ||||||
| b["description"] = dics[propName]; | ||||||
| } | ||||||
| return b; | ||||||
| }, | ||||||
| })); | ||||||
| if (a.TypeInfo.Type.IsEnum) | ||||||
| { | ||||||
| b["type"] = "string"; | ||||||
| } | ||||||
|
|
||||||
| if (a.PropertyInfo == null) | ||||||
| return b; | ||||||
| var propName = ToCamelCase(a.PropertyInfo.Name); | ||||||
| if (dics.ContainsKey(propName)) | ||||||
| { | ||||||
| b["description"] = dics[propName]; | ||||||
| } | ||||||
|
|
||||||
| return b; | ||||||
| }, | ||||||
| })); | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| foreach (var re in x.Properties) | ||||||
| { | ||||||
| required.Add(re.Key); | ||||||
| } | ||||||
|
|
||||||
| var mainDescription =x.Description ?? dics["mainFunction_Desc"]; | ||||||
|
|
||||||
| var mainDescription = x.Description ?? dics["mainFunction_Desc"]; | ||||||
|
||||||
| var mainDescription = x.Description ?? dics["mainFunction_Desc"]; | |
| var mainDescription = x.Description ?? (dics.TryGetValue("mainFunction_Desc", out var desc) ? desc : ""); |
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.
🛠️ Refactor suggestion
Consider unifying or clarifying the return type of
InvokeCoreAsync.Currently, when the call succeeds, the method returns a
JsonElement, but in the catch block, it returns a string holding the exception message. Mixing these return types (both raw JSON and a string) might create ambiguity for consumers of this method if they expect a consistent data structure. Additionally, returning the full exception message could expose internal information.📝 Committable suggestion