|
| 1 | +using System.Text.Json; |
| 2 | +using Cnblogs.DashScope.Core; |
| 3 | +using Json.Schema; |
| 4 | +using Json.Schema.Generation; |
| 5 | +using Microsoft.Extensions.AI; |
| 6 | +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; |
| 7 | + |
| 8 | +namespace Cnblogs.DashScope.Sdk; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// <see cref="IChatClient" /> implemented with DashScope. |
| 12 | +/// </summary> |
| 13 | +public sealed class DashScopeChatClient : IChatClient |
| 14 | +{ |
| 15 | + private readonly IDashScopeClient _dashScopeClient; |
| 16 | + private readonly string _modelId; |
| 17 | + |
| 18 | + private static readonly JsonSchema EmptyObjectSchema = |
| 19 | + JsonSchema.FromText("""{"type":"object","required":[],"properties":{}}"""); |
| 20 | + |
| 21 | + private static readonly TextGenerationParameters DefaultTextGenerationParameter = new() { ResultFormat = "message" }; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initialize a new instance of the |
| 25 | + /// </summary> |
| 26 | + /// <param name="dashScopeClient"></param> |
| 27 | + /// <param name="modelId"></param> |
| 28 | + public DashScopeChatClient(IDashScopeClient dashScopeClient, string modelId) |
| 29 | + { |
| 30 | + ArgumentNullException.ThrowIfNull(dashScopeClient, nameof(dashScopeClient)); |
| 31 | + ArgumentNullException.ThrowIfNull(modelId, nameof(modelId)); |
| 32 | + |
| 33 | + _dashScopeClient = dashScopeClient; |
| 34 | + _modelId = modelId; |
| 35 | + Metadata = new ChatClientMetadata("dashscope", _dashScopeClient.BaseAddress, _modelId); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets or sets <see cref="JsonSerializerOptions"/> to use for any serialization activities related to tool call arguments and results. |
| 40 | + /// </summary> |
| 41 | + public JsonSerializerOptions ToolCallJsonSerializerOptions { get; set; } = new(JsonSerializerDefaults.Web); |
| 42 | + |
| 43 | + /// <inheritdoc /> |
| 44 | + public async Task<ChatCompletion> CompleteAsync( |
| 45 | + IList<ChatMessage> chatMessages, |
| 46 | + ChatOptions? options = null, |
| 47 | + CancellationToken cancellationToken = default) |
| 48 | + { |
| 49 | + var useVl = options?.AdditionalProperties?.GetValueOrDefault("useVl") ?? false; |
| 50 | + if (useVl) |
| 51 | + { |
| 52 | + var response = await _dashScopeClient.GetMultimodalGenerationAsync( |
| 53 | + new ModelRequest<MultimodalInput, IMultimodalParameters>() |
| 54 | + { |
| 55 | + Input = new MultimodalInput() { Messages = } |
| 56 | + }) |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + var parameters = options |
| 61 | + var response = await _dashScopeClient.GetTextCompletionAsync( |
| 62 | + new ModelRequest<TextGenerationInput, ITextGenerationParameters>() |
| 63 | + { |
| 64 | + Input = new TextGenerationInput() |
| 65 | + { |
| 66 | + Messages = chatMessages.SelectMany(ToTextChatMessages), |
| 67 | + Tools = ToToolDefinitions(options?.Tools) |
| 68 | + }, |
| 69 | + Model = _modelId, |
| 70 | + Parameters = |
| 71 | + }) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <inheritdoc /> |
| 76 | + public IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync( |
| 77 | + IList<ChatMessage> chatMessages, |
| 78 | + ChatOptions? options = null, |
| 79 | + CancellationToken cancellationToken = default) |
| 80 | + { |
| 81 | + throw new NotImplementedException(); |
| 82 | + } |
| 83 | + |
| 84 | + /// <inheritdoc /> |
| 85 | + public object? GetService(Type serviceType, object? serviceKey = null) |
| 86 | + { |
| 87 | + throw new NotImplementedException(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <inheritdoc /> |
| 91 | + public void Dispose() |
| 92 | + { |
| 93 | + // nothing to dispose. |
| 94 | + } |
| 95 | + |
| 96 | + /// <inheritdoc /> |
| 97 | + public ChatClientMetadata Metadata { get; } |
| 98 | + |
| 99 | + private IEnumerable<Cnblogs.DashScope.Core.ChatMessage> ToTextChatMessages(ChatMessage from) |
| 100 | + { |
| 101 | + if (from.Role == ChatRole.System || from.Role == ChatRole.User) |
| 102 | + { |
| 103 | + yield return new Core.ChatMessage(from.Role.Value, from.Text ?? string.Empty, from.AuthorName); |
| 104 | + } |
| 105 | + else if (from.Role == ChatRole.Tool) |
| 106 | + { |
| 107 | + foreach (var content in from.Contents) |
| 108 | + { |
| 109 | + if (content is FunctionResultContent resultContent) |
| 110 | + { |
| 111 | + var result = resultContent.Result as string; |
| 112 | + if (result is null && resultContent.Result is not null) |
| 113 | + { |
| 114 | + try |
| 115 | + { |
| 116 | + result = JsonSerializer.Serialize(resultContent.Result, ToolCallJsonSerializerOptions); |
| 117 | + } |
| 118 | + catch (NotSupportedException) |
| 119 | + { |
| 120 | + // If the type can't be serialized, skip it. |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + yield return new Core.ChatMessage(from.Role.Value, result ?? string.Empty); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + else if (from.Role == ChatRole.Assistant) |
| 129 | + { |
| 130 | + var functionCall = from.Contents |
| 131 | + .OfType<FunctionCallContent>() |
| 132 | + .Select( |
| 133 | + c => new ToolCall( |
| 134 | + c.CallId, |
| 135 | + "function", |
| 136 | + new FunctionCall(c.Name, JsonSerializer.Serialize(c.Arguments, ToolCallJsonSerializerOptions)))) |
| 137 | + .ToList(); |
| 138 | + yield return new Core.ChatMessage( |
| 139 | + from.Role.Value, |
| 140 | + from.Text ?? string.Empty, |
| 141 | + from.AuthorName, |
| 142 | + functionCall); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static TextGenerationParameters? ToTextGenerationParameters(ChatOptions? options) |
| 147 | + { |
| 148 | + if (options is null) |
| 149 | + { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + var format = "message"; |
| 154 | + if (options.ResponseFormat is ChatResponseFormatJson) |
| 155 | + { |
| 156 | + format = "json_object"; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + var parameter = new TextGenerationParameters() |
| 161 | + { |
| 162 | + ResultFormat = format, |
| 163 | + Temperature = options.Temperature, |
| 164 | + MaxTokens = options.MaxOutputTokens, |
| 165 | + TopP = options.TopP, |
| 166 | + TopK = options.TopK, |
| 167 | + RepetitionPenalty = options.FrequencyPenalty, |
| 168 | + Seed = options.Seed == null ? null : (ulong)options.Seed.Value, |
| 169 | + Stop = options.StopSequences == null ? null : new TextGenerationStop(options.StopSequences), |
| 170 | + Tools = options.Tools == null ? null : ToToolDefinitions(options.Tools), |
| 171 | + ToolChoice = options.ToolMode |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | + private static IEnumerable<ToolDefinition>? ToToolDefinitions(IList<AITool>? tools) |
| 176 | + { |
| 177 | + return tools?.OfType<AIFunction>().Select( |
| 178 | + f => new ToolDefinition( |
| 179 | + "function", |
| 180 | + new FunctionDefinition( |
| 181 | + f.Metadata.Name, |
| 182 | + f.Metadata.Description, |
| 183 | + GetParameterSchema(f.Metadata.Parameters)))); |
| 184 | + } |
| 185 | + |
| 186 | + private static JsonSchema GetParameterSchema(IEnumerable<AIFunctionParameterMetadata> metadata) |
| 187 | + { |
| 188 | + return new JsonSchemaBuilder() |
| 189 | + .Properties(metadata.Select(c => (c.Name, Schema: c.Schema as JsonSchema ?? EmptyObjectSchema)).ToArray()) |
| 190 | + .Build(); |
| 191 | + } |
| 192 | + |
| 193 | + private static List<ChatMessageContentPart> GetContentParts(IList<AIContent> contents) |
| 194 | + { |
| 195 | + List<ChatMessageContentPart> parts = []; |
| 196 | + foreach (var content in contents) |
| 197 | + { |
| 198 | + switch (content) |
| 199 | + { |
| 200 | + case TextContent textContent: |
| 201 | + parts.Add(ChatMessageContentPart.CreateTextPart(textContent.Text)); |
| 202 | + break; |
| 203 | + |
| 204 | + case ImageContent imageContent when imageContent.Data is { IsEmpty: false } data: |
| 205 | + parts.Add( |
| 206 | + ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(data), imageContent.MediaType)); |
| 207 | + break; |
| 208 | + |
| 209 | + case ImageContent imageContent when imageContent.Uri is string uri: |
| 210 | + parts.Add(ChatMessageContentPart.CreateImagePart(new Uri(uri))); |
| 211 | + break; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + if (parts.Count == 0) |
| 216 | + { |
| 217 | + parts.Add(ChatMessageContentPart.CreateTextPart(string.Empty)); |
| 218 | + } |
| 219 | + |
| 220 | + return parts; |
| 221 | + } |
| 222 | +} |
0 commit comments