当前位置:   article > 正文

LangChain4j-RAG高级-检索增强器_bgesmallenv15quantizedembeddingmodel

bgesmallenv15quantizedembeddingmodel

Retrieval Augmentor 检索增强器

RetrievalAugmentor 是 RAG 管道的入口点。它负责使用从各种来源检索的相关 Content 来扩充 ChatMessage

可以在创建 AiService 期间指定 RetrievalAugmentor 的实例:

  1. Assistant assistant = AiServices.builder(Assistant.class)
  2. ...
  3. .retrievalAugmentor(retrievalAugmentor)
  4. .build();

每次调用 AiService 时,都会调用指定的 RetrievalAugmentor 来扩充当前的 UserMessage

可以使用LangChain4j中提供的 RetrievalAugmentor 的默认实现(DefaultRetrievalAugmentor) 或 实现自定义实现。

Default Retrieval Augmentor 默认检索增强器

LangChain4j 提供了RetrievalAugmentor接口的现成实现: DefaultRetrievalAugmentor ,它应该适合大多数 RAG 用例。

官方使用示例:

  1. public class _04_Advanced_RAG_with_Metadata_Example {
  2. /**
  3. * Please refer to {@link Naive_RAG_Example} for a basic context.
  4. * <p>
  5. * Advanced RAG in LangChain4j is described here: https://github.com/langchain4j/langchain4j/pull/538
  6. * <p>
  7. * This example illustrates how to include document source and other metadata into the LLM prompt.
  8. */
  9. public static void main(String[] args) {
  10. Assistant assistant = createAssistant("documents/miles-of-smiles-terms-of-use.txt");
  11. // Ask "What is the name of the file where cancellation policy is defined?".
  12. // Observe how "file_name" metadata entry was injected into the prompt.
  13. startConversationWith(assistant);
  14. }
  15. private static Assistant createAssistant(String documentPath) {
  16. Document document = loadDocument(toPath(documentPath), new TextDocumentParser());
  17. EmbeddingModel embeddingModel = new BgeSmallEnV15QuantizedEmbeddingModel();
  18. EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
  19. EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
  20. .documentSplitter(DocumentSplitters.recursive(300, 0))
  21. .embeddingModel(embeddingModel)
  22. .embeddingStore(embeddingStore)
  23. .build();
  24. ingestor.ingest(document);
  25. ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
  26. .embeddingStore(embeddingStore)
  27. .embeddingModel(embeddingModel)
  28. .build();
  29. // Each retrieved segment should include "file_name" and "index" metadata values in the prompt
  30. ContentInjector contentInjector = DefaultContentInjector.builder()
  31. // .promptTemplate(...) // Formatting can also be changed
  32. .metadataKeysToInclude(asList("file_name", "index"))
  33. .build();
  34. RetrievalAugmentor retrievalAugmentor = DefaultRetrievalAugmentor.builder()
  35. .contentRetriever(contentRetriever)
  36. .contentInjector(contentInjector)
  37. .build();
  38. ChatLanguageModel chatLanguageModel = OpenAiChatModel.builder()
  39. .apiKey(OPENAI_API_KEY)
  40. .baseUrl(OPENAI_API_URL)
  41. .logRequests(true)
  42. .build();
  43. return AiServices.builder(Assistant.class)
  44. .chatLanguageModel(chatLanguageModel)
  45. .retrievalAugmentor(retrievalAugmentor)
  46. .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
  47. .build();
  48. }
  49. }

源码逻辑梳理:

  • 用户在定义AiService的时候需要设置是否需要检索增强器
  • 在触发AiService的调用的时候会在invoke()函数中检查是否有添加RetrievalAugmentor增强器
  • 如果有则会调用context.retrievalAugmentor.augment(augmentationRequest)Query进行处理:

  • augment()方法中使用的QueryTransformer有三种实现类:
    • DefaultQueryTransformer: 默认的转换器,不会对Query做任何处理,而是封装成集合后返回。
    • ExpandingQueryTransformer: 扩展转换器,对Query进行措辞的修改和拆分为多个(默认3个)具体的Query,达到增强效果。
    • CompressingQueryTransformer压缩转换器,根据提供的历史聊天和当前提问内容,LLM理解后会给出更精确的提问。

Query

Query表示 RAG 管道中的用户查询。它包含查询文本和查询元数据。

Query Metadata 查询元数据

Query中的Metadata包含可能对 RAG 管道的各个组件有用的信息,例如:

  • Metadata.userMessage() - 应该增强的原始UserMessage
  • Metadata.chatMemoryId() - @MemoryId注释的方法参数的值。这可用于识别用户并在检索期间应用访问限制或过滤器。
  • Metadata.chatMemory() - 所有历史的ChatMessages 。这可以帮助理解提出Query的上下文。

Query Transformer 查询转换

QueryTransformer将给定的Query转换为一个或多个Query 。目标是通过修改扩展原始Query来提高检索质量。

一些已知的改进检索的方法包括:

  • Query compression 查询压缩
  • Query expansion 查询扩展
  • Query re-writing 查询重写
  • Step-back prompting 后退提示
  • Hypothetical document embeddings (HyDE) 假设文档嵌入

Content

Content表示与用户Query相关的内容。目前,它仅限于文本内容(即TextSegment ),但将来它可能支持其他模式(例如,图像、音频、视频等)。

Content Retriever 内容检索

ContentRetriever使用给定的Query从底层数据源检索Content 。底层数据源几乎可以是任何东西:

  • Embedding store 陷入存储
  • Full-text search engine 全文搜索引擎
  • Hybrid of vector and full-text search 矢量和全文搜索的混合
  • Web Search Engine 网络搜索引擎
  • Knowledge graph 知识图谱
  • SQL database SQL数据库

Embedding Store Content Retriever 嵌入存入内容检索

EmbeddingStoreContentRetriever使用EmbeddingModel来嵌入QueryEmbeddingStore检索相关Content

使用示例:

  1. EmbeddingStore embeddingStore = ...
  2. EmbeddingModel embeddingModel = ...
  3. ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
  4. .embeddingStore(embeddingStore)
  5. .embeddingModel(embeddingModel)
  6. .maxResults(3)
  7. // maxResults can also be specified dynamically depending on the query
  8. .dynamicMaxResults(query -> 3)
  9. .minScore(0.75)
  10. // minScore can also be specified dynamically depending on the query
  11. .dynamicMinScore(query -> 0.75)
  12. .filter(metadataKey("userId").isEqualTo("12345"))
  13. // filter can also be specified dynamically depending on the query
  14. .dynamicFilter(query -> {
  15. String userId = getUserId(query.metadata().chatMemoryId());
  16. return metadataKey("userId").isEqualTo(userId);
  17. })
  18. .build();

Web Search Content Retriever 网络搜索内容

WebSearchContentRetriever使用WebSearchEngine从网络检索相关Content

目前WebSearchEngine接口有 2 个实现:

  • langchain4j-web-search-engine-google-custom模块中的GoogleCustomWebSearchEngine
  • langchain4j-web-search-engine-tavily模块中的TavilyWebSearchEngine

使用示例:

  1. WebSearchEngine googleSearchEngine = GoogleCustomWebSearchEngine.builder()
  2. .apiKey(System.getenv("GOOGLE_API_KEY"))
  3. .csi(System.getenv("GOOGLE_SEARCH_ENGINE_ID"))
  4. .build();
  5. ContentRetriever contentRetriever = WebSearchContentRetriever.builder()
  6. .webSearchEngine(googleSearchEngine)
  7. .maxResults(3)
  8. .build();

SQL Database Content Retriever SQL数据库内容检索

SqlDatabaseContentRetrieverContentRetriever实验性实现,可以在langchain4j-experimental-sql模块中找到。后续可能会删除或改动较大,所以并不推荐直接使用到生产。

它使用DataSourceLLM为给定的自然语言Query生成并执行 SQL 查询。

使用示例:

  1. public class _10_Advanced_RAG_SQL_Database_Retreiver_Example {
  2. /**
  3. * Please refer to {@link Naive_RAG_Example} for a basic context.
  4. * <p>
  5. * Advanced RAG in LangChain4j is described here: https://github.com/langchain4j/langchain4j/pull/538
  6. * <p>
  7. * This example demonstrates how to use SQL database content retriever.
  8. * <p>
  9. * WARNING! Although fun and exciting, {@link SqlDatabaseContentRetriever} is dangerous to use!
  10. * Do not ever use it in production! The database user must have very limited READ-ONLY permissions!
  11. * Although the generated SQL is somewhat validated (to ensure that the SQL is a SELECT statement),
  12. * there is no guarantee that it is harmless. Use it at your own risk!
  13. * <p>
  14. * In this example we will use an in-memory H2 database with 3 tables: customers, products and orders.
  15. * See "resources/sql" directory for more details.
  16. * <p>
  17. * This example requires "langchain4j-experimental-sql" dependency.
  18. */
  19. public static void main(String[] args) {
  20. Assistant assistant = createAssistant();
  21. // You can ask questions such as "How many customers do we have?" and "What is our top seller?".
  22. startConversationWith(assistant);
  23. }
  24. private static Assistant createAssistant() {
  25. DataSource dataSource = createDataSource();
  26. ChatLanguageModel chatLanguageModel = OpenAiChatModel.withApiKey(OPENAI_API_KEY);
  27. ContentRetriever contentRetriever = SqlDatabaseContentRetriever.builder()
  28. .dataSource(dataSource)
  29. .chatLanguageModel(chatLanguageModel)
  30. .build();
  31. return AiServices.builder(Assistant.class)
  32. .chatLanguageModel(chatLanguageModel)
  33. .contentRetriever(contentRetriever)
  34. .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
  35. .build();
  36. }
  37. private static DataSource createDataSource() {
  38. JdbcDataSource dataSource = new JdbcDataSource();
  39. dataSource.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
  40. dataSource.setUser("sa");
  41. dataSource.setPassword("sa");
  42. String createTablesScript = read("sql/create_tables.sql");
  43. execute(createTablesScript, dataSource);
  44. String prefillTablesScript = read("sql/prefill_tables.sql");
  45. execute(prefillTablesScript, dataSource);
  46. return dataSource;
  47. }
  48. private static String read(String path) {
  49. try {
  50. return new String(Files.readAllBytes(toPath(path)));
  51. } catch (IOException e) {
  52. throw new RuntimeException(e);
  53. }
  54. }
  55. private static void execute(String sql, DataSource dataSource) {
  56. try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) {
  57. for (String sqlStatement : sql.split(";")) {
  58. statement.execute(sqlStatement.trim());
  59. }
  60. } catch (SQLException e) {
  61. throw new RuntimeException(e);
  62. }
  63. }
  64. }

Azure AI Search Content Retriever Azure AI 搜索内容检索

AzureAiSearchContentRetriever可以在langchain4j-azure-ai-search模块中找到。

Neo4j Content Retriever Neo4j 内容检索

Neo4jContentRetriever可以在langchain4j-neo4j模块中找到。

Query Router 查询路由

QueryRouter负责将Query路由到适当的ContentRetriever

Default Query Router 默认查询路由

DefaultQueryRouterDefaultRetrievalAugmentor中使用的默认实现。它将每个Query路由到所有配置的ContentRetriever

Language Model Query Router 大语言模型查询路由

LanguageModelQueryRouter使用LLM决定将给定的Query路由到何处。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/944649
推荐阅读
相关标签
  

闽ICP备14008679号