|
| 1 | +package org.llm4j.api; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +public class ChatHistory { |
| 10 | + |
| 11 | + String context; |
| 12 | + List<Map.Entry<Message, Message>> exampleList = new ArrayList<>(); |
| 13 | + List<Message> messageList = new ArrayList<>(); |
| 14 | + |
| 15 | + public ChatHistory setContext(String context) { |
| 16 | + this.context = context; |
| 17 | + return this; |
| 18 | + } |
| 19 | + |
| 20 | + public ChatHistory addExample(String text1, String text2) { |
| 21 | + Map.Entry<Message, Message> pair = new ImmutablePair<>(new Message(text1), new Message(text1)); |
| 22 | + exampleList.add(pair); |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + public ChatHistory addMessage(String content) { |
| 27 | + messageList.add(new Message(content)); |
| 28 | + return this; |
| 29 | + } |
| 30 | + |
| 31 | + public ChatHistory addMessage(String author, String content) { |
| 32 | + messageList.add(new Message(author, content)); |
| 33 | + return this; |
| 34 | + } |
| 35 | + |
| 36 | + public String getContext() { |
| 37 | + return context; |
| 38 | + } |
| 39 | + |
| 40 | + public List<Map.Entry<Message, Message>> getExampleList() { |
| 41 | + return exampleList; |
| 42 | + } |
| 43 | + |
| 44 | + public List<Message> getMessageList() { |
| 45 | + return messageList; |
| 46 | + } |
| 47 | + |
| 48 | + public static class Message { |
| 49 | + |
| 50 | + private final String author; |
| 51 | + private final String content; |
| 52 | + |
| 53 | + public Message(String author, String content) { |
| 54 | + this.author = author; |
| 55 | + this.content = content; |
| 56 | + } |
| 57 | + |
| 58 | + public Message(String content) { |
| 59 | + this(null, content); |
| 60 | + } |
| 61 | + |
| 62 | + public String getAuthor() { |
| 63 | + return author; |
| 64 | + } |
| 65 | + |
| 66 | + public String getContent() { |
| 67 | + return content; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String toString() { |
| 72 | + String text = ""; |
| 73 | + if(author!=null && !author.isEmpty()) { |
| 74 | + text += author; |
| 75 | + } |
| 76 | + if(content!=null && !content.isEmpty()) { |
| 77 | + text += text.isEmpty() ? content: ":" + content; |
| 78 | + } |
| 79 | + return text; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments