2.5. semantic kernel中FunctionCalling
自动函数调用的工作原理
模拟函数调用
// 添加一个来自助手的模拟函数调用
chatHistory.Add(
new() {
Role = AuthorRole.Assistant,
Items = [
new FunctionCallContent(
functionName: "get_user_allergies", // 函数名称:获取用户过敏信息
pluginName: "User", // 插件名称:用户(User)
id: "0001", // 调用 ID
arguments: new () { {"username", "laimonisdumins"} } // 参数:用户名 laimonisdumins
),
new FunctionCallContent(
functionName: "get_user_allergies", // 函数名称:获取用户过敏信息
pluginName: "User", // 插件名称:用户(User)
id: "0002", // 调用 ID
arguments: new () { {"username", "emavargova"} } // 参数:用户名 emavargova
)
]
}
);
// 添加来自工具角色的模拟函数返回结果
chatHistory.Add(
new() {
Role = AuthorRole.Tool,
Items = [
new FunctionResultContent(
functionName: "get_user_allergies", // 函数名称:获取用户过敏信息
pluginName: "User", // 插件名称:用户(User)
id: "0001", // 调用 ID
result: "{ \"allergies\": [\"peanuts\", \"gluten\"] }" // 返回结果:用户对花生和麸质过敏
)
]
}
);
chatHistory.Add(
new() {
Role = AuthorRole.Tool,
Items = [
new FunctionResultContent(
functionName: "get_user_allergies", // 函数名称:获取用户过敏信息
pluginName: "User", // 插件名称:用户(User)
id: "0002", // 调用 ID
result: "{ \"allergies\": [\"dairy\", \"soy\"] }" // 返回结果:用户对乳制品和大豆过敏
)
]
}
);函数调用的示例
Semantic Kernel 中的函数调用
步骤
Function Calling 原始做法(自己做)
SK 帮你封装
Last updated