22package com .alibaba .dashscope .app ;
33
44import static com .alibaba .dashscope .utils .ApiKeywords .HISTORY ;
5+ import static com .alibaba .dashscope .utils .ApiKeywords .MESSAGES ;
56import static com .alibaba .dashscope .utils .ApiKeywords .PROMPT ;
67
78import com .alibaba .dashscope .base .HalfDuplexParamBase ;
89import com .alibaba .dashscope .common .History ;
10+ import com .alibaba .dashscope .common .Message ;
11+ import com .alibaba .dashscope .common .Role ;
912import com .alibaba .dashscope .exception .InputRequiredException ;
1013import com .alibaba .dashscope .utils .ApiKeywords ;
1114import com .alibaba .dashscope .utils .JsonUtils ;
@@ -42,6 +45,9 @@ public class ApplicationParam extends HalfDuplexParamBase {
4245 /** chat history */
4346 private List <History > history ;
4447
48+ /** chat message */
49+ private List <Message > messages ;
50+
4551 /** Session id for storing chat history note: this will be ignored if history passed */
4652 private String sessionId ;
4753
@@ -164,13 +170,27 @@ public JsonObject getHttpBody() {
164170 public JsonObject getInput () {
165171 JsonObject input = new JsonObject ();
166172
167- input .addProperty (PROMPT , getPrompt ());
168173 input .addProperty (AppKeywords .SESSION_ID , getSessionId ());
169174 input .addProperty (AppKeywords .MEMORY_ID , memoryId );
170175
171- if (history != null && !history .isEmpty ()) {
172- JsonArray historyJson = JsonUtils .toJsonElement (history ).getAsJsonArray ();
176+ if (getMessages () != null && !getMessages ().isEmpty ()) {
177+ JsonArray messagesJson = new JsonArray ();
178+ messagesJson .addAll (JsonUtils .toJsonArray (getMessages ()));
179+ if (getPrompt () != null ) {
180+ Message msg = Message .builder ().role (Role .USER .getValue ()).content (getPrompt ()).build ();
181+ messagesJson .add (JsonUtils .toJsonElement (msg ));
182+ }
183+ input .add (MESSAGES , messagesJson );
184+ } else if (getHistory () != null && !getHistory ().isEmpty ()) {
185+ JsonArray historyJson = JsonUtils .toJsonElement (getHistory ()).getAsJsonArray ();
173186 input .add (HISTORY , historyJson );
187+ if (getPrompt () != null ) {
188+ input .addProperty (PROMPT , getPrompt ());
189+ }
190+ } else {
191+ if (getPrompt () != null ) {
192+ input .addProperty (PROMPT , getPrompt ());
193+ }
174194 }
175195
176196 if (bizParams != null ) {
@@ -197,8 +217,8 @@ public ByteBuffer getBinaryData() {
197217
198218 @ Override
199219 public void validate () throws InputRequiredException {
200- if (getPrompt () == null ) {
201- throw new InputRequiredException ("prompt must not be null" );
220+ if (getPrompt () == null && ( getMessages () == null || getMessages (). isEmpty ()) ) {
221+ throw new InputRequiredException ("prompt or messages must not be null" );
202222 }
203223 }
204224}
0 commit comments