org.llm4s.javaapi.rag
Members list
Type members
Classlikes
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.
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
- Supertypes
Attributes
- Companion
- class
- Source
- JRag.scala
- Supertypes
- Self type
-
JRag.type
An answer being streamed from JRag.askStream to its JRagStreamListener (#2727): cancel it, or await the answer. It is AutoCloseable, and close() cancels, so a try-with-resources block stops a call its body leaves. It works as JLlmClient.stream's CompletionStream does: the search and the model call run on a virtual thread of their own, and cancelling interrupts that thread.
An answer being streamed from JRag.askStream to its JRagStreamListener (#2727): cancel it, or await the answer. It is AutoCloseable, and close() cancels, so a try-with-resources block stops a call its body leaves. It works as JLlmClient.stream's CompletionStream does: the search and the model call run on a virtual thread of their own, and cancelling interrupts that thread.
try (JRagAnswerStream stream = rag.askStream(question, listener).get()) {
LlmResult<JRagAnswer> answer = stream.await();
}
Attributes
- Source
- JRagAnswerStream.scala
- Supertypes
Receives an answer streamed from JRag.askStream (#2727): the sources it is generated from, once, through onSources, before the model is asked; each piece of the answer's text, in order, through onChunk; then exactly one of onComplete (the whole answer, with the same sources) or onError (the error the call failed with, a cancellation included). Calls are made one at a time, never on the caller's thread.
Receives an answer streamed from JRag.askStream (#2727): the sources it is generated from, once, through onSources, before the model is asked; each piece of the answer's text, in order, through onChunk; then exactly one of onComplete (the whole answer, with the same sources) or onError (the error the call failed with, a cancellation included). Calls are made one at a time, never on the caller's thread.
Only onChunk is abstract, so a Java lambda is a listener:
JRagAnswerStream stream = rag.askStream("How long do refunds take?", System.out::print).get();
JRagAnswer answer = stream.await().get();
A failure before the sources are found - of the embedding provider or the search - reaches onError with no onSources. A listener that throws stops the call (its thread is interrupted, which is how llm4s providers are cancelled; one that throws from onSources stops it before the model is asked), and onError receives what it threw.
Attributes
- Source
- JRagStreamListener.scala
- Supertypes
