Skip to content

Lesson 5: Semantic Kernel chatbot with Chat Completion AgentΒΆ

In this lesson we will introduce a Chat Completion Agent. The Chat Completion Agent is one of the agents available in the Semantic Kernel Agent framework. This agent has specific instructions to provide sentiment analysis on stocks. Note that the Agent Framework is currently in preview and subject to change.

  1. Ensure all pre-requisites are met and installed.

  2. Switch to Lesson 5 directory:

    cd ../Lesson5
    
  3. Start by copying appsettings.json from Lesson 1:

    cp ../Lesson1/appsettings.json .
    
  4. Open Program.cs and locate: TODO: Step 0a - Comment line to print all plugins and functions and comment the line below:

    Console.WriteLine(functions.ToPrintableString());
    
  5. Next locate TODO: Step 0b - Uncomment out all code after "Execute program" comment in Program.cs by removing the /* and */ characters so that the chat completion code executes:

    // TODO: Step 0b - Uncomment out all code after "Execute program" comment
    // Execute program.
    /*
    const string terminationPhrase = "quit";
    string? userInput;
    do
    {
        ...
    }
    while (userInput != terminationPhrase);
    */
    
  6. Run program and ask what the sentiment on Microsoft stock is:

    dotnet run
    
  7. At the prompt enter:

    What is the sentiment on Microsoft stock?
    

    Assistant will give a generic response:

    Assistant > The sentiment on Microsoft (ticker symbol: MSFT) largely hinges on factors like:
    
        - Tech innovation (e.g., AI, Azure cloud service, and gaming)
        - Quarterly earnings reports
        - Overall market conditions
        - How much caffeine traders have consumed
    

    Notice it does not provide a specific answer. We will add introduce a Stock Sentiment agent to provide a more specific answer.

  8. Next locate TODO: Step 1 in Program.cs and add the following import lines:

    using Microsoft.SemanticKernel.Agents;
    
  9. Next locate TODO: Step 2 - Add code to create Stock Sentiment Agent in Program.cs and provide the following lines to initialize the StockSentimentAgent using ChatCompletionAgent:

    ChatCompletionAgent stockSentimentAgent =
        new()
        {
            Name = "StockSentimentAgent",
            Instructions =
                """
                Your responsibility is to find the stock sentiment for a given Stock.
    
                RULES:
                - Use stock sentiment scale from 1 to 10 where stock sentiment is 1 for sell and 10 for buy.
                - Provide the rating in your response and a recommendation to buy, hold or sell.
                - Include the reasoning behind your recommendation.
                - Include the source of the sentiment in your response.
                """,
            Kernel = kernel,
            Arguments = new KernelArguments(new OpenAIPromptExecutionSettings() { 
                FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()})
        };
    
  10. Locate TODO: Step 3 - Replace chatCompletionService with stockSentimentAgent and replace the foreach line to use the stockSentimentAgent instead of the chatCompletionService as follows:

        await foreach (var chatUpdate in stockSentimentAgent.InvokeAsync(chatHistory, kernelArgs))
    
  11. Re-run the program and ask what the sentiment on Microsoft stock is:

    dotnet run
    User > What is the sentiment on Microsoft stock?
    

    Assistant response:

    Assistant > Based on its current performance, Microsoft's stock price (MSFT) is at $408.43, reflecting a strong position as a leading tech giant known for its robust ecosystem and diversified revenue streams.
    
    Stock Sentiment: **8 out of 10**
    Recommendation: **Buy**
    
    Reasoning:
    1. Microsoft's cloud computing segment, Azure, is growing rapidly and gaining market share.
    2. The company's involvement in AI and other cutting-edge technologies positions it for long-term growth.
    3. Continuous expansion into lucrative markets like gaming (Xbox) and enterprise software keeps its portfolio resilient.
    
    However, as with all investments, ensure you're comfortable with the valuation and market conditions before diving in!
    
    Source: Current stock price from live data. 
    

Expect to see a more specific response. Notice it provides a rating on 1 to 10 and recommendation to sell, hold or buy as specified in the agent instructions, however the only live data used for this recommendation is the stock price, so I would not trust this advice blindly. On the next lesson we will introduce grounding with Bing search to be able to retrieve more up to date data grounded using Bing Search data.