|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using Cnblogs.DashScope.Sdk; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using Microsoft.KernelMemory.AI; |
| 5 | +using Microsoft.KernelMemory.Diagnostics; |
| 6 | + |
| 7 | +namespace Cnblogs.KernelMemory.AI.DashScope; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Text generator using DashScope. |
| 11 | +/// </summary> |
| 12 | +/// <param name="dashScopeClient">The <see cref="IDashScopeClient"/>.</param> |
| 13 | +/// <param name="modelId">Model name.</param> |
| 14 | +/// <param name="loggerFactory">Logger factory to use.</param> |
| 15 | +/// <param name="tokenizer">Tokenizer to use.</param> |
| 16 | +/// <param name="maxToken">Maximum token count.</param> |
| 17 | +public class DashScopeTextGenerator( |
| 18 | + IDashScopeClient dashScopeClient, |
| 19 | + string modelId, |
| 20 | + ILoggerFactory? loggerFactory = null, |
| 21 | + ITextTokenizer? tokenizer = null, |
| 22 | + int maxToken = 6000) : ITextGenerator |
| 23 | +{ |
| 24 | + private readonly ILogger<DashScopeTextGenerator> _logger = loggerFactory?.CreateLogger<DashScopeTextGenerator>() |
| 25 | + ?? DefaultLogger<DashScopeTextGenerator>.Instance; |
| 26 | + |
| 27 | + /// <inheritdoc /> |
| 28 | + public int CountTokens(string text) |
| 29 | + { |
| 30 | + return tokenizer?.CountTokens(text) ?? QWenTokenizer.CountTokensStatic(text); |
| 31 | + } |
| 32 | + |
| 33 | + /// <inheritdoc /> |
| 34 | + public async IAsyncEnumerable<string> GenerateTextAsync( |
| 35 | + string prompt, |
| 36 | + TextGenerationOptions options, |
| 37 | + [EnumeratorCancellation] CancellationToken cancellationToken = new()) |
| 38 | + { |
| 39 | + var parameters = new TextGenerationParameters |
| 40 | + { |
| 41 | + TopP = options.TopP == 0 ? null : (float)options.TopP, |
| 42 | + Temperature = options.Temperature == 0 ? null : (float)options.Temperature, |
| 43 | + RepetitionPenalty = |
| 44 | + options.FrequencyPenalty == 0 ? null : ((float)options.FrequencyPenalty + 1), // dashScope's default value is 1.0, kernel memory is 0.0 |
| 45 | + MaxTokens = options.MaxTokens == 0 ? null : options.MaxTokens, |
| 46 | + Stop = options.StopSequences.ToArray(), |
| 47 | + IncrementalOutput = true, |
| 48 | + ResultFormat = ResultFormats.Text |
| 49 | + }; |
| 50 | + |
| 51 | + if (options.TokenSelectionBiases.Count != 0) |
| 52 | + { |
| 53 | + _logger.LogWarning("TokenSelectionBiases is not supported by DashScope and will be ignored"); |
| 54 | + } |
| 55 | + |
| 56 | + var request = new ModelRequest<TextGenerationInput, ITextGenerationParameters> |
| 57 | + { |
| 58 | + Model = modelId, |
| 59 | + Input = new TextGenerationInput { Prompt = prompt }, |
| 60 | + Parameters = parameters |
| 61 | + }; |
| 62 | + var tokens = dashScopeClient.GetTextCompletionStreamAsync(request, cancellationToken); |
| 63 | + await foreach (var token in tokens) |
| 64 | + { |
| 65 | + yield return token.Output.Text!; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// <inheritdoc /> |
| 70 | + public int MaxTokenTotal => maxToken; |
| 71 | +} |
0 commit comments