|
| 1 | +using System.Text; |
| 2 | +using Cnblogs.DashScope.Core; |
| 3 | + |
| 4 | +namespace Cnblogs.DashScope.Sample.Multimodal; |
| 5 | + |
| 6 | +public class ImageInputSample : ISample |
| 7 | +{ |
| 8 | + /// <inheritdoc /> |
| 9 | + public string Description => "Chat with image input"; |
| 10 | + |
| 11 | + /// <inheritdoc /> |
| 12 | + public async Task RunAsync(IDashScopeClient client) |
| 13 | + { |
| 14 | + var messages = new List<MultimodalMessage>(); |
| 15 | + messages.Add( |
| 16 | + MultimodalMessage.User( |
| 17 | + [ |
| 18 | + MultimodalMessageContent.ImageContent( |
| 19 | + "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"), |
| 20 | + MultimodalMessageContent.ImageContent("https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"), |
| 21 | + MultimodalMessageContent.TextContent("这些图展现了什么内容?") |
| 22 | + ])); |
| 23 | + var completion = client.GetMultimodalGenerationStreamAsync( |
| 24 | + new ModelRequest<MultimodalInput, IMultimodalParameters>() |
| 25 | + { |
| 26 | + Model = "qwen3-vl-plus", |
| 27 | + Input = new MultimodalInput() { Messages = messages }, |
| 28 | + Parameters = new MultimodalParameters() |
| 29 | + { |
| 30 | + IncrementalOutput = true, |
| 31 | + EnableThinking = true, |
| 32 | + VlHighResolutionImages = true |
| 33 | + } |
| 34 | + }); |
| 35 | + var reply = new StringBuilder(); |
| 36 | + var reasoning = false; |
| 37 | + MultimodalTokenUsage? usage = null; |
| 38 | + await foreach (var chunk in completion) |
| 39 | + { |
| 40 | + var choice = chunk.Output.Choices[0]; |
| 41 | + if (string.IsNullOrEmpty(choice.Message.ReasoningContent) == false) |
| 42 | + { |
| 43 | + // reasoning |
| 44 | + if (reasoning == false) |
| 45 | + { |
| 46 | + Console.Write("Reasoning > "); |
| 47 | + reasoning = true; |
| 48 | + } |
| 49 | + |
| 50 | + Console.Write(choice.Message.ReasoningContent); |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + if (reasoning) |
| 55 | + { |
| 56 | + reasoning = false; |
| 57 | + Console.WriteLine(); |
| 58 | + Console.Write("Assistant > "); |
| 59 | + } |
| 60 | + |
| 61 | + if (choice.Message.Content.Count == 0) |
| 62 | + { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + Console.Write(choice.Message.Content[0].Text); |
| 67 | + reply.Append(choice.Message.Content[0].Text); |
| 68 | + usage = chunk.Usage; |
| 69 | + } |
| 70 | + |
| 71 | + Console.WriteLine(); |
| 72 | + messages.Add(MultimodalMessage.Assistant([MultimodalMessageContent.TextContent(reply.ToString())])); |
| 73 | + if (usage != null) |
| 74 | + { |
| 75 | + Console.WriteLine( |
| 76 | + $"Usage: in({usage.InputTokens})/out({usage.OutputTokens})/image({usage.ImageTokens})/reasoning({usage.OutputTokensDetails?.ReasoningTokens})/total({usage.TotalTokens})"); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments