{"id":4904,"date":"2025-06-05T02:28:47","date_gmt":"2025-06-05T09:28:47","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=4904"},"modified":"2025-06-05T02:28:47","modified_gmt":"2025-06-05T09:28:47","slug":"smarter-sk-agents-with-contextual-function-selection","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/smarter-sk-agents-with-contextual-function-selection\/","title":{"rendered":"Smarter SK Agents with Contextual Function Selection"},"content":{"rendered":"<h1>Smarter SK Agents with Contextual Function Selection<\/h1>\n<p>In today&#8217;s fast-paced AI landscape, developers are constantly seeking ways to make AI interactions more efficient and relevant. The new <strong>Contextual Function Selection<\/strong> feature in the Semantic Kernel Agent Framework is here to address this need. By dynamically selecting and advertising only the most relevant functions based on the current conversation context, this feature ensures that your AI agents are smarter, faster, and more effective than ever before.<\/p>\n<h2>Why Contextual Function Selection Matters<\/h2>\n<p>When dealing with a large number of available functions, AI models often struggle to choose the right one, leading to confusion and inefficiency. Contextual Function Selection solves this problem by leveraging <strong>Retrieval-Augmented Generation (RAG)<\/strong> to filter and present only the most pertinent functions for each interaction. This not only improves the accuracy of AI responses but also reduces token consumption and enhances overall performance.<\/p>\n<h2>Key Benefits of Contextual Function Selection<\/h2>\n<p>This feature is particularly useful for scenarios where agents have access to a broad set of plugins or tools. Here are some key capabilities:<\/p>\n<ul>\n<li><strong>Dynamic Function Filtering<\/strong>: Automatically select the top relevant functions based on the conversation context.<\/li>\n<li><strong>Enhanced AI Performance<\/strong>: Reduce confusion and improve response accuracy by narrowing down function choices.<\/li>\n<li><strong>Token Efficiency<\/strong>: Minimize input token usage by advertising only the necessary functions.<\/li>\n<\/ul>\n<h3>Example Use Case: Summarizing Customer Reviews<\/h3>\n<p>Imagine you&#8217;re building an AI agent to summarize customer reviews. With Contextual Function Selection, the agent dynamically selects only the relevant functions, such as retrieving reviews, summarizing them, and analyzing sentiments. Here&#8217;s how it works:<\/p>\n<pre><code class=\"language-csharp\">\/\/ Create an embedding generator for function vectorization\r\nvar embeddingGenerator = new AzureOpenAIClient(new Uri(\"&lt;endpoint&gt;\"), new ApiKeyCredential(\"&lt;api-key&gt;\"))\r\n    .GetEmbeddingClient(\"&lt;deployment-name&gt;\")\r\n    .AsIEmbeddingGenerator();\r\n\r\n\/\/ Create a chat completion agent\r\nChatCompletionAgent agent = new()\r\n{\r\n    Name = \"ReviewGuru\",\r\n    Instructions = \"You are a friendly assistant that summarizes key points and sentiments\" +\r\n                   \"from customer reviews. For each response, list available functions\",\r\n    Kernel = kernel,\r\n    Arguments = new(new PromptExecutionSettings\r\n    {\r\n        FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()\r\n    })\r\n};\r\n\r\n\/\/ Register the contextual function provider\r\nagentThread.AIContextProviders.Add(\r\n    new ContextualFunctionProvider(\r\n        vectorStore: new InMemoryVectorStore(new InMemoryVectorStoreOptions()\r\n        {\r\n           EmbeddingGenerator = embeddingGenerator\r\n        }),\r\n        vectorDimensions: 1536,\r\n        functions: GetAvailableFunctions(),\r\n        maxNumberOfFunctions: 3\r\n    )\r\n);\r\n\r\n\/\/ Invoke the agent\r\nChatMessageContent message = await agent\r\n    .InvokeAsync(\"Get and summarize customer review.\", agentThread)\r\n    .FirstAsync();\r\nConsole.WriteLine(message.Content);\r\n\r\nprivate IReadOnlyList&lt;AIFunction&gt; GetAvailableFunctions() \r\n{\r\n   \/\/ Returns 16 functions across 6 categories: customer reviews (1), sentiment analysis (2),\r\n   \/\/ summaries (2), communication (3), date\/time (2), and Azure services (6)\r\n}<\/code><\/pre>\n<h3>Example Output:<\/h3>\n<pre><code>Customer Reviews:\r\n-----------------\r\n1. John D. - \u2605\u2605\u2605\u2605\u2605\r\n   Comment: Great product and fast shipping!\r\n   Date: 2023-10-01\r\n\r\n2. Jane S. - \u2605\u2605\u2605\u2605\r\n   Comment: Good quality, but delivery was a bit slow.\r\n   Date: 2023-09-28\r\n\r\n3. Mike J. - \u2605\u2605\u2605\r\n   Comment: Average. Works as expected.\r\n   Date: 2023-09-25\r\n\r\nSummary:\r\n--------\r\nThe reviews indicate overall customer satisfaction, with highlights on product quality and shipping\r\nefficiency. While some customers experienced excellent service, others mentioned areas for \r\nimprovement, particularly regarding delivery times.\r\n\r\nAvailable functions:\r\n--------------------\r\n- Tools-GetCustomerReviews\r\n- Tools-Summarize\r\n- Tools-CollectSentiments\r\n<\/code><\/pre>\n<p>Note: Only these 3 functions (GetCustomerReviews, Summarize, and CollectSentiments)\nout of 16 available functions were selected and advertised to the model based on the conversation\ncontext.<\/p>\n<h2>Why This Matters to You<\/h2>\n<p>For developers, this feature simplifies the process of building intelligent, context-aware agents. Whether you&#8217;re managing customer interactions, automating workflows, or analyzing data, Contextual Function Selection ensures your AI agents are always on point.<\/p>\n<h2>Learn More<\/h2>\n<p>Ready to dive deeper? Check out the <a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/frameworks\/agent\/agent-contextual-function-selection?pivots=programming-language-csharp\" target=\"_blank\" rel=\"noopener\">official documentation on Microsoft Learn<\/a> and explore the <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/dotnet\/samples\/Concepts\/Agents\/ChatCompletion_ContextualFunctionSelection.cs\" target=\"_blank\" rel=\"noopener\">sample code on GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Smarter SK Agents with Contextual Function Selection In today&#8217;s fast-paced AI landscape, developers are constantly seeking ways to make AI interactions more efficient and relevant. The new Contextual Function Selection feature in the Semantic Kernel Agent Framework is here to address this need. By dynamically selecting and advertising only the most relevant functions based on [&hellip;]<\/p>\n","protected":false},"author":157200,"featured_media":4915,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[78,27,47,1,16],"tags":[],"class_list":["post-4904","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-agents","category-announcement","category-semantic-kernel","category-tools"],"acf":[],"blog_post_summary":"<p>Smarter SK Agents with Contextual Function Selection In today&#8217;s fast-paced AI landscape, developers are constantly seeking ways to make AI interactions more efficient and relevant. The new Contextual Function Selection feature in the Semantic Kernel Agent Framework is here to address this need. By dynamically selecting and advertising only the most relevant functions based on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/157200"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=4904"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4904\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/4915"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=4904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=4904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=4904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}