前言
基础可先看文章 .Net基于AgentFramework中智能体Agent Skill集成Shell命令实现小龙虾mini版
参考源代码: NetCoreKevin框架下的kevin.AI.AgentFramework模块
基于.NET构建的企业级SaaSAI智能体应用架构,采用前后端分离设计,具备以下核心特性:
- AI智能体框架RAG检索增强
- AI知识库
- AI智能体技能集成
- -RabbitMQ消息队列
- 一库多租户解决方案
- 多级缓存机制
- CAP事件集成
- SignalR实时通信
- 等等......
- 项目地址:github:github.com/junkai-li/N… Gitee: gitee.com/netkevin-li…
参考文献:
learn.microsoft.com/zh-cn/agent…
下一步
在上一篇文章中,我们学习了如何为AgentFramework添加Agent Skill命令。接下来,我们将共同封装一个可执行的Python技能,具体实现代码如下:
using System.ComponentModel;
using System.Diagnostics;
namespace kevin.AI.AgentFramework.Tools
{
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// RunPython — 一个 执行Python脚本的工具
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
public class PythonTools
{
[Description("执行Python脚本。通过System.Diagnostics.Process类来启动一个新的进程,并运行Python.py的脚本。这种方法适用于Windows和Linux系统。")]
public static string RunPythonPy([Description("需要执行的python脚本路径。例如:'Skills\\python-skills\\hello-python\\scripts\\hello-python.py'")]
string scriptPath,
[Description("需要传入python脚本的参数。例如:['你好','word']")]
List<string> args = default
)
{
try
{
string output = "";
scriptPath = AppContext.BaseDirectory + scriptPath.Replace(@"/", @"\");
Console.WriteLine($"🔧 正在执行Py脚本 {scriptPath}");
// 设置进程信息
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"python"; // Python解释器的路径,例如 "python" 或 "python3"
start.Arguments = $"{scriptPath}"; // 传递参数
if (args != default)
{
foreach (var item in args)
{
start.Arguments += $" {item} \" ";
}
}
start.UseShellExecute = false; // 不使用操作系统外壳启动
start.RedirectStandardOutput = true; // 重定向标准输出
start.RedirectStandardError = true; // 重定向标准错误
using (Process process = Process.Start(start))
{
// 获取输出
output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit(); // 等待进程结束
if (!string.IsNullOrEmpty(error))
{
return $"❌ 执行失败: {error}";
}
}
if (string.IsNullOrWhiteSpace(output))
{
output = "Python脚本执行完成,但没有输出结果。";
}
return output;
}
catch (Exception ex)
{
return $"❌ 执行失败: {ex.Message}";
}
}
[Description("执行Python代码。使用IronPython库直接执行Python代码 必须定义为main函数")]
public static string RunPythonCode([Description("需要执行的python代码。例如:'def main(name): return 'Hello ' + name.title() + '!'")]
string code)
{
try
{
Console.WriteLine($"🔧 正在执行Py代码 {code}");
// 创建Python引擎和作用域
var eng = IronPython.Hosting.Python.CreateEngine();
var scope = eng.CreateScope();
eng.Execute(code, scope);
dynamic main = scope.GetVariable("main");
return main();
}
catch (Exception ex)
{
return $"❌ 执行失败: {ex.Message}";
}
}
}
}
注册技能
#pragma warning disable MAAI001 // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
var skillsProvider = new FileAgentSkillsProvider(
skillPaths: [Path.Combine(AppContext.BaseDirectory + "/Skills", "expense-report-skills"),
Path.Combine(AppContext.BaseDirectory + "/Skills", "system-ops-skills"),
Path.Combine(AppContext.BaseDirectory + "/Skills", "hello-python-skills")
],
options: new FileAgentSkillsProviderOptions
{
SkillsInstructionPrompt = """
你可以使用以下技能获取领域知识和操作指引。
所有文件目录都在服务器的/Skills 文件夹下,技能文件夹命名为 技能名称-skills,技能文件夹内包含技能指令文件 instruction.txt、参考资料文件夹 resources 和可执行脚本文件夹 scripts。
脚本文件夹 scripts 如何包含python脚本,则使用RunPythonPy来执行或者RunPythonCode来执行,否则使用RunShell来执行。
每个技能提供专业指令、参考文档和可执行脚本
它们如下:
{0}
使用 `system-ops-skills` 这个技能 工作流程:
1. 当用户任务匹配技能描述时,使用 `load_skill` 加载该技能的完整指令
2. 技能指令中会标明可用脚本及其执行命令
3. 使用 `run_shell` 工具执行技能中标注的命令
4. 需要时使用 `read_skill_resource` 读取参考资料。
重要原则:先加载知识,再执行操作。
使用 `hello-python-skills` 这个技能
1.当用户任务匹配技能描述时,使用 `load_skill` 加载该技能的完整指令
2.技能指令中会标明可用脚本及其执行命令
3.用于测试python环境运行的技能,包含一个简单的python脚本 脚本文件名:hello-python.py, 可以随意传入参数, 输出“你所有的参数和Hello,Python”返回结果为"你所有的参数和Hello,Python"。
4. 需要时使用 `read_skill_resource` 读取参考资料。
"""
});
#pragma warning restore MAAI001 // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
chatClientAgentOptions.AIContextProviders = [skillsProvider];
Console.WriteLine("📂 Skills 已从文件系统加载");