Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit 519080c

Browse files
author
deep-learning-dynamo
committed
UserMessage: returned back support for "content" property of type String for backwards compatibility with OpenAI-like APIs
1 parent 4137abf commit 519080c

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

src/main/java/dev/ai4j/openai4j/chat/UserMessage.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
public final class UserMessage implements Message {
1313

1414
private final Role role = USER;
15-
private final List<Content> content;
15+
private final Object content;
1616
private final String name;
1717

1818
private UserMessage(Builder builder) {
19-
this.content = builder.content;
19+
this.content = builder.stringContent != null ? builder.stringContent : builder.content;
2020
this.name = builder.name;
2121
}
2222

2323
public Role role() {
2424
return role;
2525
}
2626

27-
public List<Content> content() {
27+
public Object content() {
2828
return content;
2929
}
3030

@@ -63,6 +63,12 @@ public String toString() {
6363
+ "}";
6464
}
6565

66+
public static UserMessage from(String text) {
67+
return UserMessage.builder()
68+
.content(text)
69+
.build();
70+
}
71+
6672
public static UserMessage from(String text, String... imageUrls) {
6773
return UserMessage.builder()
6874
.addText(text)
@@ -76,6 +82,7 @@ public static Builder builder() {
7682

7783
public static final class Builder {
7884

85+
private String stringContent; // keeping it for compatibility with other OpenAI-like APIs
7986
private List<Content> content;
8087
private String name;
8188

@@ -127,6 +134,11 @@ public Builder content(List<Content> content) {
127134
return this;
128135
}
129136

137+
public Builder content(String content) {
138+
this.stringContent = content;
139+
return this;
140+
}
141+
130142
public Builder name(String name) {
131143
this.name = name;
132144
return this;

src/test/java/dev/ai4j/openai4j/Test.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/test/java/dev/ai4j/openai4j/chat/ChatCompletionTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,27 @@ void testGpt4Vision() {
429429
// then
430430
assertThat(response.content()).containsIgnoringCase("green");
431431
}
432+
433+
@ParameterizedTest
434+
@EnumSource(value = ChatCompletionModel.class, mode = EXCLUDE, names = {
435+
"GPT_4_32K", "GPT_4_32K_0314", "GPT_4_32K_0613" // I don't have access to these models
436+
})
437+
void testUserMessageWithStringContent(ChatCompletionModel model) {
438+
439+
// given
440+
UserMessage userMessage = UserMessage.builder()
441+
.content("What is the capital of Germany?")
442+
.build();
443+
444+
ChatCompletionRequest request = ChatCompletionRequest.builder()
445+
.model(model)
446+
.messages(userMessage)
447+
.build();
448+
449+
// when
450+
ChatCompletionResponse response = client.chatCompletion(request).execute();
451+
452+
// then
453+
assertThat(response.content()).contains("Berlin");
454+
}
432455
}

0 commit comments

Comments
 (0)