马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
在 Microsoft Agent Framework 中,Agent Skills 是一个非常紧张但轻易被忽略的本领。它可以让你的 Agent 拥有“插件化本领”,乃至可以实行当地脚本(如 Python),实现真正的自动化。
本文团结完备示例,带你从 原理 → 结构 → 实战 → 坑点 全面把握怎样通过 Skills + 自界说 Tool,实现实行 Python 脚本。
一、什么是 Agent Skills?
官方界说:
Agent Skills 是一组可移植的指令、脚本和资源,用于为 Agent 提供特定本领。
简单明白就是给 Agent 安装“技能包”,好比:
- 数据分析 skill
- 报销考核 skill
- 集会纪要 skill(本文示例)
二、Skill 目次结构
一个标准 Skill 结构如下:- skills/
- └── meeting-notes/
- ├── SKILL.md
- └── scripts/
- └── GetMeetingNotes.py
复制代码 1. SKILL.md(核心)
- ---
- name: meeting-notes
- description: Various python scripts for meeting-notes
- ---
复制代码- # The meeting notes
- [GetMeetingNotes](meeting-notes/scripts/GetMeetingNotes.py)
- Give above script-path to a 'execute_python' tool
复制代码 关键点:
- name 必须和目次划一
- description 用于让 Agent 判断是否使用该 skill
- markdown 内容 = 利用阐明
2. Python Script
- def main():
- return "Chester completed the create user api, and need implement the update user api"
- if __name__ == "__main__":
- print(main())
复制代码 阐明:
三、关键标题:C# 现在不支持自动实行 scripts
官方文档明白阐明:
Script execution is not yet supported in C#
以是:
- ❌ Agent 不会自动实行 .py
- ✅ 须要本身提供 Tool
四、核心实现:自界说 Python 实行器
PythonRunner.cs
- public static class PythonRunner
- {
- public static string RunPhytonScript(string pythonFilePath)
- {
- if (string.IsNullOrWhiteSpace(pythonFilePath))
- throw new ArgumentException("File path cannot be empty.", nameof(pythonFilePath));
- pythonFilePath = Path.Combine(AppContext.BaseDirectory, "skills", pythonFilePath);
- if (!File.Exists(pythonFilePath))
- throw new FileNotFoundException("Python file not found.", pythonFilePath);
- if (!string.Equals(Path.GetExtension(pythonFilePath), ".py", StringComparison.OrdinalIgnoreCase))
- throw new ArgumentException("File must have a .py extension.", nameof(pythonFilePath));
- ProcessStartInfo startInfo = new ProcessStartInfo
- {
- FileName = "python",
- Arguments = """ + pythonFilePath + """,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- CreateNoWindow = true,
- StandardOutputEncoding = Encoding.UTF8,
- StandardErrorEncoding = Encoding.UTF8
- };
- using (Process process = new Process { StartInfo = startInfo })
- {
- process.Start();
- string output = process.StandardOutput.ReadToEnd();
- string error = process.StandardError.ReadToEnd();
- process.WaitForExit();
- if (process.ExitCode != 0 || !string.IsNullOrWhiteSpace(error))
- throw new InvalidOperationException(
- "Python script failed. ExitCode=" + process.ExitCode + Environment.NewLine + error.Trim());
- return output.TrimEnd();
- }
- }
- }
复制代码 五、把 Python 实行本领酿成 Agent Tool
Program.cs
- Tools = [
- AIFunctionFactory.Create(
- PythonRunner.RunPhytonScript,
- name: "execute_python"
- )
- ]
复制代码 关键点:
- 方法袒露为 Tool
- 名字必须和 SKILL.md 里的阐明划一
六、Skills + Tool 协作流程
- 用户输入
- ↓
- Agent 发现 skills(Advertise)
- ↓
- 判断匹配 meeting-notes skill
- ↓
- load_skill(加载 SKILL.md)
- ↓
- 理解指令:"调用 execute_python"
- ↓
- 调用 Tool
- ↓
- 执行 Python
- ↓
- 返回结果
复制代码 七、运行结果
用户输入:Agent 内部活动:
- 发现 meeting-notes
- 读取 SKILL.md
- 找到实行阐明:
- Give above script-path to a 'execute_python' tool
复制代码- execute_python("meeting-notes/scripts/GetMeetingNotes.py")
复制代码 终极输出:- Chester completed the create user api, and need implement the update user api
复制代码 八、关键筹划模式
Pattern:Skill 负责“决定”,Tool 负责“实行”
层级作用Skill告诉 Agent 做什么Tool真正实行阐明:
- Skill ≠ 代码实行
- Skill = Prompt + 指南
九、安全发起
实行脚本属于高风险利用,须要注意:
1. 限定路径
克制实行体系敏感目次,如:2. 白名单机制
只答应实行:3. 沙箱运行
|