JRag

org.llm4s.javaapi.rag.JRag
See theJRag companion object
final class JRag extends AutoCloseable

Retrieval-augmented generation from Java: ingest documents, then ask questions that a model answers from the chunks of them that match, with those chunks returned as the answer's sources. It is llm4s-rag's pipeline - chunking, embedding, a vector and keyword (hybrid) search and the answer prompt - with no Scala type in sight.

try (JRag rag = JRag.builder()
       .embeddings(Llm4s.createDefaultEmbeddingClient().get())
       .llm(Llm4s.createDefaultClient().get())
       .build()
       .get()) {
   rag.ingest(Path.of("docs")).get();
   JRagAnswer answer = rag.ask("How do I configure a provider?").get();
   System.out.println(answer.answer());
   answer.sources().forEach(s -> System.out.println("  " + s.documentId() + " (" + s.score() + ")"));
}

Obtain one from JRag.builder. Every call blocks the calling thread and never throws: a failure - of the embedding provider, the model, a file or an argument - comes back inside the LlmResult, whose getError().getKind() says which, as for JLlmClient and JEmbeddingClient. Interrupting the blocked thread returns a failed result of kind CANCELLED, with the interrupt flag still set.

Different documents may be ingested from several threads at once; ingesting the same document id from two threads at once is not supported. close() releases the store - the SQLite connections, or the memory of an in-memory store

  • and not the clients it was built with, which stay the caller's to close.

Attributes

Companion
object
Graph
Supertypes
trait AutoCloseable
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def ask(question: String): LlmResult[JRagAnswer]

Answers question from the chunks that match it best, at most the builder's topK: the question is embedded, the chunks found, and the model asked to answer from them alone. The answer lists those chunks as its sources; with nothing indexed it has none, and the model is still asked, so its answer says it cannot tell.

Answers question from the chunks that match it best, at most the builder's topK: the question is embedded, the chunks found, and the model asked to answer from them alone. The answer lists those chunks as its sources; with nothing indexed it has none, and the model is still asked, so its answer says it cannot tell.

A pipeline built without a chat client (llm) returns a failed result of kind CONFIGURATION; a null or blank question one of kind VALIDATION. A failure of the embedding provider or the model has the kind of that failure.

Attributes

def ask(question: String, topK: Int): LlmResult[JRagAnswer]

Answers question as ask(question) does, from at most topK chunks, at least 1, instead of the builder's.

Answers question as ask(question) does, from at most topK chunks, at least 1, instead of the builder's.

Attributes

def chunkCount: Int

How many chunks this pipeline has indexed: a document ingested again replaces its chunks.

How many chunks this pipeline has indexed: a document ingested again replaces its chunks.

Attributes

override def close(): Unit

Releases the store. The embedding and chat clients the pipeline was built with are not closed.

Releases the store. The embedding and chat clients the pipeline was built with are not closed.

Attributes

Definition Classes
AutoCloseable
def documentCount: Int

How many documents this pipeline has indexed: a document ingested twice counts once.

How many documents this pipeline has indexed: a document ingested twice counts once.

Attributes

def ingest(path: Path): LlmResult[Integer]

Ingests a file, or every file directly in a directory whose name ends in .txt, .md, .pdf, .docx, .json, .xml or .html, returning how many chunks were indexed. A file's id is its name: ingesting a file of the same name again replaces what was indexed under it. The same as ingest(path, Map.of()).

Ingests a file, or every file directly in a directory whose name ends in .txt, .md, .pdf, .docx, .json, .xml or .html, returning how many chunks were indexed. A file's id is its name: ingesting a file of the same name again replaces what was indexed under it. The same as ingest(path, Map.of()).

Attributes

def ingest(path: Path, metadata: Map[String, String]): LlmResult[Integer]

Ingests a file or a directory as ingest(path) does, attaching metadata to every chunk; each chunk also gets the file's name as source and its absolute path as path. A null argument, a null key or value, or a path that does not exist is a failed result of kind VALIDATION.

Ingests a file or a directory as ingest(path) does, attaching metadata to every chunk; each chunk also gets the file's name as source and its absolute path as path. A null argument, a null key or value, or a path that does not exist is a failed result of kind VALIDATION.

Attributes

def ingestText(text: String, documentId: String): LlmResult[Integer]

Ingests text as the document documentId, returning how many chunks were indexed. Ingesting an id again replaces what was indexed under it. The same as ingestText(text, documentId, Map.of()).

Ingests text as the document documentId, returning how many chunks were indexed. Ingesting an id again replaces what was indexed under it. The same as ingestText(text, documentId, Map.of()).

Attributes

def ingestText(text: String, documentId: String, metadata: Map[String, String]): LlmResult[Integer]

Ingests text as the document documentId, attaching metadata to every chunk. A null argument, a blank id, or a null key or value is a failed result of kind VALIDATION.

Ingests text as the document documentId, attaching metadata to every chunk. A null argument, a blank id, or a null key or value is a failed result of kind VALIDATION.

Attributes

def search(query: String): LlmResult[List[JRagSource]]

The chunks that match query best, at most the builder's topK, best first, without asking a model: what ask would answer from. Works with or without a chat client. A null or blank query is a failed result of kind VALIDATION. An unmodifiable list.

The chunks that match query best, at most the builder's topK, best first, without asking a model: what ask would answer from. Works with or without a chat client. A null or blank query is a failed result of kind VALIDATION. An unmodifiable list.

Attributes

def search(query: String, topK: Int): LlmResult[List[JRagSource]]

The chunks that match query best, as search(query) returns them, at most topK, at least 1.

The chunks that match query best, as search(query) returns them, at most topK, at least 1.

Attributes

override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any