You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Semantic Kernel(Python):Agent调用插件方法时参数无法传递的求助

解决Semantic Kernel Agent调用插件参数传不进去的问题

为啥参数传不进去?

你的代码里有几个关键问题:

  • 插件函数没明确声明topic参数,用了**kwargs,Semantic Kernel无法识别这个参数
  • 函数里用了未定义的question变量,而且prompt参数根本没用到
  • Agent调用时,消息格式和参数传递的逻辑不符合要求

怎么改?

1. 规范插件函数的参数定义

用@kernel_function明确声明topic参数,同时修正函数内部的错误:

from semantic_kernel.functions.kernel_function_decorator import kernel_function
from semantic_kernel.functions.kernel_function_parameter import KernelFunctionParameter

class DatabrickConnector:
    @kernel_function(
        description="获取指定主题的详细描述",
        name="get_topic_description",
        parameters=[
            KernelFunctionParameter(
                name="topic",
                description="需要描述的主题",
                type=str
            )
        ]
    )
    async def get_topic_description(self, topic: str) -> str:
        # 确保self.client是已经初始化好的OpenAI客户端
        response = await self.client.beta.chat.completions.parse(
            model=openai_gpt_deployment_name,
            messages=[
                {"role": "user", "content": f"给我详细描述一下{topic}"}
            ],
        )
        # 返回实际的回答内容,根据你的需求调整
        return response.choices[0].message.content

2. 调整Agent的调用方式

Agent的invoke方法需要传入聊天历史列表,不是字符串,同时正确传递参数:

from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.functions.kernel_arguments import KernelArguments

# 构造聊天历史
chat_history = ChatHistory()
chat_history.add_user_message("请提供指定主题的详细描述")

# 调用Agent并传参
response = await agent.invoke(
    chat_history=chat_history,
    arguments=KernelArguments(topic="Semantic Kernel")
)

print(response.content)

3. 给Agent加明确的指令

让Agent知道必须调用你的插件函数,避免自行生成内容:

agent = ChatCompletionAgent(
    kernel=kernel_RAG,
    name="Response_Agent",
    instructions="必须使用DatabrickConnector插件的get_topic_description函数来生成主题描述,一定要用传入的topic参数"
)

这样修改后,参数就能正确传递到插件函数中了。

内容的提问来源于stack exchange,提问作者Team Constellation OPT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 19:20:02