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
- Source
- JRag.scala
- Graph
-
- Supertypes
Members list
Value members
Concrete methods
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. When no chunk is found - nothing is indexed - the model is not asked (#3022): the answer says nothing relevant was found, with no sources and no usage.
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. When no chunk is found - nothing is indexed - the model is not asked (#3022): the answer says nothing relevant was found, with no sources and no usage.
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
- Source
- JRag.scala
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
- Source
- JRag.scala
Answers question as ask(question) does, from the chunks options lets through (#3022): those its filter matches and its minimum score admits, at most its topK. When none is left the model is not asked: the answer is options' no-context answer, with no sources. A null options is a failed result of kind VALIDATION.
Answers question as ask(question) does, from the chunks options lets through (#3022): those its filter matches and its minimum score admits, at most its topK. When none is left the model is not asked: the answer is options' no-context answer, with no sources. A null options is a failed result of kind VALIDATION.
Attributes
- Source
- JRag.scala
Answers question, a turn of a chat, as ask(question, history) does, with options as ask(question, options).
Answers question, a turn of a chat, as ask(question, history) does, with options as ask(question, options).
Attributes
- Source
- JRag.scala
Answers question, a turn of a chat, as ask(question) does, with history - the conversation so far, oldest first - sent to the model before the chunks and the question (#2766), so it can answer "and the second one?". Build the messages with JMessage.user and JMessage.assistant, or pass back a JAgentResult's. With a question condenser on the builder (questionCondenser) and a non-empty history, the search uses the standalone question it builds; the model still answers question.
Answers question, a turn of a chat, as ask(question) does, with history - the conversation so far, oldest first - sent to the model before the chunks and the question (#2766), so it can answer "and the second one?". Build the messages with JMessage.user and JMessage.assistant, or pass back a JAgentResult's. With a question condenser on the builder (questionCondenser) and a non-empty history, the search uses the standalone question it builds; the model still answers question.
List<JMessage> history = List.of(
JMessage.user("Which plans do you sell?"),
JMessage.assistant("Basic and Pro."));
JRagAnswer answer = rag.ask("How much is the second one?", history).get();
A null history, a null message in it or a SYSTEM message (the pipeline sends its own system prompt) is a failed result of kind VALIDATION, before any search or model call.
Attributes
- Source
- JRag.scala
Answers question with history as ask(question, history) does, from at most topK chunks, at least 1.
Answers question with history as ask(question, history) does, from at most topK chunks, at least 1.
Attributes
- Source
- JRag.scala
Answers question as ask(question) does, streaming the answer to listener as it is generated (#2727): the sources first, once, before the model is asked, so a chat UI can show its citations while the answer arrives; then each piece of the answer's text; then the whole answer or the error (see JRagStreamListener). Returns at once with the running JRagAnswerStream, which cancels the call (close() or cancel()) and awaits the answer (await()).
Answers question as ask(question) does, streaming the answer to listener as it is generated (#2727): the sources first, once, before the model is asked, so a chat UI can show its citations while the answer arrives; then each piece of the answer's text; then the whole answer or the error (see JRagStreamListener). Returns at once with the running JRagAnswerStream, which cancels the call (close() or cancel()) and awaits the answer (await()).
JRagAnswerStream stream = rag.askStream("How long do refunds take?", new JRagStreamListener() {
public void onSources(List<JRagSource> sources) { ui.showSources(sources); }
public void onChunk(String text) { ui.append(text); }
}).get();
JRagAnswer answer = stream.await().get();
What ask returns as a failed result at once - a null or blank question, a null listener, a pipeline built without a chat client - is a failed result here too, and the listener hears nothing. A failure once the call has started, of the embedding provider, the search or the model, reaches JRagStreamListener.onError.
Attributes
- Source
- JRag.scala
Streams the answer to question as askStream(question, listener) does, from at most topK chunks, at least 1.
Streams the answer to question as askStream(question, listener) does, from at most topK chunks, at least 1.
Attributes
- Source
- JRag.scala
Streams the answer to question as askStream(question, listener) does, from the chunks options lets through (#3022). When none is left the model is not asked: the listener hears no sources, then the no-context answer as one chunk.
Streams the answer to question as askStream(question, listener) does, from the chunks options lets through (#3022). When none is left the model is not asked: the listener hears no sources, then the no-context answer as one chunk.
Attributes
- Source
- JRag.scala
Streams the answer to question with history, as askStream(question, history, listener), with options.
Streams the answer to question with history, as askStream(question, history, listener), with options.
Attributes
- Source
- JRag.scala
Streams the answer to question, a turn of a chat, as askStream(question, listener) does, with history sent to the model as ask(question, history) sends it. A null history, a null message or a SYSTEM message in it is a failed result of kind VALIDATION, and the listener hears nothing.
Streams the answer to question, a turn of a chat, as askStream(question, listener) does, with history sent to the model as ask(question, history) sends it. A null history, a null message or a SYSTEM message in it is a failed result of kind VALIDATION, and the listener hears nothing.
Attributes
- Source
- JRag.scala
Streams the answer to question with history, as askStream(question, history, listener), from topK chunks.
Streams the answer to question with history, as askStream(question, history, listener), from topK chunks.
Attributes
- Source
- JRag.scala
How many chunks the store holds, read from the store (#3022): a document ingested again replaces its chunks. A store that cannot be read counts 0.
How many chunks the store holds, read from the store (#3022): a document ingested again replaces its chunks. A store that cannot be read counts 0.
Attributes
- Source
- JRag.scala
Deletes every document from the store (#3021).
Releases the store. The embedding and chat clients the pipeline was built with are not closed. Never throws, as every other call here does not (#3021): a store that fails to close is left as it is.
Releases the store. The embedding and chat clients the pipeline was built with are not closed. Never throws, as every other call here does not (#3021): a store that fails to close is left as it is.
Attributes
- Definition Classes
- Source
- JRag.scala
Deletes the document documentId - every chunk indexed under it - from the store (#3021): the way to remove a stale document from a persistent store. Deleting an id that is not indexed succeeds. A null or blank id is a failed result of kind VALIDATION.
Deletes the document documentId - every chunk indexed under it - from the store (#3021): the way to remove a stale document from a persistent store. Deleting an id that is not indexed succeeds. A null or blank id is a failed result of kind VALIDATION.
Attributes
- Source
- JRag.scala
How many documents the store holds, read from the store (#3022): a store reopened from a file counts what an earlier pipeline indexed, and a document ingested twice counts once. A store that cannot be read counts 0; storedChunkCount returns a failed result instead.
How many documents the store holds, read from the store (#3022): a store reopened from a file counts what an earlier pipeline indexed, and a document ingested twice counts once. A store that cannot be read counts 0; storedChunkCount returns a failed result instead.
Attributes
- Source
- JRag.scala
Ingests a file, or every file in a directory and its subdirectories that DirectoryLoader reads, returning how many chunks were indexed. A file's id is its absolute path: ingesting the same file again replaces what was indexed under it, and two files of the same name in different directories are two documents. The same as ingest(path, Map.of()).
Ingests a file, or every file in a directory and its subdirectories that DirectoryLoader reads, returning how many chunks were indexed. A file's id is its absolute path: ingesting the same file again replaces what was indexed under it, and two files of the same name in different directories are two documents. The same as ingest(path, Map.of()).
Attributes
- Source
- JRag.scala
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
- Source
- JRag.scala
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
- Source
- JRag.scala
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
- Source
- JRag.scala
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
- Source
- JRag.scala
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
- Source
- JRag.scala
The chunks that match query best, as search(query) returns them, that options lets through (#3022): those its filter matches and its minimum score admits, at most its topK; possibly none. A null options, or one out of range, is a failed result of kind VALIDATION.
The chunks that match query best, as search(query) returns them, that options lets through (#3022): those its filter matches and its minimum score admits, at most its topK; possibly none. A null options, or one out of range, is a failed result of kind VALIDATION.
Attributes
- Source
- JRag.scala
How many chunks the store holds, read from the store (#3021): unlike chunkCount, it counts what an earlier run of a persistent store indexed too.
How many chunks the store holds, read from the store (#3021): unlike chunkCount, it counts what an earlier run of a persistent store indexed too.
Attributes
- Source
- JRag.scala
Brings the store up to date with a file, or every file of a directory and its subdirectories (#3021): a file not yet indexed is ingested, one whose content changed since it was last synced is ingested again, one unchanged is left as it is - not embedded again - and a document an earlier sync of the same path indexed that is no longer there is deleted. Ids are absolute paths, as for ingest(path). Returns what it did; a file that could not be read is listed in its failures and keeps what was indexed before.
Brings the store up to date with a file, or every file of a directory and its subdirectories (#3021): a file not yet indexed is ingested, one whose content changed since it was last synced is ingested again, one unchanged is left as it is - not embedded again - and a document an earlier sync of the same path indexed that is no longer there is deleted. Ids are absolute paths, as for ingest(path). Returns what it did; a file that could not be read is listed in its failures and keeps what was indexed before.
A SQLite store remembers what each sync indexed in a third file beside its two (docs-registry.db for docs.db), so a sync after a restart changes only what changed. An in-memory store starts empty, so its first sync ingests everything. The same as sync(path, "default"): syncing two directories into one store needs a sourceId each, or each deletes what the other indexed. A null path or one that does not exist is a failed result of kind VALIDATION.
Attributes
- Source
- JRag.scala
Syncs path as sync(path) does, as the source sourceId among several: it deletes only the documents an earlier sync of sourceId indexed, so syncing two directories into one store, each under its own sourceId, never deletes the other's. Use the same sourceId for the same source on every run. A null or blank sourceId is a failed result of kind VALIDATION.
Syncs path as sync(path) does, as the source sourceId among several: it deletes only the documents an earlier sync of sourceId indexed, so syncing two directories into one store, each under its own sourceId, never deletes the other's. Use the same sourceId for the same source on every run. A null or blank sourceId is a failed result of kind VALIDATION.
Attributes
- Source
- JRag.scala
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
- Source
- JRag.scala
