ช่วงลองทำ Side Project เกี่ยวกับเจ้า Semantic Kernel ทำไว [KernelFunction] เตรียม Function การคำนวณให้ LLM มันปั๊น และส่งค่ามาให้ถูกเท่านั้นพอ ทำมาหลาย [KernelFunction] แต่อันนี้ดัน Error ซะงั้น
ถ้าดู Error จะขึ้นแนวๆ System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.properties.month.type | LineNumber: 0 | BytePositionInLine: 187. ถ้าตัว Trace เต็มๆตามนี้ครับ
System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.properties.month.type | LineNumber: 0 | BytePositionInLine: 187. ---> System.InvalidOperationException: Cannot get the value of a token type 'StartArray' as a string. at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType) at System.Text.Json.Utf8JsonReader.GetString() at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue) at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TDictionary& value) at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue) at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state) --- End of inner exception stack trace --- at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex) at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount) at System.Text.Json.JsonSerializer.Deserialize[TValue](JsonElement element, JsonSerializerOptions options) at OllamaSharp.MicrosoftAi.AbstractionMapper.ToOllamaSharpTool(AIFunction function) at OllamaSharp.MicrosoftAi.AbstractionMapper.ToOllamaSharpTool(AITool tool) at System.Linq.Enumerable.ListSelectIterator`2.MoveNext() at System.Linq.Enumerable.IEnumerableWhereIterator`1.MoveNext() at System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter`2.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed) at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo) at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options) at OllamaSharp.OllamaApiClient.ChatAsync(ChatRequest request, CancellationToken cancellationToken)+MoveNext() at OllamaSharp.OllamaApiClient.ChatAsync(ChatRequest request, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult() at OllamaSharp.IAsyncEnumerableExtensions.StreamToEndAsync[Tin,Tout](IAsyncEnumerable`1 stream, IAppender`2 appender, Action`1 itemCallback) at OllamaSharp.IAsyncEnumerableExtensions.StreamToEndAsync[Tin,Tout](IAsyncEnumerable`1 stream, IAppender`2 appender, Action`1 itemCallback) at OllamaSharp.OllamaApiClient.Microsoft.Extensions.AI.IChatClient.GetResponseAsync(IEnumerable`1 messages, ChatOptions options, CancellationToken cancellationToken) at Microsoft.Extensions.AI.FunctionInvokingChatClient.GetResponseAsync(IEnumerable`1 messages, ChatOptions options, CancellationToken cancellationToken) at Microsoft.SemanticKernel.ChatCompletion.ChatClientChatCompletionService.GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken) at Microsoft.SemanticKernel.ChatCompletion.ChatCompletionServiceExtensions.GetChatMessageContentAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken) at FinancialChatBotAPI.Hubs.ChatHub.SendMessage(String message) in /src/FinancialChatBotAPI/Hubs/ChatHub.cs:line 94
ตอนแรกก็งงไปเกือบวัน พังเพราะอะไร แต่หลังจากกินข้าว และลองมาไล่อีกที เจ้า Kernel Function ของเรามันไม่เคยรับ Nullable เลยนี่หว่า พวก int? เลยลองไปดู Sample จาก
- https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithAgents/Step04_KernelFunctionStrategies.cs
- https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithAgents/Step08_AgentAsKernelFunction.cs
อ้าวมันไม่มีเหมือนกันนะ ตัวอย่างมันไม่มีพวก Nullable เลยนี่หว่า พวก int? เลยลองเอาออก และลองใช้งานดู
- ของเดิม
[KernelFunction] [Description( "Get prize 2 digit history from the LottoAPI with optional month and day filters")] public async Task<string> GetPrize2DigitHistoryAsync( [Description("Optional month filter (1-12)")] int? month = null, [Description("Optional day filter (1-31)")] int? day = null) {
- เอา nullable ออก
[KernelFunction] [Description( "Get prize 2 digit history from the LottoAPI with optional month and day filters")] public async Task<string> GetPrize2DigitHistoryAsync( [Description("Optional month filter (1-12)")] int month, [Description("Optional day filter (1-31)")] int day) {
ทำงานได้ซะงั้น ไม่มี Error แต่ต้องพิมพ์ Prompt ให้มันชัดเจนหน่อยอย่าง "ข้อมูล Lotto ย้อนหลังเดือน 9 วันที่ 1" ให้มันเข้าทำงานได้ หรือ เราควร Design เป็นส่ง String ให้ [KernelFunction] จะปลอดภัยกว่าป่าวนะ แล้วให้ไปแปลง + ดักข้อมูลกันเองอีกที

จบไปอีกวันครับ เดี๋ยวต้องลองไปหาเพิ่งว่ามีคนแจ้งเคสไปยัง ถ้าไม่มีเราอาจจะได้เป็นคนเจิมครับ ^__^
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.