retrieval-modules

Retrieval

Vector store 같은 데이터 시스템에 쿼리를 날려서 가장 관련된 문서를 추출하는 모듈

Document Search

search engine, vector store, database 등의 data source에서 Documents를 검색한다.

VectorStoreDocumentRetriever

vector store에서 documents를 검색한다.

DocumentRetriever retriever = VectorStoreDocumentRetriever.builder()
    .vectorStore(vectorStore)
    .similarityThreshold(0.73) // 유사도 임계값
    .topK(5) // 상위 k개 결과
    .filterExpression(new FilterExpressionBuilder() // static fileter
        .eq("genre", "fairytale")
        .build())
    .build();
List<Document> documents = retriever.retrieve(new Query("What is the main character of the story?"));

위에선 static filter를 적용되어있다. dynamic filter에는 supplier와 param을 사용할 수 있다.

DocumentRetriever retriever = VectorStoreDocumentRetriever.builder()
    .vectorStore(vectorStore)
    .filterExpression(() -> new FilterExpressionBuilder()
        .eq("tenant", TenantContextHolder.getTenantIdentifier())
        .build())
    .build();
List<Document> documents = retriever.retrieve(new Query("What are the KPIs for the next semester?"));

Query query = Query.builder()
    .text("Who is Anacletus?")
    .context(Map.of(VectorStoreDocumentRetriever.FILTER_EXPRESSION, "location == 'Whispering Woods'"))
    .build();
List<Document> retrievedDocuments = documentRetriever.retrieve(query);

Document Join

여러개의 Query와 여러개의 data source에서 검색한 Document들을 하나의 Document list로 만들어준다. 중복된 문서를 처리하거나, 우선순위를 조정하는 작업도 수행한다.

ConcatenationDocumentJoiner

여러개의 쿼리에 대해서 여러 DataSource에 검색한 결과를 하나의 Document List로 합친다. 중복된 문서의 경우 첫 문서만 유지하고 각 문저의 Score는 그대로 유지한다.

Map<Query, List<List<Document>>> documentsForQuery = ...
DocumentJoiner documentJoiner = new ConcatenationDocumentJoiner();
List<Document> documents = documentJoiner.join(documentsForQuery);