Skip to content

Commit 29b6519

Browse files
authored
fix: Fix AI dialogue history (#1289)
1 parent 9119adc commit 29b6519

13 files changed

+186
-56
lines changed

src/intTest/java/com/box/sdk/BoxAIIT.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import static org.hamcrest.Matchers.containsString;
1111
import static org.hamcrest.Matchers.equalTo;
1212
import static org.hamcrest.Matchers.is;
13+
import static org.hamcrest.Matchers.notNullValue;
1314

1415
import com.eclipsesource.json.Json;
15-
import com.eclipsesource.json.JsonObject;
1616
import java.text.ParseException;
1717
import java.util.ArrayList;
1818
import java.util.Arrays;
@@ -111,8 +111,8 @@ public void askAIMultipleItems() throws InterruptedException {
111111
public void askAITextGenItemWithDialogueHistory() throws ParseException, InterruptedException {
112112
BoxAPIConnection api = jwtApiForServiceAccount();
113113
String fileName = "[askAITextGenItemWithDialogueHistory] Test File.txt";
114-
Date date1 = BoxDateFormat.parse("2013-05-16T15:27:57-07:00");
115-
Date date2 = BoxDateFormat.parse("2013-05-16T15:26:57-07:00");
114+
Date date1 = BoxDateFormat.parse("2021-01-01T00:00:00Z");
115+
Date date2 = BoxDateFormat.parse("2021-02-01T00:00:00Z");
116116

117117
BoxFile uploadedFile = uploadFileToUniqueFolder(api, fileName, "Test file");
118118
try {
@@ -148,28 +148,25 @@ public void askAITextGenItemWithDialogueHistory() throws ParseException, Interru
148148
@Test
149149
public void getAIAgentDefaultConfiguration() {
150150
BoxAPIConnection api = jwtApiForServiceAccount();
151-
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.ASK,
152-
"en", "openai__gpt_3_5_turbo");
151+
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.ASK);
153152
BoxAIAgentAsk askAgent = (BoxAIAgentAsk) agent;
154153

155154
assertThat(askAgent.getType(), is(equalTo(BoxAIAgentAsk.TYPE)));
156-
assertThat(askAgent.getBasicText().getModel(), is(equalTo("openai__gpt_3_5_turbo")));
155+
assertThat(askAgent.getBasicText().getModel(), is(notNullValue()));
157156

158-
BoxAIAgent agent2 = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.TEXT_GEN,
159-
"en", "openai__gpt_3_5_turbo");
157+
BoxAIAgent agent2 = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.TEXT_GEN);
160158
BoxAIAgentTextGen textGenAgent = (BoxAIAgentTextGen) agent2;
161159

162160
assertThat(textGenAgent.getType(), is(equalTo(BoxAIAgentTextGen.TYPE)));
163-
assertThat(textGenAgent.getBasicGen().getModel(), is(equalTo("openai__gpt_3_5_turbo")));
161+
assertThat(textGenAgent.getBasicGen().getModel(), is(notNullValue()));
164162
}
165163

166164
@Test
167165
public void askAISingleItemWithAgent() throws InterruptedException {
168166
BoxAPIConnection api = jwtApiForServiceAccount();
169167
String fileName = "[askAISingleItem] Test File.txt";
170168
BoxFile uploadedFile = uploadFileToUniqueFolder(api, fileName, "Test file");
171-
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.ASK,
172-
"en", "openai__gpt_3_5_turbo_16k");
169+
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.ASK);
173170
BoxAIAgentAsk askAgent = (BoxAIAgentAsk) agent;
174171

175172
try {
@@ -199,8 +196,10 @@ public void askAISingleItemWithAgent() throws InterruptedException {
199196
@Test
200197
public void aiExtract() throws InterruptedException {
201198
BoxAPIConnection api = jwtApiForServiceAccount();
202-
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.EXTRACT, "en-US", null);
199+
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.EXTRACT);
203200
BoxAIAgentExtract agentExtract = (BoxAIAgentExtract) agent;
201+
// AI team is going to move away from supporting overriding embeddings model
202+
agentExtract.getLongText().setEmbeddings(null);
204203

205204
BoxFile uploadedFile = uploadFileToUniqueFolder(api, "[aiExtract] Test File.txt",
206205
"My name is John Doe. I live in San Francisco. I was born in 1990. I work at Box.");
@@ -224,8 +223,10 @@ public void aiExtract() throws InterruptedException {
224223
@Test
225224
public void aiExtractStructuredWithFields() throws InterruptedException {
226225
BoxAPIConnection api = jwtApiForServiceAccount();
227-
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.EXTRACT_STRUCTURED, "en-US", null);
226+
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.EXTRACT_STRUCTURED);
228227
BoxAIAgentExtractStructured agentExtractStructured = (BoxAIAgentExtractStructured) agent;
228+
// AI team is going to move away from supporting overriding embeddings model
229+
agentExtractStructured.getLongText().setEmbeddings(null);
229230

230231
BoxFile uploadedFile = uploadFileToUniqueFolder(api, "[aiExtractStructuredWithFields] Test File.txt",
231232
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar.");
@@ -259,12 +260,16 @@ public void aiExtractStructuredWithFields() throws InterruptedException {
259260
"What is your hobby?")
260261
),
261262
agentExtractStructured);
262-
JsonObject sourceJson = response.getSourceJson();
263-
assertThat(sourceJson.get("firstName").asString(), is(equalTo("John")));
264-
assertThat(sourceJson.get("lastName").asString(), is(equalTo("Doe")));
265-
assertThat(sourceJson.get("dateOfBirth").asString(), is(equalTo("1990-07-04")));
266-
assertThat(sourceJson.get("age").asInt(), is(equalTo(34)));
267-
assertThat(sourceJson.get("hobby").asArray().get(0).asString(), is(equalTo("guitar")));
263+
assertThat(response.getSourceJson().get("answer"), is(equalTo(response.getAnswer())));
264+
265+
assertThat(response.getAnswer().get("firstName").asString(), is(equalTo("John")));
266+
assertThat(response.getAnswer().get("lastName").asString(), is(equalTo("Doe")));
267+
assertThat(response.getAnswer().get("dateOfBirth").asString(), is(equalTo("1990-07-04")));
268+
assertThat(response.getAnswer().get("age").asInt(), is(equalTo(34)));
269+
assertThat(response.getAnswer().get("hobby").asArray().get(0).asString(), is(equalTo("guitar")));
270+
271+
assertThat(response.getCompletionReason(), equalTo("done"));
272+
assertThat(response.getCreatedAt(), is(notNullValue()));
268273
}, 2, 2000);
269274
} finally {
270275
deleteFile(uploadedFile);
@@ -274,8 +279,10 @@ public void aiExtractStructuredWithFields() throws InterruptedException {
274279
@Test
275280
public void aiExtractStructuredWithMetadataTemplate() throws InterruptedException {
276281
BoxAPIConnection api = jwtApiForServiceAccount();
277-
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.EXTRACT_STRUCTURED, "en-US", null);
282+
BoxAIAgent agent = BoxAI.getAiAgentDefaultConfig(api, BoxAIAgent.Mode.EXTRACT_STRUCTURED);
278283
BoxAIAgentExtractStructured agentExtractStructured = (BoxAIAgentExtractStructured) agent;
284+
// AI team is going to move away from supporting overriding embeddings model
285+
agentExtractStructured.getLongText().setEmbeddings(null);
279286

280287
BoxFile uploadedFile = uploadFileToUniqueFolder(api, "[aiExtractStructuredWithMetadataTemplate] Test File.txt",
281288
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar.");
@@ -312,12 +319,15 @@ public void aiExtractStructuredWithMetadataTemplate() throws InterruptedExceptio
312319
Collections.singletonList(new BoxAIItem(uploadedFile.getID(), BoxAIItem.Type.FILE)),
313320
new BoxAIExtractMetadataTemplate(templateKey, "enterprise"),
314321
agentExtractStructured);
315-
JsonObject sourceJson = response.getSourceJson();
316-
assertThat(sourceJson.get("firstName").asString(), is(equalTo("John")));
317-
assertThat(sourceJson.get("lastName").asString(), is(equalTo("Doe")));
318-
assertThat(sourceJson.get("dateOfBirth").asString(), is(equalTo("1990-07-04T00:00:00Z")));
319-
assertThat(sourceJson.get("age").asInt(), is(equalTo(34)));
320-
assertThat(sourceJson.get("hobby").asArray().get(0).asString(), is(equalTo("guitar")));
322+
assertThat(response.getSourceJson().get("answer"), is(equalTo(response.getAnswer())));
323+
324+
assertThat(response.getAnswer().get("firstName").asString(), is(equalTo("John")));
325+
assertThat(response.getAnswer().get("lastName").asString(), is(equalTo("Doe")));
326+
assertThat(response.getAnswer().get("dateOfBirth").asString(), is(equalTo("1990-07-04T00:00:00Z")));
327+
assertThat(response.getAnswer().get("age").asInt(), is(equalTo(34)));
328+
assertThat(response.getAnswer().get("hobby").asArray().get(0).asString(), is(equalTo("guitar")));
329+
assertThat(response.getCompletionReason(), equalTo("done"));
330+
assertThat(response.getCreatedAt(), is(notNullValue()));
321331
}, 2, 2000);
322332
} finally {
323333
deleteFile(uploadedFile);

src/main/java/com/box/sdk/BoxAIAgentAsk.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,18 @@ void parseJSONMember(JsonObject.Member member) {
151151
public JsonObject getJSONObject() {
152152
JsonObject jsonObject = new JsonObject();
153153
JsonUtils.addIfNotNull(jsonObject, "type", this.getType());
154-
JsonUtils.addIfNotNull(jsonObject, "basic_text", this.basicText.getJSONObject());
155-
JsonUtils.addIfNotNull(jsonObject, "basic_text_multi", this.basicTextMulti.getJSONObject());
156-
JsonUtils.addIfNotNull(jsonObject, "long_text", this.longText.getJSONObject());
157-
JsonUtils.addIfNotNull(jsonObject, "long_text_multi", this.longTextMulti.getJSONObject());
154+
if (this.basicText != null) {
155+
jsonObject.add("basic_text", this.basicText.getJSONObject());
156+
}
157+
if (this.basicTextMulti != null) {
158+
jsonObject.add("basic_text_multi", this.basicTextMulti.getJSONObject());
159+
}
160+
if (this.longText != null) {
161+
jsonObject.add("long_text", this.longText.getJSONObject());
162+
}
163+
if (this.longTextMulti != null) {
164+
jsonObject.add("long_text_multi", this.longTextMulti.getJSONObject());
165+
}
158166
return jsonObject;
159167
}
160168
}

src/main/java/com/box/sdk/BoxAIAgentAskBasicText.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ void parseJSONMember(JsonObject.Member member) {
175175

176176
public JsonObject getJSONObject() {
177177
JsonObject jsonObject = new JsonObject();
178-
JsonUtils.addIfNotNull(jsonObject, "llm_endpoint_params", this.llmEndpointParams.getJSONObject());
178+
if (this.llmEndpointParams != null) {
179+
jsonObject.add("llm_endpoint_params", this.llmEndpointParams.getJSONObject());
180+
}
179181
JsonUtils.addIfNotNull(jsonObject, "model", this.model);
180182
JsonUtils.addIfNotNull(jsonObject, "num_tokens_for_completion", this.numTokensForCompletion);
181183
JsonUtils.addIfNotNull(jsonObject, "prompt_template", this.promptTemplate);

src/main/java/com/box/sdk/BoxAIAgentAskLongText.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ void parseJSONMember(JsonObject.Member member) {
201201

202202
public JsonObject getJSONObject() {
203203
JsonObject jsonObject = new JsonObject();
204-
JsonUtils.addIfNotNull(jsonObject, "embeddings", this.embeddings.getJSONObject());
205-
JsonUtils.addIfNotNull(jsonObject, "llm_endpoint_params", this.llmEndpointParams.getJSONObject());
204+
if (this.embeddings != null) {
205+
jsonObject.add("embeddings", this.embeddings.getJSONObject());
206+
}
207+
if (this.llmEndpointParams != null) {
208+
jsonObject.add("llm_endpoint_params", this.llmEndpointParams.getJSONObject());
209+
}
206210
JsonUtils.addIfNotNull(jsonObject, "model", this.model);
207211
JsonUtils.addIfNotNull(jsonObject, "num_tokens_for_completion", this.numTokensForCompletion);
208212
JsonUtils.addIfNotNull(jsonObject, "prompt_template", this.promptTemplate);

src/main/java/com/box/sdk/BoxAIAgentEmbeddings.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ void parseJSONMember(JsonObject.Member member) {
8787
public JsonObject getJSONObject() {
8888
JsonObject jsonObject = new JsonObject();
8989
JsonUtils.addIfNotNull(jsonObject, "model", this.model);
90-
JsonUtils.addIfNotNull(jsonObject, "strategy", this.strategy.getJSONObject());
90+
if (this.strategy != null) {
91+
jsonObject.add("strategy", this.strategy.getJSONObject());
92+
}
9193
return jsonObject;
9294
}
9395
}

src/main/java/com/box/sdk/BoxAIAgentExtract.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ void parseJSONMember(JsonObject.Member member) {
101101
public JsonObject getJSONObject() {
102102
JsonObject jsonObject = new JsonObject();
103103
JsonUtils.addIfNotNull(jsonObject, "type", this.getType());
104-
JsonUtils.addIfNotNull(jsonObject, "basic_text", this.basicText.getJSONObject());
105-
JsonUtils.addIfNotNull(jsonObject, "long_text", this.longText.getJSONObject());
104+
if (this.basicText != null) {
105+
jsonObject.add("basic_text", this.basicText.getJSONObject());
106+
}
107+
if (this.longText != null) {
108+
jsonObject.add("long_text", this.longText.getJSONObject());
109+
}
106110
return jsonObject;
107111
}
108112
}

src/main/java/com/box/sdk/BoxAIAgentExtractStructured.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ void parseJSONMember(JsonObject.Member member) {
101101
public JsonObject getJSONObject() {
102102
JsonObject jsonObject = new JsonObject();
103103
JsonUtils.addIfNotNull(jsonObject, "type", this.getType());
104-
JsonUtils.addIfNotNull(jsonObject, "basic_text", this.basicText.getJSONObject());
105-
JsonUtils.addIfNotNull(jsonObject, "long_text", this.longText.getJSONObject());
104+
if (this.basicText != null) {
105+
jsonObject.add("basic_text", this.basicText.getJSONObject());
106+
}
107+
if (this.longText != null) {
108+
jsonObject.add("long_text", this.longText.getJSONObject());
109+
}
106110
return jsonObject;
107111
}
108112
}

src/main/java/com/box/sdk/BoxAIAgentTextGen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ void parseJSONMember(JsonObject.Member member) {
6464
public JsonObject getJSONObject() {
6565
JsonObject jsonObject = new JsonObject();
6666
JsonUtils.addIfNotNull(jsonObject, "type", this.getType());
67-
JsonUtils.addIfNotNull(jsonObject, "basic_gen", this.basicGen.getJSONObject());
67+
if (this.basicGen != null) {
68+
jsonObject.add("basic_gen", this.basicGen.getJSONObject());
69+
}
6870
return jsonObject;
6971
}
7072
}

src/main/java/com/box/sdk/BoxAIAgentTextGenBasicGen.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,12 @@ void parseJSONMember(JsonObject.Member member) {
224224
public JsonObject getJSONObject() {
225225
JsonObject jsonObject = new JsonObject();
226226
JsonUtils.addIfNotNull(jsonObject, "content_template", this.contentTemplate);
227-
JsonUtils.addIfNotNull(jsonObject, "embeddings", this.embeddings.getJSONObject());
228-
JsonUtils.addIfNotNull(jsonObject, "llm_endpoint_params", this.llmEndpointParams.getJSONObject());
227+
if (this.embeddings != null) {
228+
jsonObject.add("embeddings", this.embeddings.getJSONObject());
229+
}
230+
if (this.llmEndpointParams != null) {
231+
jsonObject.add("llm_endpoint_params", this.llmEndpointParams.getJSONObject());
232+
}
229233
JsonUtils.addIfNotNull(jsonObject, "model", this.model);
230234
JsonUtils.addIfNotNull(jsonObject, "num_tokens_for_completion", this.numTokensForCompletion);
231235
JsonUtils.addIfNotNull(jsonObject, "prompt_template", this.promptTemplate);

src/main/java/com/box/sdk/BoxAIDialogueEntry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public void setCreatedAt(Date createdAt) {
9494
*/
9595
public JsonObject getJSONObject() {
9696
JsonObject itemJSON = new JsonObject()
97-
.add("id", this.prompt)
98-
.add("type", this.answer);
97+
.add("prompt", this.prompt)
98+
.add("answer", this.answer);
9999

100100
if (this.createdAt != null) {
101-
itemJSON.add("content", this.createdAt.toString());
101+
itemJSON.add("created_at", BoxDateFormat.format(this.createdAt));
102102
}
103103

104104
return itemJSON;

0 commit comments

Comments
 (0)