Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Samples/Senparc.AI.Samples.Consoles/SampleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -118,5 +119,17 @@ static void PrintOptions(string[] options, int currentSelection, int cursorTop)
Console.WriteLine(" " + options[i].PadRight(Console.WindowWidth - options[i].Length - 2));
}
}

/// <summary>
/// Print alert message
/// </summary>
/// <param name="message"></param>
public static void PrintNote(string message)
{
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\t\t >>> " + message);
Console.ForegroundColor = oldColor;
}
}
}
4 changes: 2 additions & 2 deletions Samples/Senparc.AI.Samples.Consoles/Samples/ChatSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ 输入 exit 退出。
{
Console.WriteLine();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"\t\t >>> 本次请求执行了 function-calling:{result.GetLastFunctionResultContent().FunctionResultContent?.FunctionName} <<<");

SampleHelper.PrintNote($"本次请求执行了 function-calling:{result.GetLastFunctionResultContent().FunctionResultContent?.FunctionName}");
}

//复原颜色
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.SemanticKernel;
using Senparc.AI.Samples.Consoles;
using System;
using System.ComponentModel;

Expand All @@ -10,15 +11,17 @@ public class NowPlugin
[KernelFunction("GetCurrentNowTime"), Description("获取当前时间")]
public static async Task<string> GetCurrentTime([Description("C# 时间格式")] string format)
{
Console.WriteLine("[运行 GetCurrentTime] function-calling]");
return DateTime.Now.ToString(format);
SampleHelper.PrintNote("[运行 GetCurrentTime] function-calling");
Console.WriteLine();
return DateTime.Now.ToString(format);
}


[KernelFunction("AddTime"), Description("修改时间")]
public static DateTime AddTime([Description("需要修改的时间,C# 的时间格式")] DateTime? dateTime, int days, double seconds)
{
Console.WriteLine($"[运行 AddTime] function-calling {dateTime},{days},{seconds}]");
return (dateTime??DateTime.Now).AddSeconds(seconds).AddDays(days);
SampleHelper.PrintNote("[运行 AddTime] function-calling");
Console.WriteLine();
return (dateTime ?? DateTime.Now).AddSeconds(seconds).AddDays(days);
}
}
22 changes: 18 additions & 4 deletions src/Senparc.AI.Kernel/Entities/SenparcAiResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class SenparcAiResult : IAiResult

public virtual IWantToRun IWantToRun { get; set; }

public FunctionResultContent LastFunctionResultContent { get; private set; }

public SenparcAiResult(IWantToRun iwantToRun, string? inputContent)
{
IWantToRun = iwantToRun;
Expand All @@ -53,6 +55,17 @@ public SenparcAiResult(IWantToRun iwantToRun, IAiContext inputContext)
InputContext = inputContext;
}

/// <summary>
/// Set last response FunctionResultContent
/// </summary>
/// <param name="functionResultContent"></param>
public void SetLastFunctionResultContent(FunctionResultContent functionResultContent = null)
{
LastFunctionResultContent = functionResultContent ??
((IWantToRun.StoredAiArguments.Context["history"] as ChatHistory)?
.LastOrDefault()?.Items?.LastOrDefault() as Microsoft.SemanticKernel.FunctionResultContent);
}

/// <summary>
/// Get last response FunctionResultContent
/// </summary>
Expand All @@ -65,9 +78,9 @@ public SenparcAiResult(IWantToRun iwantToRun, IAiContext inputContext)

//var msgResult = (result.IWantToRun.StoredAiArguments.Context["history"] as ChatHistory).Last().Items.FirstOrDefault(z => z is Microsoft.SemanticKernel.FunctionResultContent);

var functionResultContent =
(IWantToRun.StoredAiArguments.Context["history"] as ChatHistory)
.Last().Items.LastOrDefault() as Microsoft.SemanticKernel.FunctionResultContent;
var functionResultContent = LastFunctionResultContent;
//(IWantToRun.StoredAiArguments.Context["history"] as ChatHistory)
//.Last().Items.LastOrDefault() as Microsoft.SemanticKernel.FunctionResultContent;

/*
{
Expand All @@ -91,7 +104,8 @@ public class SenparcAiResult<T> : SenparcAiResult, IAiResult
{
public T Result { get; set; }
public IAsyncEnumerable<StreamingKernelContent>? /*SKContext*/
StreamResult { get; set; }
StreamResult
{ get; set; }

public SenparcAiResult(IWantToRun iWwantToRun, string inputContent)
: base(iWwantToRun, inputContent)
Expand Down
32 changes: 19 additions & 13 deletions src/Senparc.AI.Kernel/Handlers/SemanticAiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using NetTopologySuite.Index.HPRtree;
using Senparc.AI.Entities;
using Senparc.AI.Entities.Keys;
using Senparc.AI.Interfaces;
Expand Down Expand Up @@ -168,16 +169,9 @@ public async Task<SenparcAiResult> ChatAsync(IWantToRun iWantToRun, string input
this.RemoveHistory(chatHistory, maxHistoryCount - 1);
}

var history = aiResult.IWantToRun.StoredAiArguments.Context[historyArgName] as ChatHistory;
if (history != null)
{
chatHistory.Add(history.Last());
}
else
{
//newHistory = newHistory + $"\n{humanId}: {input}\n{robotId}: {aiResult.OutputString}";
chatHistory.AddAssistantMessage(aiResult.OutputString);
}
aiResult.SetLastFunctionResultContent();
//newHistory = newHistory + $"\n{humanId}: {input}\n{robotId}: {aiResult.OutputString}";
chatHistory.AddAssistantMessage(aiResult.OutputString);

//记录对话历史(可选)
//request.SetStoredContext(historyArgName, newHistory);
Expand Down Expand Up @@ -227,14 +221,26 @@ public void RemoveHistory(ChatHistory chatHistory, int maxHistoryCount)
while (removeCount > 0)
{
var firstUser = chatHistory.First(z => z.Role == AuthorRole.User);
var firstAssistant = chatHistory.FirstOrDefault(z => z.Role == AuthorRole.Assistant);
var firstUserIndex = chatHistory.IndexOf(firstUser);

chatHistory.Remove(firstUser);
if (firstAssistant != null)

var removeList = chatHistory.Skip(firstUserIndex).TakeWhile(z => z.Role != AuthorRole.User).ToList();

//中间可能还有其他类型,或 tool

for (int i = 0; i < removeList.Count(); i++)
{
chatHistory.Remove(firstAssistant);
chatHistory.Remove(removeList[i]);
}


//var firstAssistant = chatHistory.FirstOrDefault(z => z.Role == AuthorRole.Assistant);
//if (firstAssistant != null)
//{
// chatHistory.Remove(firstAssistant);
//}

removeCount--;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Senparc.AI.Kernel/Senparc.AI.Kernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.23.3</Version>
<Version>0.23.4</Version>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<AssemblyName>Senparc.AI.Kernel</AssemblyName>
Expand Down Expand Up @@ -56,6 +56,7 @@
[2025-01-25] v0.22.1 fix output result for nonstreaming request
[2025-02-02] v0.23.0-beta1 Upgrade support for Ollama, finished test for DeepSeek-r1 and nomic-embed-text:v1.5
[2025-05-10] v0.23.3 Support result.GetLastFunctionResultContent() function
[2025-05-10] v0.23.4 Upgrade for Chat history limit
</PackageReleaseNotes>
<RepositoryUrl>https://github.com/Senparc/Senparc.AI.Kernel</RepositoryUrl>
<Configurations>Debug;Release;Test</Configurations>
Expand Down
Loading