Skip to content

Refactor: Add null check, optimize string joining, and add JavaDocs #3663

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -21,6 +21,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -144,11 +145,20 @@ public OpenAiApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> he
.build(); // @formatter:on
}

/**
* Returns a string containing all text values from the given media content list. Only
* elements of type "text" are processed and concatenated in order.
* @param content The list of {@link ChatCompletionMessage.MediaContent}
* @return a string containing all text values from "text" type elements
* @throws IllegalArgumentException if content is null
*/
public static String getTextContent(List<ChatCompletionMessage.MediaContent> content) {
Assert.notNull(content, "content cannot be null");

return content.stream()
.filter(c -> "text".equals(c.type()))
.map(ChatCompletionMessage.MediaContent::text)
.reduce("", (a, b) -> a + b);
.collect(Collectors.joining());
}

/**
Expand Down