Skip to content

Commit

Permalink
refactor(adaptive-rag): make opening Chroma Store lazy
Browse files Browse the repository at this point in the history
resolve #5
  • Loading branch information
bsorrentino committed Jun 21, 2024
1 parent a0fd5a9 commit 6892438
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.bsc.langgraph4j.CompiledGraph;
Expand Down Expand Up @@ -57,17 +58,22 @@ public List<String> documents() {

private final String openApiKey;
private final String tavilyApiKey;
private final ChromaStore chroma;
@Getter(lazy = true)
private final ChromaStore chroma = openChroma();

public AdaptiveRag( String openApiKey, String tavilyApiKey ) {
Objects.requireNonNull(openApiKey, "no OPENAI APIKEY provided!");
Objects.requireNonNull(tavilyApiKey, "no TAVILY APIKEY provided!");
this.openApiKey = openApiKey;
this.tavilyApiKey = tavilyApiKey;
this.chroma = ChromaStore.of(openApiKey);
//this.chroma = ChromaStore.of(openApiKey);

}

private ChromaStore openChroma() {
return ChromaStore.of(openApiKey);
}

/**
* Node: Retrieve documents
* @param state The current graph state
Expand All @@ -78,7 +84,7 @@ private Map<String,Object> retrieve( State state ) {

String question = state.question();

EmbeddingSearchResult<TextSegment> relevant = this.chroma.search( question );
EmbeddingSearchResult<TextSegment> relevant = this.getChroma().search( question );

List<String> documents = relevant.matches().stream()
.map( m -> m.embedded().text() )
Expand Down

0 comments on commit 6892438

Please sign in to comment.