Passing Reserve Keywords (kernel, chatHistory) to Functions with ChatCompletion Agents #11000
-
Hello, I'm noticing that with a ChatCompletion agent that has a plugin with functions that require the reserved keyword parameters are not recognizing that they should be intercepted from the LLM needing to identify those arguments (referenced here: #10214). As an example, I defined a basic kernel function with chat_history as a parameter, expecting that this would not be serialized in the tool call definition to the LLM but that SK would pass this into the function's context. After some logging, it looks as though the LLM is extracting what should be passed like any other argument in the kernel function. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Thanks for raising this question. Here’s a breakdown of how the How It Works TodayWith the chat completion agent, you can pass Example Test Kernel FunctionFor testing, I defined a kernel function that retrieves the chat history. Notice that I renamed the parameter to class History:
@kernel_function(description="Provides the chat history.")
def get_history(self, arguments: KernelArguments) -> Annotated[str, "Returns the chat history."]:
chat_history = arguments.get("history")
if chat_history:
print(f"Chat History: {chat_history}") Since this function requires the Parameter Handling in the CodeIn the if param.name == "arguments":
function_arguments[param.name] = context.arguments
continue This makes sure that if the arguments are provided and recognized as part of the JSON Schema OverviewFor the example kernel function I showed above, the JSON Schema advertised to the model is structured as follows: {
"type": "function",
"function": {
"name": "History-get_history",
"description": "Provides the chat history.",
"parameters": {
"type": "object",
"properties": {
"arguments": {
"type": "object",
"properties": {}
}
},
"required": ["arguments"]
}
}
} Potential ImprovementOne improvement to consider is not advertising the arguments parameter in the JSON schema at all. We had a solution for this in the past, which was later removed to align with .NET. It appears that the .NET team has moved forward with handling it, while the Python implementation still awaits a similar update. I recognize that since some of the parameters are reserved, we don't need to send them, and thus can save on some token cost. |
Beta Was this translation helpful? Give feedback.
Hi @haithamshahin333,
Thanks for raising this question. Here’s a breakdown of how the
ChatCompletionAgent
currently handles reserved keyword parameters likechat_history
and where we might improve clarity.How It Works Today
With the chat completion agent, you can pass
chat_history
(or in my test case below, renamed ashistory
) as part of the kernel arguments. This argument is used to generate the prompt for the model. In our implementation, we explicitly remove this argument from the kernel arguments before passing them to the function. (I acknowledge that this behavior could be cleaner.)Example Test Kernel Function
For testing, I defined a kernel function that retrieves the chat histor…