iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😺

Using Bing Search with Semantic Kernel's ChatCompletion

に公開

The training data for GPT-3.5-Turbo and GPT-4 is cut off as of September 2021. This means they do not possess knowledge about events occurring after that date. Furthermore, when asked about specific individuals, such as "Tell me about Tomomitsu Kusaba," the answers provided are often inaccurate or fabricated.

Therefore, I thought it would be effective to correct the answers by providing a prompt based on Bing search results. The downside to this approach is that each time you use Bing search, the portion of the prompt occupied by the search results increases.

Prerequisites

In ChatCompletion, message types are categorized as follows:

Message Type Content
System Prompt message
User Message input by the user
Assistant Message input by the AI assistant

Overview

Retrieve the top 10 results from a Bing search, concatenate them into a single string, and include it as a System message. Then, include the user's question as a User message to generate the response.

This is the method I intend to implement this time.

Code

            // Create the kernel
            kernel = new KernelBuilder().Configure(c =>
            {
                //c.AddAzureChatCompletionService(serviceId, deploymentName, baseUrl, key);
                c.AddAzureChatCompletionService(deploymentName, baseUrl, key, logger: _logger);

            }).WithLogger(_logger).Build();
            // Create the Bing connector
            using var bingConnector = new BingConnector(_configuration.GetValue<string>("BingKey") ?? string.Empty, logger1);


            GptChat4 = kernel.GetService<IChatCompletion>();

            chatHistory = (OpenAIChatHistory)GptChat4.CreateNewChat("{{bing.search $input}} You are an AI assistant named Honoka. You provide helpful answers in a casual, feminine tone.");

            // Bing search: Retrieve the top 10 items
            var res = await bingConnector.SearchAsync(input,10);
            // Concatenate all 10 items obtained from the search into a string
            var sb = new StringBuilder();
            foreach (var item in res)
            {
                sb.Append(item);
            }
            chatHistory.AddSystemMessage(sb.ToString());
            var setting = new ChatRequestSettings
            {
                Temperature = 0.8,
                MaxTokens = 2000,
                FrequencyPenalty = 0.5,

            };
            var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseAutoLinks().UseBootstrap().UseDiagrams().UseGridTables().Build();
            string fullMessage = string.Empty;
            GeneratedHtml = string.Empty;
            await foreach (string message in GptChat4.GenerateMessageStreamAsync(chatHistory, setting))
            {
                _logger.LogInformation("message : {}", message);
                if (message != null && message.Length > 0)
                {
                    var messageHtml = Markdown.ToHtml(fullMessage, pipeline);
                    _logger.LogInformation("messageHtml : {}", messageHtml);
                    //chatHistory.AddAssistantMessage(message);
                    GeneratedHtml = messageHtml;
                    fullMessage += message;
                    NotifyStateChanged();
                }

            }
            chatHistory.AddAssistantMessage(fullMessage);

For final output to the Web, using GeneratedHtml is recommended, while for text-based output, using fullMessage would be appropriate.

Discussion