This repository was archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathVisionUtil.java
More file actions
49 lines (44 loc) · 1.71 KB
/
VisionUtil.java
File metadata and controls
49 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.theokanning.openai.utils;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageContent;
import com.theokanning.openai.completion.chat.ImageUrl;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Vision tool class
*
* @author cong
* @since 2023/11/17
*/
public class VisionUtil {
private static final Pattern pattern = Pattern.compile("(https?://\\S+)");
public static ChatMessage<List<ChatMessageContent>> convertForVision(ChatMessage<String> msg) {
List<ChatMessageContent> content = new ArrayList<>();
String sourceText = msg.getContent();
// Regular expression to match image URLs
Matcher matcher = pattern.matcher(sourceText);
// Find image URLs and split the string
int lastIndex = 0;
while (matcher.find()) {
String url = matcher.group();
// Add the text before the image URL
if (matcher.start() > lastIndex) {
String text = sourceText.substring(lastIndex, matcher.start()).trim();
content.add(new ChatMessageContent(text));
}
// Add the image URL
ImageUrl imageUrl = new ImageUrl();
imageUrl.setUrl(url);
content.add(new ChatMessageContent(imageUrl));
lastIndex = matcher.end();
}
// Add the remaining text
if (lastIndex < sourceText.length()) {
String text = sourceText.substring(lastIndex).trim();
content.add(new ChatMessageContent(text));
}
return new ChatMessage<>(msg.getRole(), content, msg.getName());
}
}