Skip to content

Commit 1da38ab

Browse files
authored
Update to the new LangChain4j API (#18)
1 parent add5339 commit 1da38ab

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/backend/src/main/java/ai/azure/openai/rag/workshop/backend/rest/ChatResource.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import dev.langchain4j.model.embedding.EmbeddingModel;
1111
import dev.langchain4j.model.output.Response;
1212
import dev.langchain4j.store.embedding.EmbeddingMatch;
13+
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
14+
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
1315
import dev.langchain4j.store.embedding.EmbeddingStore;
1416
import jakarta.inject.Inject;
1517
import jakarta.ws.rs.*;
@@ -62,15 +64,17 @@ public ChatResponse chat(ChatRequest chatRequest) {
6264

6365
// Find relevant embeddings from Qdrant based on the user's question
6466
log.info("### Find relevant embeddings from Qdrant based on the question");
65-
List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(embeddedQuestion, 3);
67+
EmbeddingSearchResult<TextSegment> relevant = embeddingStore.search(EmbeddingSearchRequest.builder()
68+
.queryEmbedding(embeddedQuestion)
69+
.build());
6670

6771
// Builds chat history using the relevant embeddings
6872
log.info("### Builds chat history using the relevant embeddings");
6973
List<ChatMessage> chatMessages = new ArrayList<>();
7074
chatMessages.add(SystemMessage.from(SYSTEM_MESSAGE_PROMPT));
7175
String userMessage = question + "\n\nSources:\n";
72-
for (EmbeddingMatch<TextSegment> textSegmentEmbeddingMatch : relevant) {
73-
userMessage += textSegmentEmbeddingMatch.embedded().metadata("filename") + ": " + textSegmentEmbeddingMatch.embedded().text() + "\n";
76+
for (EmbeddingMatch<TextSegment> textSegmentEmbeddingMatch : relevant.matches()) {
77+
userMessage += textSegmentEmbeddingMatch.embedded().metadata().getString("filename") + ": " + textSegmentEmbeddingMatch.embedded().text() + "\n";
7478
}
7579
chatMessages.add(UserMessage.from(userMessage));
7680

0 commit comments

Comments
 (0)