Skip to content

Commit

Permalink
feat: Update /ai/extract_structured response schema (box/box-codege…
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build authored Jan 30, 2025
1 parent 45021c2 commit d42892a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "6582211", "specHash": "3dc3f1e", "version": "0.3.0" }
{ "engineHash": "a74691d", "specHash": "1fdcbef", "version": "0.3.0" }
2 changes: 1 addition & 1 deletion docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ client.getAi().createAiExtractStructured(new AiExtractStructured.AiExtractStruct

### Returns

This function returns a value of type `AiExtractResponse`.
This function returns a value of type `AiExtractStructuredResponse`.

A successful response including the answer from the LLM.

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/box/sdkgen/internal/utils/UtilsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,15 @@ public static String createJwtAssertion(
}

public static JsonNode getValueFromObjectRawData(SerializableObject obj, String key) {
return obj.getRawData().get(key);
JsonNode value = obj.getRawData();
for (String k : key.split("\\.")) {
if (value == null || !value.has(k)) {
return null;
}
value = value.get(k);
}

return value;
}

public static double random(double min, double max) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/box/sdkgen/managers/ai/AiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import com.box.sdkgen.schemas.aiagentaskoraiagentextractoraiagentextractstructuredoraiagenttextgen.AiAgentAskOrAiAgentExtractOrAiAgentExtractStructuredOrAiAgentTextGen;
import com.box.sdkgen.schemas.aiask.AiAsk;
import com.box.sdkgen.schemas.aiextract.AiExtract;
import com.box.sdkgen.schemas.aiextractresponse.AiExtractResponse;
import com.box.sdkgen.schemas.aiextractstructured.AiExtractStructured;
import com.box.sdkgen.schemas.aiextractstructuredresponse.AiExtractStructuredResponse;
import com.box.sdkgen.schemas.airesponse.AiResponse;
import com.box.sdkgen.schemas.airesponsefull.AiResponseFull;
import com.box.sdkgen.schemas.aitextgen.AiTextGen;
Expand Down Expand Up @@ -145,11 +145,11 @@ public AiResponse createAiExtract(AiExtract requestBody, CreateAiExtractHeaders
return JsonManager.deserialize(response.getData(), AiResponse.class);
}

public AiExtractResponse createAiExtractStructured(AiExtractStructured requestBody) {
public AiExtractStructuredResponse createAiExtractStructured(AiExtractStructured requestBody) {
return createAiExtractStructured(requestBody, new CreateAiExtractStructuredHeaders());
}

public AiExtractResponse createAiExtractStructured(
public AiExtractStructuredResponse createAiExtractStructured(
AiExtractStructured requestBody, CreateAiExtractStructuredHeaders headers) {
Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
FetchResponse response =
Expand All @@ -169,7 +169,7 @@ public AiExtractResponse createAiExtractStructured(
.auth(this.auth)
.networkSession(this.networkSession)
.build());
return JsonManager.deserialize(response.getData(), AiExtractResponse.class);
return JsonManager.deserialize(response.getData(), AiExtractStructuredResponse.class);
}

public Authentication getAuth() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.box.sdkgen.schemas.aiextractstructuredresponse;

import com.box.sdkgen.internal.SerializableObject;
import com.box.sdkgen.schemas.aiagentinfo.AiAgentInfo;
import com.box.sdkgen.schemas.aiextractresponse.AiExtractResponse;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

public class AiExtractStructuredResponse extends SerializableObject {

protected final AiExtractResponse answer;

@JsonProperty("created_at")
protected final String createdAt;

@JsonProperty("completion_reason")
protected String completionReason;

@JsonProperty("ai_agent_info")
protected AiAgentInfo aiAgentInfo;

public AiExtractStructuredResponse(
@JsonProperty("answer") AiExtractResponse answer,
@JsonProperty("created_at") String createdAt) {
super();
this.answer = answer;
this.createdAt = createdAt;
}

protected AiExtractStructuredResponse(AiExtractStructuredResponseBuilder builder) {
super();
this.answer = builder.answer;
this.createdAt = builder.createdAt;
this.completionReason = builder.completionReason;
this.aiAgentInfo = builder.aiAgentInfo;
}

public AiExtractResponse getAnswer() {
return answer;
}

public String getCreatedAt() {
return createdAt;
}

public String getCompletionReason() {
return completionReason;
}

public AiAgentInfo getAiAgentInfo() {
return aiAgentInfo;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AiExtractStructuredResponse casted = (AiExtractStructuredResponse) o;
return Objects.equals(answer, casted.answer)
&& Objects.equals(createdAt, casted.createdAt)
&& Objects.equals(completionReason, casted.completionReason)
&& Objects.equals(aiAgentInfo, casted.aiAgentInfo);
}

@Override
public int hashCode() {
return Objects.hash(answer, createdAt, completionReason, aiAgentInfo);
}

@Override
public String toString() {
return "AiExtractStructuredResponse{"
+ "answer='"
+ answer
+ '\''
+ ", "
+ "createdAt='"
+ createdAt
+ '\''
+ ", "
+ "completionReason='"
+ completionReason
+ '\''
+ ", "
+ "aiAgentInfo='"
+ aiAgentInfo
+ '\''
+ "}";
}

public static class AiExtractStructuredResponseBuilder {

protected final AiExtractResponse answer;

protected final String createdAt;

protected String completionReason;

protected AiAgentInfo aiAgentInfo;

public AiExtractStructuredResponseBuilder(AiExtractResponse answer, String createdAt) {
this.answer = answer;
this.createdAt = createdAt;
}

public AiExtractStructuredResponseBuilder completionReason(String completionReason) {
this.completionReason = completionReason;
return this;
}

public AiExtractStructuredResponseBuilder aiAgentInfo(AiAgentInfo aiAgentInfo) {
this.aiAgentInfo = aiAgentInfo;
return this;
}

public AiExtractStructuredResponse build() {
return new AiExtractStructuredResponse(this);
}
}
}
29 changes: 16 additions & 13 deletions src/test/java/com/box/sdkgen/test/ai/AiITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import com.box.sdkgen.schemas.aiask.AiAskModeField;
import com.box.sdkgen.schemas.aidialoguehistory.AiDialogueHistory;
import com.box.sdkgen.schemas.aiextract.AiExtract;
import com.box.sdkgen.schemas.aiextractresponse.AiExtractResponse;
import com.box.sdkgen.schemas.aiextractstructured.AiExtractStructured;
import com.box.sdkgen.schemas.aiextractstructured.AiExtractStructuredFieldsField;
import com.box.sdkgen.schemas.aiextractstructured.AiExtractStructuredFieldsOptionsField;
import com.box.sdkgen.schemas.aiextractstructured.AiExtractStructuredMetadataTemplateField;
import com.box.sdkgen.schemas.aiextractstructuredresponse.AiExtractStructuredResponse;
import com.box.sdkgen.schemas.aiitembase.AiItemBase;
import com.box.sdkgen.schemas.aiitembase.AiItemBaseTypeField;
import com.box.sdkgen.schemas.airesponse.AiResponse;
Expand Down Expand Up @@ -191,7 +191,7 @@ public void testAiExtractStructuredWithFields() {
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar.")));
FileFull file = uploadedFiles.getEntries().get(0);
delayInSeconds(5);
AiExtractResponse response =
AiExtractStructuredResponse response =
client
.getAi()
.createAiExtractStructured(
Expand Down Expand Up @@ -239,12 +239,14 @@ public void testAiExtractStructuredWithFields() {
new AiExtractStructuredFieldsOptionsField("books")))
.build()))
.build());
assert convertToString(getValueFromObjectRawData(response, "firstName")).equals("John");
assert convertToString(getValueFromObjectRawData(response, "lastName")).equals("Doe");
assert convertToString(getValueFromObjectRawData(response, "dateOfBirth")).equals("1990-07-04");
assert convertToString(getValueFromObjectRawData(response, "age")).equals("34");
assert convertToString(getValueFromObjectRawData(response, "hobby"))
assert convertToString(getValueFromObjectRawData(response, "answer.hobby"))
.equals(convertToString(Arrays.asList("guitar")));
assert convertToString(getValueFromObjectRawData(response, "answer.firstName")).equals("John");
assert convertToString(getValueFromObjectRawData(response, "answer.lastName")).equals("Doe");
assert convertToString(getValueFromObjectRawData(response, "answer.dateOfBirth"))
.equals("1990-07-04");
assert convertToString(getValueFromObjectRawData(response, "answer.age")).equals("34");
assert response.getCompletionReason().equals("done");
client.getFiles().deleteFileById(file.getId());
}

Expand Down Expand Up @@ -314,7 +316,7 @@ public void testAiExtractStructuredWithMetadataTemplate() {
"books")))
.build()))
.build());
AiExtractResponse response =
AiExtractStructuredResponse response =
client
.getAi()
.createAiExtractStructured(
Expand All @@ -327,13 +329,14 @@ public void testAiExtractStructuredWithMetadataTemplate() {
.scope("enterprise")
.build())
.build());
assert convertToString(getValueFromObjectRawData(response, "firstName")).equals("John");
assert convertToString(getValueFromObjectRawData(response, "lastName")).equals("Doe");
assert convertToString(getValueFromObjectRawData(response, "dateOfBirth"))
assert convertToString(getValueFromObjectRawData(response, "answer.firstName")).equals("John");
assert convertToString(getValueFromObjectRawData(response, "answer.lastName")).equals("Doe");
assert convertToString(getValueFromObjectRawData(response, "answer.dateOfBirth"))
.equals("1990-07-04T00:00:00Z");
assert convertToString(getValueFromObjectRawData(response, "age")).equals("34");
assert convertToString(getValueFromObjectRawData(response, "hobby"))
assert convertToString(getValueFromObjectRawData(response, "answer.age")).equals("34");
assert convertToString(getValueFromObjectRawData(response, "answer.hobby"))
.equals(convertToString(Arrays.asList("guitar")));
assert response.getCompletionReason().equals("done");
client
.getMetadataTemplates()
.deleteMetadataTemplate(DeleteMetadataTemplateScope.ENTERPRISE, template.getTemplateKey());
Expand Down

0 comments on commit d42892a

Please sign in to comment.