-
-
Notifications
You must be signed in to change notification settings - Fork 2
Fix: Function Tools AOT bugs #26
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
6243888
972b9c5
b49d3d2
e30dd35
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,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <PublishAot>true</PublishAot> | ||
| <LangVersion>latest</LangVersion> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\libs\CSharpToJsonSchema.Generators\CSharpToJsonSchema.Generators.csproj" OutputItemType="analyzer" /> | ||
| <ProjectReference Include="..\..\libs\CSharpToJsonSchema\CSharpToJsonSchema.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" /> | ||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25114.11" /> | ||
| <PackageReference Include="OpenAI" Version="2.2.0-beta.1" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // See https://aka.ms/new-console-template for more information | ||
|
|
||
| using System.ClientModel; | ||
| using CSharpToJsonSchema.MeaiTests.Services; | ||
| using Microsoft.Extensions.AI; | ||
| using OpenAI; | ||
|
|
||
| var key = Environment.GetEnvironmentVariable("OPEN_AI_APIKEY",EnvironmentVariableTarget.User); | ||
| if (string.IsNullOrWhiteSpace(key)) | ||
| return; | ||
| var prompt = "how does student john doe in senior grade is doing this year, enrollment start 01-01-2024 to 01-01-2025?"; | ||
|
|
||
| var client = new OpenAIClient(new ApiKeyCredential(key)); | ||
|
|
||
| Microsoft.Extensions.AI.OpenAIChatClient openAiClient = new OpenAIChatClient(client.GetChatClient("gpt-4o-mini")); | ||
|
|
||
| var chatClient = new FunctionInvokingChatClient(openAiClient); | ||
| var chatOptions = new ChatOptions(); | ||
|
|
||
| var service = new StudentRecordService(); | ||
|
|
||
| var tools = new Tools([service.GetStudentRecordAsync]); | ||
| chatOptions.Tools = tools.AsMeaiTools(); | ||
| var message = new ChatMessage(ChatRole.User, prompt); | ||
| var response = await chatClient.GetResponseAsync(message,options:chatOptions).ConfigureAwait(false); | ||
|
|
||
| Console.WriteLine(response.Choices.LastOrDefault().Text); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,38 @@ | ||||||
| using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; | ||||||
|
|
||||||
|
|
||||||
| namespace CSharpToJsonSchema.MeaiTests.Services; | ||||||
|
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. 🛠️ Refactor suggestion Namespace mismatch with file location The namespace -namespace CSharpToJsonSchema.MeaiTests.Services;
+namespace CSharpToJsonSchema.AotConsole.Services;📝 Committable suggestion
Suggested change
|
||||||
|
|
||||||
| public class GetAuthorBook | ||||||
| { | ||||||
| public string Title { get; set; } = string.Empty; | ||||||
| public string Description { get; set; } = string.Empty; | ||||||
| } | ||||||
|
|
||||||
| [GenerateJsonSchema(MeaiFunctionTool = true)] | ||||||
| public interface IBookStoreService | ||||||
| { | ||||||
| [Description("Get books written by some author")] | ||||||
| public Task<List<GetAuthorBook>> GetAuthorBooksAsync([Description("Author name")] string authorName, CancellationToken cancellationToken = default); | ||||||
|
|
||||||
| [Description("Get book page content")] | ||||||
| public Task<string> GetBookPageContentAsync([Description("Book Name")] string bookName, [Description("Book Page Number")] int bookPageNumber, CancellationToken cancellationToken = default); | ||||||
|
|
||||||
| } | ||||||
| public class BookStoreService : IBookStoreService | ||||||
| { | ||||||
| public Task<List<GetAuthorBook>> GetAuthorBooksAsync(string authorName, CancellationToken cancellationToken = default) | ||||||
| { | ||||||
| return Task.FromResult(new List<GetAuthorBook>([ | ||||||
| new GetAuthorBook | ||||||
| { Title = "Five point someone", Description = "This book is about 3 college friends" }, | ||||||
| new GetAuthorBook | ||||||
| { Title = "Two States", Description = "This book is about intercast marriage in India" } | ||||||
| ])); | ||||||
| } | ||||||
|
|
||||||
| public Task<string> GetBookPageContentAsync(string bookName, int bookPageNumber, CancellationToken cancellationToken = default) | ||||||
| { | ||||||
| return Task.FromResult("this is a cool weather out there, and I am stuck at home."); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||||||||||
| namespace CSharpToJsonSchema.MeaiTests.Services; | ||||||||||||||
| using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; | ||||||||||||||
|
|
||||||||||||||
| public class StudentRecordService | ||||||||||||||
| { | ||||||||||||||
| [System.ComponentModel.Description("Get student record for the year")] | ||||||||||||||
| [FunctionTool(MeaiFunctionTool = true)] | ||||||||||||||
|
|
||||||||||||||
|
Comment on lines
+7
to
+8
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 using statement for FunctionTool attribute. The code uses the namespace CSharpToJsonSchema.MeaiTests.Services;
using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
+using CSharpToJsonSchema; // Add this for FunctionTool attribute📝 Committable suggestion
Suggested change
|
||||||||||||||
| public async Task<StudentRecord> GetStudentRecordAsync(QueryStudentRecordRequest query, CancellationToken cancellationToken = default) | ||||||||||||||
| { | ||||||||||||||
| return new StudentRecord | ||||||||||||||
| { | ||||||||||||||
| StudentId = "12345", | ||||||||||||||
| FullName = query.FullName, | ||||||||||||||
| Level = StudentRecord.GradeLevel.Senior, | ||||||||||||||
| EnrolledCourses = new List<string> { "Math 101", "Physics 202", "History 303" }, | ||||||||||||||
| Grades = new Dictionary<string, double> | ||||||||||||||
| { | ||||||||||||||
| { "Math 101", 3.5 }, | ||||||||||||||
| { "Physics 202", 3.8 }, | ||||||||||||||
| { "History 303", 3.9 } | ||||||||||||||
| }, | ||||||||||||||
| EnrollmentDate = new DateTime(2020, 9, 1), | ||||||||||||||
| IsActive = true | ||||||||||||||
| }; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public class StudentRecord | ||||||||||||||
| { | ||||||||||||||
| public enum GradeLevel | ||||||||||||||
| { | ||||||||||||||
| Freshman, | ||||||||||||||
| Sophomore, | ||||||||||||||
| Junior, | ||||||||||||||
| Senior, | ||||||||||||||
| Graduate | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public string StudentId { get; set; } = string.Empty; | ||||||||||||||
| public string FullName { get; set; } = string.Empty; | ||||||||||||||
| public GradeLevel Level { get; set; } = GradeLevel.Freshman; | ||||||||||||||
| public List<string> EnrolledCourses { get; set; } = new List<string>(); | ||||||||||||||
| public Dictionary<string, double> Grades { get; set; } = new Dictionary<string, double>(); | ||||||||||||||
| public DateTime EnrollmentDate { get; set; } = DateTime.Now; | ||||||||||||||
| public bool IsActive { get; set; } = true; | ||||||||||||||
|
|
||||||||||||||
| public double CalculateGPA() | ||||||||||||||
| { | ||||||||||||||
| if (Grades.Count == 0) return 0.0; | ||||||||||||||
| return Grades.Values.Average(); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| [Description("Request class containing filters for querying student records.")] | ||||||||||||||
| public class QueryStudentRecordRequest | ||||||||||||||
| { | ||||||||||||||
| [Description("The student's full name.")] | ||||||||||||||
| public string FullName { get; set; } = string.Empty; | ||||||||||||||
|
|
||||||||||||||
| [Description("Grade filters for querying specific grades, e.g., Freshman or Senior.")] | ||||||||||||||
| public List<StudentRecord.GradeLevel> GradeFilters { get; set; } = new(); | ||||||||||||||
|
|
||||||||||||||
| [Description("The start date for the enrollment date range. ISO 8601 standard date")] | ||||||||||||||
| public DateTime EnrollmentStartDate { get; set; } | ||||||||||||||
|
|
||||||||||||||
| [Description("The end date for the enrollment date range. ISO 8601 standard date")] | ||||||||||||||
| public DateTime EnrollmentEndDate { get; set; } | ||||||||||||||
|
|
||||||||||||||
| [Description("The flag indicating whether to include only active students.")] | ||||||||||||||
| public bool? IsActive { get; set; } = true; | ||||||||||||||
| } | ||||||||||||||
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
Add error handling for API calls.
The API call lacks try-catch error handling. Network requests can fail for various reasons, and the application should gracefully handle these failures.
📝 Committable suggestion