laitimes

Semantic Kernel:Service内置服务

author:opendotnet

There are some built-in services in SK that allow our applications to have AI capabilities with simple instantiation calls.

Taking OpenAI as an example, SK has the following built-in OpenAI services:

  • 内容生成服务:OpenAITextGenerationService
  • 聊天服务:OpenAIChatCompletionService
  • 文本转图片服务:OpenAITextToImageService
  • 声音转文本服务:OpenAIAudioToTextService
  • 文本转声音服务:OpenAITextToAudioService
  • 文本嵌入向量服务:OpenAITextEmbeddingGenerationService

These services can be instantiated directly or used in conjunction with the kernel. The following is how to use the TextGeneration service, TextGeration only supports models with modelid as gpt-3.5-turbo-instruct, and the specific implementation is as follows:

using Microsoft.Extensions.Logging;              using Microsoft.SemanticKernel;              using Microsoft.SemanticKernel.ChatCompletion;              using Microsoft.SemanticKernel.Connectors.OpenAI;              using System.Text.Json;                  var chatModelId = "gpt-3.5-turbo-instruct";              var key = File.ReadAllText(@"C:\GPT\key.txt");                  var settings = new PromptExecutionSettings              {              ExtensionData = new Dictionary<string, object>              {              ["max_tokens"] = 1000,              ["temperature"] = 0.2,              ["top_p"] = 0.8,              ["presence_penalty"] = 0.0,              ["frequency_penalty"] = 0.0              }              };              Console.WriteLine("---------------非流式---------------");              var textGenerationService = new OpenAITextGenerationService(chatModelId, key);              var textContents = await textGenerationService.GetTextContentsAsync("用50个字描述一下.NET", settings);              foreach (var textContent in textContents)              {              var usage = textContent?.Metadata?["Usage"] as Azure.AI.OpenAI.CompletionsUsage;              if (usage != )              {              var tokenStr = @$"====================Tokens==================              提示词Tokens数:{usage.PromptTokens}              返回内容Tokens数:{usage.CompletionTokens}              总Tokens数:{usage.TotalTokens}              ===========================================";              Console.WriteLine(tokenStr);              }              Console.WriteLine(textContent.Text);              }                  Console.WriteLine("---------------流式---------------");              var streamTextContents = textGenerationService.GetStreamingTextContentsAsync("用50个字描述一下C#");              await foreach (var textContent in streamTextContents)              {              Console.Write(textContent.Text);              }           

There are two implementations listed above, blocking, which returns results at once, and streaming, which returns results from time to time.

The following parameters are used to fine-tune the behavior of the OpenAI GPT model during text generation: max_tokens: This parameter defines the maximum number of words (or tokens) that the model outputs. Tokens are not just words, but also punctuation marks and spaces, among other things. This limit helps control the length of the generated content. temperature: This parameter is used to control the randomness or creativity of the output. Temperature values range from 0 to 1, with lower temperatures (e.g., 0.2 or 0.3) making the model's output more predictable and stable, while higher temperatures (e.g., 0.8 or 1) increasing the randomness and diversity of the output, but also potentially decreasing the coherence and relevance of the text. top_p (Nucleus Sampling): This parameter controls the range that the model considers when selecting the next word. For example, if the top_p is set to 0.9, the model will select only the next word from those parts of words that have accumulated to 90% probability. This often helps to keep the text relevant while still maintaining some creative freedom. presence_penalty and frequency_penalty: These two parameters are used to increase the variety of output and reduce repeatability. presence_penalty increases the cost of reappearing words that have already occurred and helps to avoid repeating the same topic or vocabulary. frequency_penalty is similar, but it increases the cost based on how often the word appears, and the probability that the word that occurs frequently will be selected in subsequent generations will be reduced. The combination of these parameters can help adjust the style and quality of the generated text to suit different application scenarios and needs. For other services, we'll use the instructions in a subsequent article.

Read on