Smarter SK Agents with Contextual Function Selection

Wait 5 sec.

Smarter SK Agents with Contextual Function SelectionIn today'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 the current conversation context, this feature ensures that your AI agents are smarter, faster, and more effective than ever before.Why Contextual Function Selection MattersWhen 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 Retrieval-Augmented Generation (RAG) 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.Key Benefits of Contextual Function SelectionThis feature is particularly useful for scenarios where agents have access to a broad set of plugins or tools. Here are some key capabilities: Dynamic Function Filtering: Automatically select the top relevant functions based on the conversation context. Enhanced AI Performance: Reduce confusion and improve response accuracy by narrowing down function choices. Token Efficiency: Minimize input token usage by advertising only the necessary functions.Example Use Case: Summarizing Customer ReviewsImagine you'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's how it works:// Create an embedding generator for function vectorizationvar embeddingGenerator = new AzureOpenAIClient(new Uri(""), new ApiKeyCredential("")) .GetEmbeddingClient("") .AsIEmbeddingGenerator();// Create a chat completion agentChatCompletionAgent agent = new(){ Name = "ReviewGuru", Instructions = "You are a friendly assistant that summarizes key points and sentiments" + "from customer reviews. For each response, list available functions", Kernel = kernel, Arguments = new(new PromptExecutionSettings { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() })};// Register the contextual function provideragentThread.AIContextProviders.Add( new ContextualFunctionProvider( vectorStore: new InMemoryVectorStore(new InMemoryVectorStoreOptions() { EmbeddingGenerator = embeddingGenerator }), vectorDimensions: 1536, functions: GetAvailableFunctions(), maxNumberOfFunctions: 3 ));// Invoke the agentChatMessageContent message = await agent .InvokeAsync("Get and summarize customer review.", agentThread) .FirstAsync();Console.WriteLine(message.Content);private IReadOnlyList GetAvailableFunctions() { // Returns 16 functions across 6 categories: customer reviews (1), sentiment analysis (2), // summaries (2), communication (3), date/time (2), and Azure services (6)}Example Output:Customer Reviews:-----------------1. John D. - ★★★★★ Comment: Great product and fast shipping! Date: 2023-10-012. Jane S. - ★★★★ Comment: Good quality, but delivery was a bit slow. Date: 2023-09-283. Mike J. - ★★★ Comment: Average. Works as expected. Date: 2023-09-25Summary:--------The reviews indicate overall customer satisfaction, with highlights on product quality and shippingefficiency. While some customers experienced excellent service, others mentioned areas for improvement, particularly regarding delivery times.Available functions:--------------------- Tools-GetCustomerReviews- Tools-Summarize- Tools-CollectSentimentsNote: Only these 3 functions (GetCustomerReviews, Summarize, and CollectSentiments)out of 16 available functions were selected and advertised to the model based on the conversationcontext.Why This Matters to YouFor developers, this feature simplifies the process of building intelligent, context-aware agents. Whether you're managing customer interactions, automating workflows, or analyzing data, Contextual Function Selection ensures your AI agents are always on point.Learn MoreReady to dive deeper? Check out the official documentation on Microsoft Learn and explore the sample code on GitHub.