@@ -16,141 +16,161 @@ To use the sdk in your java systems, please add the maven dependency in your pom
1616
1717## QuickStart
1818
19- ### Conversation
19+ ### Generation
2020
21- You can create a conversation client simply by:
21+ You can create a generation client simply by:
2222
2323``` java
24- // Use http as the network protocol.
25- Conversation conversation = new Conversation ();
24+ Generation generation = new Generation ();
2625```
2726
28- This interface also accept a protocol argument:
29-
30- ``` java
31- import com.alibaba.dashscope.common.Protocol ;
32- Conversation conversation = new Conversation (Protocol . HTTP. getValue());
33- Conversation conversation = new Conversation (Protocol . WEBSOCKET. getValue());
34- ```
35-
36- The conversation interface supports both stream and non-stream queries. These queries all accept ` ConversationParam ` as input, and returns ` ConversationResult ` as output . Each model has a unique input structure and output structure which derives from the two data classes mentioned above. Please use these sub classes when you are using the coordinating model.
27+ The generation interface supports both stream and non-stream queries. These queries all accept ` GenerationParam ` as input, and return ` GenerationResult ` as output.
3728
3829Here shows the usages of each method, with the examples of ` qwen-turbo ` model.
3930
4031#### Support stream and non-stream mode, accept output from callback
4132
4233``` java
34+ import com.alibaba.dashscope.aigc.generation.Generation ;
35+ import com.alibaba.dashscope.aigc.generation.GenerationParam ;
36+ import com.alibaba.dashscope.aigc.generation.GenerationResult ;
37+ import com.alibaba.dashscope.common.Message ;
4338import com.alibaba.dashscope.common.ResultCallback ;
44- import com.alibaba.dashscope.aigc.conversation.Conversation ;
45- import com.alibaba.dashscope.aigc.conversation.ConversationResult ;
46- import com.alibaba.dashscope.aigc.conversation.qwen.QWenConversationParam ;
47- import com.alibaba.dashscope.aigc.conversation.qwen.QWenConversationResult ;
39+ import com.alibaba.dashscope.common.Role ;
4840import com.alibaba.dashscope.exception.ApiException ;
41+ import com.alibaba.dashscope.utils.JsonUtils ;
42+ import java.util.Arrays ;
4943
5044public class Main {
5145
5246 public static void main (String [] args ) {
53- Conversation conversation = new Conversation ();
54-
55- // QWEN model, if using other model, you can replace the QWenConversationParam here.
56- QWenConversationParam param =
57- QWenConversationParam . builder()
58- .model(QWenConversationParam . QWEN_TURBO )
59- // there are other arguments supported.
60- .prompt(" hello" )
61- .apiKey(" testKey" )
62- .build();
63-
64- class ReactCallback extends ResultCallback<ConversationResult > {
65-
66- @Override
67- public void onEvent (ConversationResult message ) {
68- QWenConversationResult result = (QWenConversationResult ) message;
69- // TODO deal with result
47+ Generation generation = new Generation ();
48+ GenerationParam param = GenerationParam . builder()
49+ .apiKey(System . getenv(" DASHSCOPE_API_KEY" ))
50+ .model(Generation . Models . QWEN_TURBO )
51+ .messages(Arrays . asList(
52+ Message . builder()
53+ .role(Role . USER. getValue())
54+ .content(" Hello, how are you?" ). build()
55+ )). build();
56+
57+ class ReactCallback extends ResultCallback<GenerationResult > {
58+
59+ @Override
60+ public void onEvent (GenerationResult message ) {
61+ System . out. println(JsonUtils . toJson(message));
62+ }
63+
64+ public void onComplete () {
65+ // TODO all messages received
66+ }
67+
68+ public void onError (Exception e ) {
69+ ApiException apiException = (ApiException ) e;
70+ // TODO deal with exception
71+ }
7072 }
7173
72- public void onComplete () {
73- // TODO all messages received
74- }
75-
76- public void onError (Exception e ) {
77- ApiException apiException = (ApiException ) e;
78- // TODO deal with exception
79- }
74+ generation. call(param, new ReactCallback ());
8075 }
81- conversation. call(param, new ReactCallback ());
82- }
8376}
84-
8577```
8678
87- The Exception instance in the conversation scenario is a ` ApiException ` instance. This Exception may contain two parts:
79+ The Exception instance is an ` ApiException ` instance. This Exception may contain two parts:
8880
89- - A ` Status ` instance. This instance carries a status_code(The http error code), a code(server error code), a message(server error message), the input message id, and the usage information.
81+ - A ` Status ` instance. This instance carries a status_code (the http error code), a code (server error code), a message (server error message), the request id, and the usage information.
9082- If an exception occurs, the ` ApiException ` instance may only carry an ` Exception ` stack trace, you can deal with it as you usually do.
9183
92- #### Stream only, accept by react io
84+ #### Stream only, accept by reactive io
9385
9486``` java
95- import com.alibaba.dashscope.aigc.conversation.Conversation ;
96- import com.alibaba.dashscope.aigc.conversation.ConversationResult ;
97- import com.alibaba.dashscope.aigc.conversation.qwen.QWenConversationParam ;
98- import com.alibaba.dashscope.common.StreamingMode ;
87+ import com.alibaba.dashscope.aigc.generation.Generation ;
88+ import com.alibaba.dashscope.aigc.generation.GenerationParam ;
89+ import com.alibaba.dashscope.aigc.generation.GenerationResult ;
90+ import com.alibaba.dashscope.common.Message ;
91+ import com.alibaba.dashscope.common.Role ;
92+ import com.alibaba.dashscope.exception.ApiException ;
93+ import com.alibaba.dashscope.exception.InputRequiredException ;
94+ import com.alibaba.dashscope.exception.NoApiKeyException ;
9995import com.alibaba.dashscope.utils.JsonUtils ;
10096import io.reactivex.Flowable ;
97+ import java.util.Arrays ;
10198
10299public class Main {
103100
104101 public static void main (String [] args ) {
105- Conversation conversation = new Conversation ();
102+ Generation generation = new Generation ();
103+
104+ Message systemMsg = Message . builder()
105+ .role(Role . SYSTEM. getValue())
106+ .content(" You are a helpful assistant." )
107+ .build();
108+ Message userMsg = Message . builder()
109+ .role(Role . USER. getValue())
110+ .content(" Hello!" )
111+ .build();
112+ GenerationParam param = GenerationParam . builder()
113+ .apiKey(System . getenv(" DASHSCOPE_API_KEY" ))
114+ .model(Generation . Models . QWEN_TURBO )
115+ .messages(Arrays . asList(systemMsg, userMsg))
116+ .resultFormat(GenerationParam . ResultFormat . MESSAGE )
117+ .build();
106118
107- // QWEN model, if using other model, you can replace the QWenConversationParam here.
108- QWenConversationParam param =
109- QWenConversationParam . builder(). model(QWenConversationParam . QWEN_TURBO ). prompt(" hello" )
110- .mode(StreamingMode . OUT )
111- .apiKey(" testKey" )
112- .build();
113-
114- Flowable<ConversationResult > flowable = conversation. streamCall(param);
115119 try {
116- flowable. blockingForEach(msg - > System . out. println(JsonUtils . toJson(msg)));
117- } catch (Throwable e) {
118- // TODO deal with error here
120+ Flowable<GenerationResult > result = generation. streamCall(param);
121+ result. blockingForEach(msg - > System . out. println(JsonUtils . toJson(msg)));
122+ } catch (ApiException | NoApiKeyException | InputRequiredException e) {
123+ System . err. println(" An error occurred: " + e. getMessage());
119124 }
120125 }
121126}
122127```
123128
124- The ` streamCall ` method accepts a ` ConversationParam ` , and returns a ` Flowable ` , which you can get the streaming result by ` blockingForEach ` , and catch the exception by the try-catch block.
129+ The ` streamCall ` method accepts a ` GenerationParam ` , and returns a ` Flowable ` , which you can get the streaming result by ` blockingForEach ` , and catch the exception by the try-catch block.
125130
126131#### Non-stream only
127132
128133``` java
129- import com.alibaba.dashscope.aigc.conversation.Conversation ;
130- import com.alibaba.dashscope.aigc.conversation.ConversationResult ;
131- import com.alibaba.dashscope.aigc.conversation.qwen.QWenConversationParam ;
134+ import com.alibaba.dashscope.aigc.generation.Generation ;
135+ import com.alibaba.dashscope.aigc.generation.GenerationParam ;
136+ import com.alibaba.dashscope.aigc.generation.GenerationResult ;
137+ import com.alibaba.dashscope.common.Message ;
138+ import com.alibaba.dashscope.common.Role ;
139+ import com.alibaba.dashscope.exception.ApiException ;
140+ import com.alibaba.dashscope.exception.InputRequiredException ;
141+ import com.alibaba.dashscope.exception.NoApiKeyException ;
142+ import com.alibaba.dashscope.utils.JsonUtils ;
143+ import java.util.Arrays ;
132144
133145public class Main {
134146
135147 public static void main (String [] args ) {
136- Conversation conversation = new Conversation ();
148+ Generation generation = new Generation ();
149+
150+ Message systemMsg = Message . builder()
151+ .role(Role . SYSTEM. getValue())
152+ .content(" You are a helpful assistant." )
153+ .build();
154+ Message userMsg = Message . builder()
155+ .role(Role . USER. getValue())
156+ .content(" Hello!" )
157+ .build();
158+ GenerationParam param = GenerationParam . builder()
159+ .apiKey(System . getenv(" DASHSCOPE_API_KEY" ))
160+ .model(Generation . Models . QWEN_TURBO )
161+ .messages(Arrays . asList(systemMsg, userMsg))
162+ .resultFormat(GenerationParam . ResultFormat . MESSAGE )
163+ .build();
137164
138- // QWEN model, if using other model, you can replace the QWenConversationParam here.
139- QWenConversationParam param =
140- QWenConversationParam . builder()
141- .model(QWenConversationParam . QWEN_TURBO )
142- .prompt(" hello" )
143- .apiKey(" testKey" )
144- .build();
145-
146- ConversationResult result = conversation. call(param);
165+ try {
166+ GenerationResult result = generation. call(param);
167+ System . out. println(JsonUtils . toJson(result));
168+ } catch (ApiException | NoApiKeyException | InputRequiredException e) {
169+ System . err. println(" An error occurred: " + e. getMessage());
170+ }
147171 }
148172}
149173```
150174
151- The ` call ` method accepts a ` ConversationParam ` , and returns a ` ConversationResult ` , you can also catch the exception with a try-catch block.
152-
153-
154-
155-
175+ The ` call ` method accepts a ` GenerationParam ` , and returns a ` GenerationResult ` , you can also catch the exception with a try-catch block.
156176
0 commit comments