Skip to content

Commit 67d21f8

Browse files
author
Max Hniebergall
committed
finish merge
1 parent bd715c1 commit 67d21f8

File tree

2 files changed

+46
-103
lines changed

2 files changed

+46
-103
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/unified/UnifiedChatCompletionRequestEntity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7171
}
7272
builder.endArray();
7373
}
74+
case null -> {
75+
// do nothing because content is optional
76+
}
7477
}
7578

7679
builder.field(ROLE_FIELD, message.role());

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/unified/UnifiedChatCompletionRequestEntityTests.java

Lines changed: 43 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -699,122 +699,62 @@ public void testSerializationWithBooleanFields() throws IOException {
699699
assertJsonEquals(expectedJsonFalse, jsonStringFalse);
700700
}
701701

702-
// 9. Serialization with Missing Required Fields
703-
// Test with missing required fields to ensure appropriate exceptions are thrown.
704-
public void testSerializationWithMissingRequiredFields() {
705-
// Create a message with missing content (required field)
702+
// 9. a test without the content field to show that the content field is optional
703+
public void testSerializationWithoutContentField() throws IOException {
706704
UnifiedCompletionRequest.Message message = new UnifiedCompletionRequest.Message(
707-
null, // missing content
708-
ROLE,
709-
null,
710705
null,
711-
null
712-
);
713-
var messageList = new ArrayList<UnifiedCompletionRequest.Message>();
714-
messageList.add(message);
715-
// Create the unified request
716-
UnifiedCompletionRequest unifiedRequest = new UnifiedCompletionRequest(
717-
messageList,
718-
null, // model
719-
null, // maxCompletionTokens
720-
null, // stop
721-
null, // temperature
722-
null, // toolChoice
723-
null, // tools
724-
null // topP
725-
);
726-
727-
// Create the unified chat input
728-
UnifiedChatInput unifiedChatInput = new UnifiedChatInput(unifiedRequest, true);
729-
730-
OpenAiChatCompletionModel model = createChatCompletionModel("test-endpoint", "organizationId", "api-key", "model-name", null);
731-
732-
// Create the entity
733-
OpenAiUnifiedChatCompletionRequestEntity entity = new OpenAiUnifiedChatCompletionRequestEntity(unifiedChatInput, model);
734-
735-
// Attempt to serialize to XContent and expect an exception
736-
try {
737-
XContentBuilder builder = JsonXContent.contentBuilder();
738-
entity.toXContent(builder, ToXContent.EMPTY_PARAMS);
739-
fail("Expected an exception due to missing required fields");
740-
} catch (NullPointerException | IOException e) {
741-
// Expected exception
742-
}
743-
}
744-
745-
// 10. Serialization with Mixed Valid and Invalid Data
746-
// Test with a mix of valid and invalid data to ensure the serializer handles it gracefully.
747-
public void testSerializationWithMixedValidAndInvalidData() throws IOException {
748-
// Create a valid message
749-
UnifiedCompletionRequest.Message validMessage = new UnifiedCompletionRequest.Message(
750-
new UnifiedCompletionRequest.ContentString("Valid content"),
751-
ROLE,
752-
"validName",
753-
"validToolCallId",
754-
Collections.singletonList(
755-
new UnifiedCompletionRequest.ToolCall(
756-
"validId",
757-
new UnifiedCompletionRequest.ToolCall.FunctionField("validArguments", "validFunctionName"),
758-
"validType"
759-
)
760-
)
761-
);
762-
763-
// Create an invalid message with null content
764-
UnifiedCompletionRequest.Message invalidMessage = new UnifiedCompletionRequest.Message(
765-
null, // invalid content
766-
ROLE,
767-
"invalidName",
768-
"invalidToolCallId",
706+
"assistant",
707+
"name\nwith\nnewlines",
708+
"tool_call_id\twith\ttabs",
769709
Collections.singletonList(
770710
new UnifiedCompletionRequest.ToolCall(
771-
"invalidId",
772-
new UnifiedCompletionRequest.ToolCall.FunctionField("invalidArguments", "invalidFunctionName"),
773-
"invalidType"
711+
"id\\with\\backslashes",
712+
new UnifiedCompletionRequest.ToolCall.FunctionField("arguments\"with\"quotes", "function_name/with/slashes"),
713+
"type"
774714
)
775715
)
776716
);
777717
var messageList = new ArrayList<UnifiedCompletionRequest.Message>();
778-
messageList.add(validMessage);
779-
messageList.add(invalidMessage);
780-
// Create the unified request with both valid and invalid messages
781-
UnifiedCompletionRequest unifiedRequest = new UnifiedCompletionRequest(
782-
messageList,
783-
"model-name",
784-
100L, // maxCompletionTokens
785-
Collections.singletonList("stop"),
786-
0.9f, // temperature
787-
new UnifiedCompletionRequest.ToolChoiceString("tool_choice"),
788-
Collections.singletonList(
789-
new UnifiedCompletionRequest.Tool(
790-
"type",
791-
new UnifiedCompletionRequest.Tool.FunctionField(
792-
"Fetches the weather in the given location",
793-
"get_weather",
794-
createParameters(),
795-
true
796-
)
797-
)
798-
),
799-
0.8f // topP
800-
);
718+
messageList.add(message);
719+
UnifiedCompletionRequest unifiedRequest = new UnifiedCompletionRequest(messageList, null, null, null, null, null, null, null);
801720

802-
// Create the unified chat input
803721
UnifiedChatInput unifiedChatInput = new UnifiedChatInput(unifiedRequest, true);
722+
OpenAiChatCompletionModel model = createChatCompletionModel("test-url", "organizationId", "api-key", "test-endpoint", null);
804723

805-
OpenAiChatCompletionModel model = createChatCompletionModel("test-endpoint", "organizationId", "api-key", "model-name", null);
806-
807-
// Create the entity
808724
OpenAiUnifiedChatCompletionRequestEntity entity = new OpenAiUnifiedChatCompletionRequestEntity(unifiedChatInput, model);
809725

810-
// Serialize to XContent and verify
811-
try {
812-
XContentBuilder builder = JsonXContent.contentBuilder();
813-
entity.toXContent(builder, ToXContent.EMPTY_PARAMS);
814-
fail("Expected an exception due to invalid data");
815-
} catch (NullPointerException | IOException e) {
816-
// Expected exception
817-
}
726+
XContentBuilder builder = JsonXContent.contentBuilder();
727+
entity.toXContent(builder, ToXContent.EMPTY_PARAMS);
728+
729+
String jsonString = Strings.toString(builder);
730+
String expectedJson = """
731+
{
732+
"messages": [
733+
{
734+
"role": "assistant",
735+
"name": "name\\nwith\\nnewlines",
736+
"tool_call_id": "tool_call_id\\twith\\ttabs",
737+
"tool_calls": [
738+
{
739+
"id": "id\\\\with\\\\backslashes",
740+
"function": {
741+
"arguments": "arguments\\"with\\"quotes",
742+
"name": "function_name/with/slashes"
743+
},
744+
"type": "type"
745+
}
746+
]
747+
}
748+
],
749+
"model": "test-endpoint",
750+
"n": 1,
751+
"stream": true,
752+
"stream_options": {
753+
"include_usage": true
754+
}
755+
}
756+
""";
757+
assertJsonEquals(jsonString, expectedJson);
818758
}
819759

820760
public static Map<String, Object> createParameters() {

0 commit comments

Comments
 (0)