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
10 changes: 9 additions & 1 deletion Samples/Senparc.AI.Samples.Consoles/Samples/ChatSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ 输入 exit 退出。
maxHistoryStore: maxHistoryCount,
chatSystemMessage: systemMessage,
senparcAiSetting: setting,
kernelBuilderAction: kh =>
kernelBuilderAction: kh =>
kh.Plugins.AddFromType<NowPlugin>()
.AddFromType<SearchPlugin>()
);
Expand Down Expand Up @@ -240,6 +240,14 @@ 输入 exit 退出。
//输出结果
SenparcAiResult result = await _semanticAiHandler.ChatAsync(iWantToRun, input, streamItemProceessing);

if (result.GetLastFunctionResultContent().IsFunctionCall)
{
Console.WriteLine();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"\t\t >>> 本次请求执行了 function-calling:{result.GetLastFunctionResultContent().FunctionResultContent?.FunctionName} <<<");
}

//复原颜色
Console.ForegroundColor = originalColor;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Senparc.AI.Agents/Senparc.AI.Agents.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.5.4-bata.3</Version>
<Version>0.5.5</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI.Agents</AssemblyName>
Expand Down
38 changes: 37 additions & 1 deletion src/Senparc.AI.Kernel/Entities/SenparcAiResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@


using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Senparc.AI.Interfaces;
using Senparc.AI.Kernel.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

Expand Down Expand Up @@ -50,12 +52,46 @@ public SenparcAiResult(IWantToRun iwantToRun, IAiContext inputContext)
IWantToRun = iwantToRun;
InputContext = inputContext;
}

/// <summary>
/// Get last response FunctionResultContent
/// </summary>
/// <returns></returns>
public (FunctionResultContent? FunctionResultContent, bool IsFunctionCall) GetLastFunctionResultContent()
{
//var functionResultContent = (IWantToRun.StoredAiArguments.Context["history"] as ChatHistory)
// ?.Last()
// .Items.FirstOrDefault(z => z is Microsoft.SemanticKernel.FunctionResultContent) as FunctionResultContent;

//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;

/*
{
"CallId": "call_yl8mID5uxS3hJB2J3RenVhXh",
"PluginName": "NowPlugin",
"FunctionName": "GetCurrentNowTime",
"Result": "2025-05-09 22:11:42",
"MimeType": null,
"InnerContent": null,
"ModelId": null,
"Metadata": null
}
*/

var isFunctionCall = functionResultContent != null && functionResultContent.FunctionName != null;
return (functionResultContent, isFunctionCall);
}
}

public class SenparcAiResult<T> : SenparcAiResult, IAiResult
{
public T Result { get; set; }
public IAsyncEnumerable<StreamingKernelContent>? /*SKContext*/ StreamResult { get; set; }
public IAsyncEnumerable<StreamingKernelContent>? /*SKContext*/
StreamResult { get; set; }

public SenparcAiResult(IWantToRun iWwantToRun, string inputContent)
: base(iWwantToRun, inputContent)
Expand Down
14 changes: 11 additions & 3 deletions src/Senparc.AI.Kernel/Handlers/SemanticAiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public async Task<SenparcAiResult> ChatAsync(IWantToRun iWantToRun, string input

SenparcKernelAiResult<string>? aiResult = null;
List<IContentItem> visionResult = await ChatHelper.TryGetImagesBase64FromContent(Senparc.CO2NET.SenparcDI.GetServiceProvider(), input);
aiResult = await iWantToRun.RunChatVisionAsync(newRequest, chatHistory, visionResult,parameter, inStreamItemProceessing);
aiResult = await iWantToRun.RunChatVisionAsync(newRequest, chatHistory, visionResult, parameter, inStreamItemProceessing);
// if (visionResult.Exists(z => z.Type == ContentType.Image))
// {
// aiResult = await iWantToRun.RunVisionAsync(newRequest, chatHistory, visionResult, inStreamItemProceessing);
Expand All @@ -168,8 +168,16 @@ public async Task<SenparcAiResult> ChatAsync(IWantToRun iWantToRun, string input
this.RemoveHistory(chatHistory, maxHistoryCount - 1);
}

//newHistory = newHistory + $"\n{humanId}: {input}\n{robotId}: {aiResult.OutputString}";
chatHistory.AddAssistantMessage(aiResult.OutputString);
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);
}

//记录对话历史(可选)
//request.SetStoredContext(historyArgName, newHistory);
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.2-beta.2</Version>
<Version>0.23.3</Version>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<AssemblyName>Senparc.AI.Kernel</AssemblyName>
Expand Down Expand Up @@ -55,6 +55,7 @@
[2024-11-19] v0.21.0 Update CO2NET, reframe for Aspire
[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
</PackageReleaseNotes>
<RepositoryUrl>https://github.com/Senparc/Senparc.AI.Kernel</RepositoryUrl>
<Configurations>Debug;Release;Test</Configurations>
Expand Down
2 changes: 1 addition & 1 deletion src/Senparc.AI/Senparc.AI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.24.0-beta.3</Version>
<Version>0.24.1</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI</AssemblyName>
Expand Down
Loading