Repository: microsoft/semantic-kernel
File: dotnet/samples/Concepts/Agents/ChatCompletion_ContextualFunctionSelection.cs (C#)

Code before:

        // Create our agent.
        Kernel kernel = this.CreateKernelWithChatCompletion();
        ChatCompletionAgent 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(options: new FunctionChoiceBehaviorOptions { RetainArgumentTypes = true }) })
            };

        // Create a thread and register context based function selection provider that will do RAG on
        // provided functions to advertise only those that are relevant to the current context.
        ChatHistoryAgentThread agentThread = new();

        var allAvailableFunctions = GetAvailableFunctions();

        agentThread.AIContextProviders.Add(
            new ContextualFunctionProvider(
                vectorStore: new InMemoryVectorStore(new InMemoryVectorStoreOptions() { EmbeddingGenerator = embeddingGenerator }),
                vectorDimensions: 1536,
                functions: allAvailableFunctions,
                maxNumberOfFunctions: 3, // Instruct the provider to return a maximum of 3 relevant functions
                contextSize: 2 // Use the last 2 messages as context for function selection
            )
        );

        // Invoke and display assistant response
        ChatMessageContent message = await agent.InvokeAsync("Get and summarize customer review.", agentThread).FirstAsync();
        Console.WriteLine(message.Content);

        //Expected output:
        /*  
            Retrieves and summarizes customer reviews.  
  
            ### Customer Reviews:  
            1. **John D.** - ★★★★★  
                *Comment:* Great product and fast shipping!  
                *Date:* 2023-10-01  
            2. **Jane S.** - ★★★★  
                *Comment:* Good quality, but delivery was a bit slow.  
                *Date:* 2023-09-28  
            3. **Mike J.** - ★★★  
                *Comment:* Average. Works as expected.  
                *Date:* 2023-09-25  
  
            ### Summary:  
            The reviews indicate overall customer satisfaction, with highlights on product quality and shipping efficiency.  
            While some customers experienced excellent service, others mentioned areas for improvement, particularly regarding delivery times.  

Review comment (suggestion):
Suggest `Conversation` since this is something more related to the `messages`.

```suggestion
                conversationSize: 2 // Use the last 2 messages as context for function selection
```

Code after:

        // Create our agent.
        Kernel kernel = this.CreateKernelWithChatCompletion();
        ChatCompletionAgent 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(options: new FunctionChoiceBehaviorOptions { RetainArgumentTypes = true }) })
            };

        // Create a thread and register context based function selection provider that will do RAG on
        // provided functions to advertise only those that are relevant to the current context.
        ChatHistoryAgentThread agentThread = new();

        var allAvailableFunctions = GetAvailableFunctions();

        agentThread.AIContextProviders.Add(
            new ContextualFunctionProvider(
                vectorStore: new InMemoryVectorStore(new InMemoryVectorStoreOptions() { EmbeddingGenerator = embeddingGenerator }),
                vectorDimensions: 1536,
                functions: allAvailableFunctions,
                maxNumberOfFunctions: 3 // Instruct the provider to return a maximum of 3 relevant functions
            )
        );

        // Invoke and display assistant response
        ChatMessageContent message = await agent.InvokeAsync("Get and summarize customer review.", agentThread).FirstAsync();
        Console.WriteLine(message.Content);

        //Expected output:
        /*  
            Retrieves and summarizes customer reviews.  
  
            ### Customer Reviews:  
            1. **John D.** - ★★★★★  
                *Comment:* Great product and fast shipping!  
                *Date:* 2023-10-01  
            2. **Jane S.** - ★★★★  
                *Comment:* Good quality, but delivery was a bit slow.  
                *Date:* 2023-09-28  
            3. **Mike J.** - ★★★  
                *Comment:* Average. Works as expected.  
                *Date:* 2023-09-25  
  
            ### Summary:  
            The reviews indicate overall customer satisfaction, with highlights on product quality and shipping efficiency.  
            While some customers experienced excellent service, others mentioned areas for improvement, particularly regarding delivery times.