66using Cnblogs . DashScope . Sdk . QWen ;
77using Json . Schema ;
88using Json . Schema . Generation ;
9+ using Microsoft . Extensions . AI ;
10+ using ChatMessage = Cnblogs . DashScope . Core . ChatMessage ;
911
1012const string apiKey = "sk-**" ;
1113var dashScopeClient = new DashScopeClient ( apiKey ) ;
4244 case SampleType . ChatCompletionWithFiles :
4345 await ChatWithFilesAsync ( ) ;
4446 break ;
47+ case SampleType . MicrosoftExtensionsAi :
48+ await ChatWithMicrosoftExtensions ( ) ;
49+ break ;
4550}
4651
4752return ;
@@ -74,10 +79,11 @@ async Task ChatStreamAsync()
7479 Console . Write ( "user > " ) ;
7580 var input = Console . ReadLine ( ) ! ;
7681 history . Add ( ChatMessage . User ( input ) ) ;
77- var stream = dashScopeClient . GetQWenChatStreamAsync (
78- QWenLlm . QWenMax ,
79- history ,
80- new TextGenerationParameters { IncrementalOutput = true , ResultFormat = ResultFormats . Message } ) ;
82+ var stream = dashScopeClient
83+ . GetQWenChatStreamAsync (
84+ QWenLlm . QWenMax ,
85+ history ,
86+ new TextGenerationParameters { IncrementalOutput = true , ResultFormat = ResultFormats . Message } ) ;
8187 var role = string . Empty ;
8288 var message = new StringBuilder ( ) ;
8389 await foreach ( var modelResponse in stream )
@@ -164,10 +170,10 @@ async Task ChatWithToolsAsync()
164170 var toolCallMessage = response . Output . Choices ! [ 0 ] . Message ;
165171 history . Add ( toolCallMessage ) ;
166172 Console . WriteLine (
167- $ "{ toolCallMessage . Role } > { toolCallMessage . ToolCalls ! [ 0 ] . Function ! . Name } { toolCallMessage . ToolCalls [ 0 ] . Function ! . Arguments } ") ;
173+ $ "{ toolCallMessage . Role } > { toolCallMessage . ToolCalls ! [ 0 ] . Function . Name } { toolCallMessage . ToolCalls [ 0 ] . Function . Arguments } ") ;
168174
169175 var toolResponse = GetWeather (
170- JsonSerializer . Deserialize < WeatherReportParameters > ( toolCallMessage . ToolCalls [ 0 ] . Function ! . Arguments ! ) ! ) ;
176+ JsonSerializer . Deserialize < WeatherReportParameters > ( toolCallMessage . ToolCalls [ 0 ] . Function . Arguments ! ) ! ) ;
171177 var toolMessage = ChatMessage . Tool ( toolResponse , nameof ( GetWeather ) ) ;
172178 history . Add ( toolMessage ) ;
173179 Console . WriteLine ( $ "{ toolMessage . Role } > { toolMessage . Content } ") ;
@@ -186,3 +192,10 @@ string GetWeather(WeatherReportParameters parameters)
186192 } ;
187193 }
188194}
195+
196+ async Task ChatWithMicrosoftExtensions ( )
197+ {
198+ var chatClient = dashScopeClient . AsChatClient ( "qwen-max" ) ;
199+ var response = await chatClient . CompleteAsync ( "你好,很高兴认识你" ) ;
200+ Console . WriteLine ( JsonSerializer . Serialize ( response ) ) ;
201+ }
0 commit comments