序
本文主要研究一下langchain4j的Naive RAG
示例
public class Naive_RAG_Example {
/**
* This example demonstrates how to implement a naive Retrieval-Augmented Generation (RAG) application.
* By "naive", we mean that we won't use any advanced RAG techniques.
* In each interaction with the Large Language Model (LLM), we will:
* 1. Take the user's query as-is.
* 2. Embed it using an embedding model.
* 3. Use the query's embedding to search an embedding store (containing small segments of your documents)
* for the X most relevant segments.
* 4. Append the found segments to the user's query.
* 5. Send the combined input (user query + segments) to the LLM.
* 6. Hope that:
* - The user's query is well-formulated and contains all necessary details for retrieval.
* - The found segments are relevant to the user's query.
*/
public static void main(String[] args) {
// Let's create an assistant that will know about our document
Assistant assistant = createAssistant("documents/miles-of-smiles-terms-of-use.txt");
// Now, let's start the conversation with the assistant. We can ask questions like:
// - Can I cancel my reservation?
// - I had an accident, should I pay extra?
startConversationWith(assistant);
}
private static Assistant createAssistant(String documentPath) {
// First, let's create a chat model, also known as a LLM, which will answer our queries.
// In this example, we will use OpenAI's gpt-4o-mini, but you can choose any supported model.
// Langchain4j currently supports more than 10 popular LLM providers.
ChatLanguageModel chatLanguageModel = OpenAiChatModel.builder()
.apiKey(OPENAI_API_KEY)
.modelName(GPT_4_O_MINI)
.build();
// Now, let's load a document that we want to use for RAG.
// We will use the terms of use from an imaginary car rental company, "Miles of Smiles".
// For this example, we'll import only a single document, but you can load as many as you need.
// LangChain4j offers built-in support for loading documents from various sources:
// File System, URL, Amazon S3, Azure Blob Storage, GitHub, Tencent COS.
// Additionally, LangChain4j supports parsing multiple document types:
// text, pdf, doc, xls, ppt.
// However, you can also manually import your data from other sources.
DocumentParser documentParser = new TextDocumentParser();
Document document = loadDocument(toPath(documentPath), documentParser);
// Now, we need to split this document into smaller segments, also known as "chunks."
// This approach allows us to send only relevant segments to the LLM in response to a user query,
// rather than the entire document. For instance, if a user asks about cancellation policies,
// we will identify and send only those segments related to cancellation.
// A good starting point is to use a recursive document splitter that initially attempts
// to split by paragraphs. If a paragraph is too large to fit into a single segment,
// the splitter will recursively divide it by newlines, then by sentences, and finally by words,
// if necessary, to ensure each piece of text fits into a single segment.
DocumentSplitter splitter = DocumentSplitters.recursive(300, 0);
List<TextSegment> segments = splitter.split(document);
// Now, we need to embed (also known as "vectorize") these segments.
// Embedding is needed for performing similarity searche

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



