@@ -7,11 +7,11 @@ English | [简体中文](https://github.com/cnblogs/dashscope-sdk/blob/main/READ
77
88An unofficial DashScope SDK maintained by Cnblogs.
99
10- # Usage
10+ # Quick Start
1111
1212## Console App
1313
14- Install Cnblogs.DashScope.Sdk package.
14+ Install ` Cnblogs.DashScope.Sdk ` package.
1515
1616``` csharp
1717var client = new DashScopeClient (" your-api-key" );
@@ -55,8 +55,109 @@ public class YourService(IDashScopeClient client)
5555- Text Generation API(qwen-turbo, qwen-max, etc.) - ` dashScopeClient.GetQwenCompletionAsync() ` and ` dashScopeClient.GetQWenCompletionStreamAsync() `
5656- BaiChuan Models - Use ` dashScopeClient.GetBaiChuanTextCompletionAsync() `
5757- LLaMa2 Models - ` dashScopeClient.GetLlama2TextCompletionAsync() `
58- - Multimodal Generation API(qwen-vl-max, etc.) - ` dashScopeClient.GetQWenMultimodalCompletionAsync ` and ` dashScopeClient.GetQWenMultimodalCompletionStreamAsync `
58+ - Multimodal Generation API(qwen-vl-max, etc.) - ` dashScopeClient.GetQWenMultimodalCompletionAsync() ` and ` dashScopeClient.GetQWenMultimodalCompletionStreamAsync() `
5959- Wanx Models(Image generation, background generation, etc)
6060 - Image Synthesis - ` CreateWanxImageSynthesisTaskAsync() ` and ` GetWanxImageSynthesisTaskAsync() `
61- - Image Generation - ` CreateWanxImageGenerationTaskAsync ` and ` GetWanxImageGenerationTaskAsync() `
62- - Background Image Generation - ` CreateWanxBackgroundGenerationTaskAsync ` and ` GetWanxBackgroundGenerationTaskAsync `
61+ - Image Generation - ` CreateWanxImageGenerationTaskAsync() ` and ` GetWanxImageGenerationTaskAsync() `
62+ - Background Image Generation - ` CreateWanxBackgroundGenerationTaskAsync() ` and ` GetWanxBackgroundGenerationTaskAsync() `
63+
64+
65+ # Examples
66+
67+ Visit [ tests] ( ./test ) for more usage of each api.
68+
69+ ## Single Text Completion
70+
71+ ``` csharp
72+ var prompt = " hello"
73+ var completion = await client .GetQWenCompletionAsync (QWenLlm .QWenMax , prompt );
74+ Console .WriteLine (completion .Output .Text );
75+ ```
76+
77+ ## Multi-round chat
78+
79+ ``` csharp
80+ var history = new List <ChatMessage >
81+ {
82+ new (" user" , " Please remember this number, 42" ),
83+ new (" assistant" , " I have remembered this number." ),
84+ new (" user" , " What was the number I metioned before?" )
85+ }
86+ var parameters = new TextGenerationParameters ()
87+ {
88+ ResultFormat = ResultFormats .Message
89+ };
90+ var completion = await client .GetQWenChatCompletionAsync (QWenLlm .QWenMax , history , parameters );
91+ Console .WriteLine (completion .Output .Choices [0 ].Message .Content ); // The number is 42
92+ ```
93+
94+ ## Function Call
95+
96+ Creates a function with parameters
97+
98+ ``` csharp
99+ string GetCurrentWeather (GetCurrentWeatherParameters parameters )
100+ {
101+ // actual implementation should be different.
102+ return " Sunny, 14" + parameters .Unit switch
103+ {
104+ TemperatureUnit .Celsius => " ℃" ,
105+ TemperatureUnit .Fahrenheit => " ℉"
106+ };
107+ }
108+
109+ public record GetCurrentWeatherParameters (
110+ [property : Required ]
111+ [property : Description (" The city and state, e.g. San Francisco, CA" )]
112+ string Location ,
113+ [property : JsonConverter (typeof (EnumStringConverter <TemperatureUnit >))]
114+ TemperatureUnit Unit = TemperatureUnit .Celsius );
115+
116+ public enum TemperatureUnit
117+ {
118+ Celsius ,
119+ Fahrenheit
120+ }
121+ ```
122+
123+ Append tool information to chat messages.
124+
125+ ``` csharp
126+ var tools = new List <ToolDefinition >()
127+ {
128+ new (
129+ ToolTypes .Function ,
130+ new FunctionDefinition (
131+ nameof (GetCurrentWeather ),
132+ " Get the weather abount given location" ,
133+ new JsonSchemaBuilder ().FromType <GetCurrentWeatherParameters >().Build ()))
134+ };
135+
136+ var history = new List <ChatMessage >
137+ {
138+ new (" user" , " What is the weather today in C.A?" )
139+ };
140+
141+ var parameters = new TextGenerationParamters ()
142+ {
143+ ResultFormat = ResultFormats .Message ,
144+ Tools = tools
145+ };
146+
147+ // send question with available tools.
148+ var completion = await client .GetQWenChatCompletionAsync (QWenLlm .QWenMax , history , parameters );
149+ history .Add (completion .Output .Choice [0 ].Message );
150+
151+ // model responding with tool calls.
152+ Console .WriteLine (completion .Output .Choice [0 ].Message .ToolCalls [0 ].Function .Name ); // GetCurrentWeather
153+
154+ // calling tool that model requests and append result into history.
155+ var result = GetCurrentWeather (JsonSerializer .Deserialize <GetCurrentWeatherParameters >(completion .Output .Choice [0 ].Message .ToolCalls [0 ].Function .Arguments ));
156+ history .Add (new (" tool" , result , nameof (GetCurrentWeather )));
157+
158+ // get back answers.
159+ completion = await client .GetQWenChatCompletionAsync (QWenLlm .QWenMax , history , parameters );
160+ Console .WriteLine (completion .Output .Choice [0 ].Message .Content );
161+ ```
162+
163+ Append the tool calling result with ` tool ` role, then model will generate answers based on tool calling result.
0 commit comments