Class RAGServiceImpl

  • All Implemented Interfaces:
    RAGService

    public class RAGServiceImpl
    extends Object
    implements RAGService
    Basic services for retrieval augmented generation (RAG).
    • Constructor Summary

      Constructors 
      Constructor Description
      RAGServiceImpl()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      List<String> collectSearchKeywords​(String querytext, org.apache.sling.api.resource.Resource rootResource)
      Processes a query to have the AI suggest a couple of search keywords for use with the other methods that might find the most relevant results.
      protected @NotNull List<String> containsQuery​(@NotNull org.apache.sling.api.resource.Resource root, @NotNull String querytext, int restOfLimit)  
      protected com.composum.ai.backend.base.service.chat.GPTEmbeddingService.EmbeddingsCache getEmbeddingsCache​(Map<String,​org.apache.sling.api.resource.Resource> textToResource)  
      protected String normalize​(String querytext)
      Turn it into a query for the words mentioned in there - that is, remove all meta characters for CONTAINS queries: AND, OR, words prefixed with -, quotes, backslashes.
      List<org.apache.sling.api.resource.Resource> orderByEmbedding​(String querytext, List<org.apache.sling.api.resource.Resource> resources, @NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull org.apache.sling.api.SlingHttpServletResponse response, @NotNull org.apache.sling.api.resource.Resource rootResource)
      Finds the resources whose markdown approximation has embeddings that are the most similar to the querytext embedding.
      String ragAnswer​(String querytext, List<org.apache.sling.api.resource.Resource> resources, org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, @NotNull org.apache.sling.api.resource.Resource rootResource, int limitRagTexts)
      Answer a question with RAG from the given resources, e.g.
      List<String> searchRelated​(org.apache.sling.api.resource.Resource root, String querytext, int limit)
      Returns a list of up to limit paths that might be related to the query.
    • Field Detail

      • embeddingService

        protected com.composum.ai.backend.base.service.chat.GPTEmbeddingService embeddingService
      • chatCompletionService

        protected com.composum.ai.backend.base.service.chat.GPTChatCompletionService chatCompletionService
      • requestCounter

        protected final AtomicLong requestCounter
    • Constructor Detail

      • RAGServiceImpl

        public RAGServiceImpl()
    • Method Detail

      • searchRelated

        @Nonnull
        public List<String> searchRelated​(@Nullable
                                          org.apache.sling.api.resource.Resource root,
                                          @Nullable
                                          String querytext,
                                          int limit)
        Description copied from interface: RAGService
        Returns a list of up to limit paths that might be related to the query. We search for the whole query, andy quoted strings inside and then for all single words in the query, scored. The paths are returned in descending order of lucene score.
        Specified by:
        searchRelated in interface RAGService
        Parameters:
        root - the root resource to search in
        querytext - the query text
        limit - the maximum number of paths to return
        Returns:
        a list of paths to jcr:content nodes, empty if no results or any of the parameters don't fit.
      • containsQuery

        @NotNull
        protected @NotNull List<String> containsQuery​(@NotNull
                                                      @NotNull org.apache.sling.api.resource.Resource root,
                                                      @NotNull
                                                      @NotNull String querytext,
                                                      int restOfLimit)
                                               throws javax.jcr.RepositoryException
        Throws:
        javax.jcr.RepositoryException
      • normalize

        @Nonnull
        protected String normalize​(@Nonnull
                                   String querytext)
        Turn it into a query for the words mentioned in there - that is, remove all meta characters for CONTAINS queries: AND, OR, words prefixed with -, quotes, backslashes. We use an OR query to find pages with as many words as possible.
      • orderByEmbedding

        @Nonnull
        public List<org.apache.sling.api.resource.Resource> orderByEmbedding​(@Nullable
                                                                             String querytext,
                                                                             @Nonnull
                                                                             List<org.apache.sling.api.resource.Resource> resources,
                                                                             @NotNull
                                                                             @NotNull org.apache.sling.api.SlingHttpServletRequest request,
                                                                             @NotNull
                                                                             @NotNull org.apache.sling.api.SlingHttpServletResponse response,
                                                                             @NotNull
                                                                             @NotNull org.apache.sling.api.resource.Resource rootResource)
        Finds the resources whose markdown approximation has embeddings that are the most similar to the querytext embedding. Useable e.g. as filter after searchRelated(Resource, String, int).
        Specified by:
        orderByEmbedding in interface RAGService
        Parameters:
        querytext - the query text
        resources - the list of resources to search in
        request - the request to use when determining the markdown approximation - not modified
        response - the response to use when determining the markdown approximation - not modified
        rootResource - the root resource to search in
      • ragAnswer

        public String ragAnswer​(@Nullable
                                String querytext,
                                @Nonnull
                                List<org.apache.sling.api.resource.Resource> resources,
                                @Nonnull
                                org.apache.sling.api.SlingHttpServletRequest request,
                                @Nonnull
                                org.apache.sling.api.SlingHttpServletResponse response,
                                @NotNull
                                @NotNull org.apache.sling.api.resource.Resource rootResource,
                                int limitRagTexts)
        Answer a question with RAG from the given resources, e.g. found with searchRelated(Resource, String, int).
        Specified by:
        ragAnswer in interface RAGService
        Parameters:
        querytext - the query text
        resources - the list of resources to answer from
        request - the request to use when determining the markdown approximation - not modified
        response - the response to use when determining the markdown approximation - not modified
        rootResource - the root resource to find GPT configuration from
        limitRagTexts - the maximum number of RAG texts to consider
        Returns:
        the answer text
      • getEmbeddingsCache

        protected com.composum.ai.backend.base.service.chat.GPTEmbeddingService.EmbeddingsCache getEmbeddingsCache​(Map<String,​org.apache.sling.api.resource.Resource> textToResource)
      • collectSearchKeywords

        @Nonnull
        public List<String> collectSearchKeywords​(@Nullable
                                                  String querytext,
                                                  @Nonnull
                                                  org.apache.sling.api.resource.Resource rootResource)
                                           throws javax.jcr.RepositoryException
        Processes a query to have the AI suggest a couple of search keywords for use with the other methods that might find the most relevant results.
        Specified by:
        collectSearchKeywords in interface RAGService
        Parameters:
        querytext - the query text for which we find keywords
        rootResource - the root resource to find GPT configuration from
        Returns:
        a list of keywords
        Throws:
        javax.jcr.RepositoryException