Skip to content

Commit 6ce7ac6

Browse files
author
Max Hniebergall
committed
Improvements from review
1 parent 902352a commit 6ce7ac6

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

server/src/main/java/org/elasticsearch/inference/UnifiedCompletionRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private static Content parseContent(XContentParser parser) throws IOException {
148148

149149
public Message(StreamInput in) throws IOException {
150150
this(
151-
in.readNamedWriteable(Content.class),
151+
in.readOptionalNamedWriteable(Content.class),
152152
in.readString(),
153153
in.readOptionalString(),
154154
in.readOptionalString(),
@@ -158,7 +158,7 @@ public Message(StreamInput in) throws IOException {
158158

159159
@Override
160160
public void writeTo(StreamOutput out) throws IOException {
161-
out.writeNamedWriteable(content);
161+
out.writeOptionalNamedWriteable(content);
162162
out.writeString(role);
163163
out.writeOptionalString(name);
164164
out.writeOptionalString(toolCallId);

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/request/openai/OpenAiUnifiedChatCompletionRequestEntityTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,64 @@ public void testSerializationWithBooleanFields() throws IOException {
702702
assertJsonEquals(expectedJsonFalse, jsonStringFalse);
703703
}
704704

705+
// 9. a test without the content field to show that the content field is optional
706+
public void testSerializationWithoutContentField() throws IOException {
707+
UnifiedCompletionRequest.Message message = new UnifiedCompletionRequest.Message(
708+
null,
709+
"assistant",
710+
"name\nwith\nnewlines",
711+
"tool_call_id\twith\ttabs",
712+
Collections.singletonList(
713+
new UnifiedCompletionRequest.ToolCall(
714+
"id\\with\\backslashes",
715+
new UnifiedCompletionRequest.ToolCall.FunctionField("arguments\"with\"quotes", "function_name/with/slashes"),
716+
"type"
717+
)
718+
)
719+
);
720+
var messageList = new ArrayList<UnifiedCompletionRequest.Message>();
721+
messageList.add(message);
722+
UnifiedCompletionRequest unifiedRequest = new UnifiedCompletionRequest(messageList, null, null, null, null, null, null, null);
723+
724+
UnifiedChatInput unifiedChatInput = new UnifiedChatInput(unifiedRequest, true);
725+
OpenAiChatCompletionModel model = createChatCompletionModel("test-url", "organizationId", "api-key", "test-endpoint", null);
726+
727+
OpenAiUnifiedChatCompletionRequestEntity entity = new OpenAiUnifiedChatCompletionRequestEntity(unifiedChatInput, model);
728+
729+
XContentBuilder builder = JsonXContent.contentBuilder();
730+
entity.toXContent(builder, ToXContent.EMPTY_PARAMS);
731+
732+
String jsonString = Strings.toString(builder);
733+
String expectedJson = """
734+
{
735+
"messages": [
736+
{
737+
"role": "assistant",
738+
"name": "name\\nwith\\nnewlines",
739+
"tool_call_id": "tool_call_id\\twith\\ttabs",
740+
"tool_calls": [
741+
{
742+
"id": "id\\\\with\\\\backslashes",
743+
"function": {
744+
"arguments": "arguments\\"with\\"quotes",
745+
"name": "function_name/with/slashes"
746+
},
747+
"type": "type"
748+
}
749+
]
750+
}
751+
],
752+
"model": "test-endpoint",
753+
"n": 1,
754+
"stream": true,
755+
"stream_options": {
756+
"include_usage": true
757+
}
758+
}
759+
""";
760+
assertJsonEquals(jsonString, expectedJson);
761+
}
762+
705763
public static Map<String, Object> createParameters() {
706764
Map<String, Object> parameters = new LinkedHashMap<>();
707765
parameters.put("type", "object");

0 commit comments

Comments
 (0)