Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [vertexai] support number types in PartMaker.fromFunctionResponse #10892

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ public static Part fromFunctionResponse(String name, Map<String, Object> respons
if (value instanceof String) {
String stringValue = (String) value;
structBuilder.putFields(key, Value.newBuilder().setStringValue(stringValue).build());
} else if (value instanceof Double) {
Double doubleValue = (Double) value;
} else if (value instanceof Number) {
// Convert a number to a double value since the proto only supports double.
double doubleValue = ((Number) value).doubleValue();
structBuilder.putFields(key, Value.newBuilder().setNumberValue(doubleValue).build());
} else if (value instanceof Boolean) {
Boolean boolValue = (Boolean) value;
Expand All @@ -126,7 +127,7 @@ public static Part fromFunctionResponse(String name, Map<String, Object> respons
} else {
throw new IllegalArgumentException(
"The value in the map can only be one of the following format: "
+ "String, Double, Boolean, null.");
+ "String, Number, Boolean, null.");
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void fromMimeTypeAndData_dataInURI() throws URISyntaxException {
}

@Test
public void testFromFunctionResponseWithStruct() {
public void testFromFunctionResponseWithStruct_containsRightNameAndValues() {
String functionName = "getCurrentWeather";
Struct functionResponse =
Struct.newBuilder()
Expand All @@ -95,7 +95,7 @@ public void testFromFunctionResponseWithStruct() {
}

@Test
public void testFromFunctionResponseWithMap() {
public void testFromFunctionResponseWithMap_containsRightNameAndValues() {
String functionName = "getCurrentWeather";
Map<String, Object> functionResponse = new HashMap<>();
functionResponse.put("currentWeather", "Super nice!");
Expand All @@ -115,7 +115,28 @@ public void testFromFunctionResponseWithMap() {
}

@Test
public void testFromFunctionResponseWithInvalidMap() {
public void testFromFunctionResponseWithNumberValues_containsRightNameAndValues() {
String functionName = "getCurrentWeather";
Map<String, Object> functionResponse = new HashMap<>();
functionResponse.put("integerNumber", 85);
functionResponse.put("doubleNumber", 85.0);
functionResponse.put("floatNumber", 85.0f);
functionResponse.put("longNumber", 85L);
functionResponse.put("shortNumber", (short) 85);

Part part = PartMaker.fromFunctionResponse(functionName, functionResponse);
Map<String, Value> fieldsMap = part.getFunctionResponse().getResponse().getFieldsMap();

assertThat(part.getFunctionResponse().getName()).isEqualTo("getCurrentWeather");
assertThat(fieldsMap.get("integerNumber").getNumberValue()).isEqualTo(85.0);
assertThat(fieldsMap.get("doubleNumber").getNumberValue()).isEqualTo(85.0);
assertThat(fieldsMap.get("floatNumber").getNumberValue()).isEqualTo(85.0);
assertThat(fieldsMap.get("longNumber").getNumberValue()).isEqualTo(85.0);
assertThat(fieldsMap.get("shortNumber").getNumberValue()).isEqualTo(85.0);
}

@Test
public void testFromFunctionResponseWithInvalidMap_throwsIllegalArgumentException() {
String functionName = "getCurrentWeather";
Map<String, Object> invalidResponse = new HashMap<>();
invalidResponse.put("currentWeather", new byte[] {1, 2, 3});
Expand All @@ -127,6 +148,6 @@ public void testFromFunctionResponseWithInvalidMap() {
.hasMessageThat()
.isEqualTo(
"The value in the map can only be one of the following format: "
+ "String, Double, Boolean, null.");
+ "String, Number, Boolean, null.");
}
}
Loading