-
Notifications
You must be signed in to change notification settings - Fork 0
/
RagChat.java
145 lines (131 loc) · 5.87 KB
/
RagChat.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS dev.langchain4j:langchain4j:0.29.1
//DEPS dev.langchain4j:langchain4j-ollama:0.29.1
//DEPS org.jsoup:jsoup:1.16.1
//DEPS io.github.furstenheim:copy_down:1.1
import java.util.*;
import java.util.function.*;
import dev.langchain4j.data.message.*;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.TokenStream;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.ollama.OllamaEmbeddingModel;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.data.document.*;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.store.embedding.*;
import dev.langchain4j.rag.content.Content;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.rag.query.Query;
public class RagChat {
ChatMemory chatMemory;
ChatbotAgent chatbotAgent;
private void setup() {
var html2markdownConverter = new io.github.furstenheim.CopyDown();
DocumentParser soupDocumentParser = inputStream -> {
try {
var soup = org.jsoup.Jsoup.parse(inputStream, "UTF-8", "");
var markdown = html2markdownConverter.convert(soup.outerHtml());
return Document.from(markdown);
} catch (Exception e) {
System.err.println(e);
throw new RuntimeException(e);
}
};
var documentsPath = "/Users/hal/git/fschat/files/html-cache/www__nord__no/__student__eksamen";
List<Document> documents = FileSystemDocumentLoader.loadDocumentsRecursively(documentsPath, soupDocumentParser);
EmbeddingModel embeddingModel = OllamaEmbeddingModel.builder()
.baseUrl("http://localhost:11434/")
.modelName("nomic-embed-text")
.build();
InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>()
// {
// public List<String> addAll(List<Embedding> embeddings, List<TextSegment> textSegments) {
// var ids = super.addAll(embeddings, textSegments);
// System.out.println("Added " + embeddings.size() + " embeddings: ");
// for (int i = 0; i < embeddings.size(); i++) {
// var embedding = embeddings.get(i);
// var textSegment = textSegments.get(i);
// System.out.println(" " + i + ": " + textSegment.metadata());
// }
// return ids;
// }
// public EmbeddingSearchResult<TextSegment> search(EmbeddingSearchRequest request) {
// var result = super.search(request);
// for (var match : result.matches()) {
// System.out.println(match.score() + ": " + match.embedded().metadata());
// }
// return result;
// }
// }
;
EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
.documentSplitter(DocumentSplitters.recursive(300, 30))
.embeddingModel(embeddingModel)
.embeddingStore(embeddingStore)
.build();
ingestor.ingest(documents);
ContentRetriever contentRetriever = new EmbeddingStoreContentRetriever(embeddingStore, embeddingModel, 5, 0.5)
// {
// public List<Content> retrieve(Query query) {
// var contents = super.retrieve(query);
// System.out.println("Retrieved " + contents.size() + " text fragments for " + query.text() + ": ");
// for (var content : contents) {
// System.out.println(content);
// }
// return contents;
// }
// }
;
StreamingChatLanguageModel llm = OllamaStreamingChatModel.builder()
.baseUrl("http://localhost:11434/")
.modelName("mistral")
.build();
chatMemory = MessageWindowChatMemory.builder()
.maxMessages(20)
.id("default")
.build();
chatMemory.add(new SystemMessage("You are a student advisor. Answer in Norwegian."));
chatbotAgent = AiServices.builder(ChatbotAgent.class)
.streamingChatLanguageModel(llm)
.chatMemory(chatMemory)
.contentRetriever(contentRetriever)
.build();
}
public interface ChatbotAgent {
TokenStream respond(String userMessage);
}
private void streamingChat() {
try (var scanner = new Scanner(System.in)) {
System.out.print("user message to llm > ");
while (true) {
String line = scanner.nextLine();
if (line == null || line.isEmpty()) {
break;
}
chatbotAgent.respond(line)
.onNext(token -> System.out.print(token))
.onComplete(response -> {
// System.out.println(chatMemory.messages());
System.out.print("\nuser message to llm > ");
})
.onError(error -> System.out.println("\n... oops, something went wrong:\n" + error))
.start();
}
}
}
public static void main(String[] args) {
var chat = new RagChat();
chat.setup();
chat.streamingChat();
System.exit(0);
}
}