Class RAGServiceImpl
- java.lang.Object
-
- com.composum.ai.backend.slingbase.impl.RAGServiceImpl
-
- All Implemented Interfaces:
RAGService
public class RAGServiceImpl extends Object implements RAGService
Basic services for retrieval augmented generation (RAG).
-
-
Field Summary
Fields Modifier and Type Field Description protected AIConfigurationService
aiConfigurationService
protected com.composum.ai.backend.base.service.chat.GPTChatCompletionService
chatCompletionService
protected com.composum.ai.backend.base.service.chat.GPTEmbeddingService
embeddingService
protected ApproximateMarkdownService
markdownService
protected PageCachedValueService
pageCachedValueService
protected AtomicLong
requestCounter
-
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
-
markdownService
protected ApproximateMarkdownService markdownService
-
embeddingService
protected com.composum.ai.backend.base.service.chat.GPTEmbeddingService embeddingService
-
aiConfigurationService
protected AIConfigurationService aiConfigurationService
-
chatCompletionService
protected com.composum.ai.backend.base.service.chat.GPTChatCompletionService chatCompletionService
-
pageCachedValueService
protected PageCachedValueService pageCachedValueService
-
requestCounter
protected final AtomicLong requestCounter
-
-
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 interfaceRAGService
- Parameters:
root
- the root resource to search inquerytext
- the query textlimit
- 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 aftersearchRelated(Resource, String, int)
.- Specified by:
orderByEmbedding
in interfaceRAGService
- Parameters:
querytext
- the query textresources
- the list of resources to search inrequest
- the request to use when determining the markdown approximation - not modifiedresponse
- the response to use when determining the markdown approximation - not modifiedrootResource
- 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 withsearchRelated(Resource, String, int)
.- Specified by:
ragAnswer
in interfaceRAGService
- Parameters:
querytext
- the query textresources
- the list of resources to answer fromrequest
- the request to use when determining the markdown approximation - not modifiedresponse
- the response to use when determining the markdown approximation - not modifiedrootResource
- the root resource to find GPT configuration fromlimitRagTexts
- 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 interfaceRAGService
- Parameters:
querytext
- the query text for which we find keywordsrootResource
- the root resource to find GPT configuration from- Returns:
- a list of keywords
- Throws:
javax.jcr.RepositoryException
-
-