66using Cnblogs . DashScope . Sdk . QWen ;
77using Json . Schema ;
88using Json . Schema . Generation ;
9+ using Microsoft . Extensions . AI ;
910
10- const string apiKey = "sk-**" ;
11- var dashScopeClient = new DashScopeClient ( apiKey ) ;
11+ Console . WriteLine ( "Reading key from environment variable DASHSCOPE_KEY" ) ;
12+ var apiKey = Environment . GetEnvironmentVariable ( "DASHSCOPE_API_KEY" ) ;
13+ if ( string . IsNullOrEmpty ( apiKey ) )
14+ {
15+ Console . Write ( "ApiKey > " ) ;
16+ apiKey = Console . ReadLine ( ) ;
17+ }
18+
19+ var dashScopeClient = new DashScopeClient ( apiKey ! ) ;
1220
1321Console . WriteLine ( "Choose the sample you want to run:" ) ;
1422foreach ( var sampleType in Enum . GetValues < SampleType > ( ) )
4250 case SampleType . ChatCompletionWithFiles :
4351 await ChatWithFilesAsync ( ) ;
4452 break ;
53+ case SampleType . MicrosoftExtensionsAi :
54+ await ChatWithMicrosoftExtensions ( ) ;
55+ break ;
56+ case SampleType . MicrosoftExtensionsAiToolCall :
57+ await dashScopeClient . ToolCallWithExtensionAsync ( ) ;
58+ break ;
4559}
4660
4761return ;
@@ -68,16 +82,17 @@ async Task TextCompletionStreamAsync(string prompt)
6882
6983async Task ChatStreamAsync ( )
7084{
71- var history = new List < ChatMessage > ( ) ;
85+ var history = new List < TextChatMessage > ( ) ;
7286 while ( true )
7387 {
7488 Console . Write ( "user > " ) ;
7589 var input = Console . ReadLine ( ) ! ;
76- history . Add ( ChatMessage . User ( input ) ) ;
77- var stream = dashScopeClient . GetQWenChatStreamAsync (
78- QWenLlm . QWenMax ,
79- history ,
80- new TextGenerationParameters { IncrementalOutput = true , ResultFormat = ResultFormats . Message } ) ;
90+ history . Add ( TextChatMessage . User ( input ) ) ;
91+ var stream = dashScopeClient
92+ . GetQWenChatStreamAsync (
93+ QWenLlm . QWenMax ,
94+ history ,
95+ new TextGenerationParameters { IncrementalOutput = true , ResultFormat = ResultFormats . Message } ) ;
8196 var role = string . Empty ;
8297 var message = new StringBuilder ( ) ;
8398 await foreach ( var modelResponse in stream )
@@ -94,25 +109,25 @@ async Task ChatStreamAsync()
94109 }
95110
96111 Console . WriteLine ( ) ;
97- history . Add ( new ChatMessage ( role , message . ToString ( ) ) ) ;
112+ history . Add ( new TextChatMessage ( role , message . ToString ( ) ) ) ;
98113 }
99114
100115 // ReSharper disable once FunctionNeverReturns
101116}
102117
103118async Task ChatWithFilesAsync ( )
104119{
105- var history = new List < ChatMessage > ( ) ;
120+ var history = new List < TextChatMessage > ( ) ;
106121 Console . WriteLine ( "uploading file \" test.txt\" " ) ;
107122 var file = new FileInfo ( "test.txt" ) ;
108123 var uploadedFile = await dashScopeClient . UploadFileAsync ( file . OpenRead ( ) , file . Name ) ;
109124 Console . WriteLine ( "file uploaded, id: " + uploadedFile . Id ) ;
110125 Console . WriteLine ( ) ;
111126
112- var fileMessage = ChatMessage . File ( uploadedFile . Id ) ;
127+ var fileMessage = TextChatMessage . File ( uploadedFile . Id ) ;
113128 history . Add ( fileMessage ) ;
114129 Console . WriteLine ( "system > " + fileMessage . Content ) ;
115- var userPrompt = ChatMessage . User ( "该文件的内容是什么" ) ;
130+ var userPrompt = TextChatMessage . User ( "该文件的内容是什么" ) ;
116131 history . Add ( userPrompt ) ;
117132 Console . WriteLine ( "user > " + userPrompt . Content ) ;
118133 var stream = dashScopeClient . GetQWenChatStreamAsync (
@@ -135,7 +150,7 @@ async Task ChatWithFilesAsync()
135150 }
136151
137152 Console . WriteLine ( ) ;
138- history . Add ( new ChatMessage ( role , message . ToString ( ) ) ) ;
153+ history . Add ( new TextChatMessage ( role , message . ToString ( ) ) ) ;
139154
140155 Console . WriteLine ( ) ;
141156 Console . WriteLine ( "Deleting file by id: " + uploadedFile . Id ) ;
@@ -145,7 +160,7 @@ async Task ChatWithFilesAsync()
145160
146161async Task ChatWithToolsAsync ( )
147162{
148- var history = new List < ChatMessage > ( ) ;
163+ var history = new List < TextChatMessage > ( ) ;
149164 var tools = new List < ToolDefinition >
150165 {
151166 new (
@@ -156,19 +171,19 @@ async Task ChatWithToolsAsync()
156171 new JsonSchemaBuilder ( ) . FromType < WeatherReportParameters > ( ) . Build ( ) ) )
157172 } ;
158173 var chatParameters = new TextGenerationParameters ( ) { ResultFormat = ResultFormats . Message , Tools = tools } ;
159- var question = ChatMessage . User ( "请问现在杭州的天气如何?" ) ;
174+ var question = TextChatMessage . User ( "请问现在杭州的天气如何?" ) ;
160175 history . Add ( question ) ;
161176 Console . WriteLine ( $ "{ question . Role } > { question . Content } ") ;
162177
163178 var response = await dashScopeClient . GetQWenChatCompletionAsync ( QWenLlm . QWenMax , history , chatParameters ) ;
164179 var toolCallMessage = response . Output . Choices ! [ 0 ] . Message ;
165180 history . Add ( toolCallMessage ) ;
166181 Console . WriteLine (
167- $ "{ toolCallMessage . Role } > { toolCallMessage . ToolCalls ! [ 0 ] . Function ! . Name } { toolCallMessage . ToolCalls [ 0 ] . Function ! . Arguments } ") ;
182+ $ "{ toolCallMessage . Role } > { toolCallMessage . ToolCalls ! [ 0 ] . Function . Name } { toolCallMessage . ToolCalls [ 0 ] . Function . Arguments } ") ;
168183
169184 var toolResponse = GetWeather (
170- JsonSerializer . Deserialize < WeatherReportParameters > ( toolCallMessage . ToolCalls [ 0 ] . Function ! . Arguments ! ) ! ) ;
171- var toolMessage = ChatMessage . Tool ( toolResponse , nameof ( GetWeather ) ) ;
185+ JsonSerializer . Deserialize < WeatherReportParameters > ( toolCallMessage . ToolCalls [ 0 ] . Function . Arguments ! ) ! ) ;
186+ var toolMessage = TextChatMessage . Tool ( toolResponse , nameof ( GetWeather ) ) ;
172187 history . Add ( toolMessage ) ;
173188 Console . WriteLine ( $ "{ toolMessage . Role } > { toolMessage . Content } ") ;
174189
@@ -186,3 +201,17 @@ string GetWeather(WeatherReportParameters parameters)
186201 } ;
187202 }
188203}
204+
205+ async Task ChatWithMicrosoftExtensions ( )
206+ {
207+ Console . WriteLine ( "Requesting model..." ) ;
208+ var chatClient = dashScopeClient . AsChatClient ( "qwen-max" ) ;
209+ List < ChatMessage > conversation =
210+ [
211+ new ( ChatRole . System , "You are a helpful AI assistant" ) ,
212+ new ( ChatRole . User , "What is AI?" )
213+ ] ;
214+ var response = await chatClient . CompleteAsync ( conversation ) ;
215+ var serializerOptions = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) { WriteIndented = true } ;
216+ Console . WriteLine ( JsonSerializer . Serialize ( response , serializerOptions ) ) ;
217+ }
0 commit comments