
一步在上一篇文章中我们学习了如何为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])] Liststring 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 已从文件系统加载);