地平線まで行ってくる。

記録あるいは忘備録。時には検討事項。

メモ:RAGを実装してみる。理解、分解、再構築。

これまで、論文をヒントに幾つかのRAGを作成しました。折角なので、それらの機能を分解し、それぞれの機能を組み合わせてみます。今回もローカルLLMで挑戦です。ollamaで動作させたgemma3:4b-it-qatを利用します。4Bクラスとしては高い性能を有していますが、さらに性能の高いAPIを利用したら精度が確保できるという期待も込めて遊びます。コストもかかりません(笑)

 

まずは、機能を分解します。それぞれのRAGの特徴的な機能を分解して、それぞれの機能を実装して揃えます。その結果以下のような状況になりました。色んなパターンが準備できました。

- query_decomposition: Breaks down complex queries into simpler sub-queries. Useful for multi-faceted questions.
- retrieval: Searches the vector database for relevant documents based on a query. Requires 'query' and 'k' (number of results).
- evaluation: Assesses the relevance of retrieved documents to the original question.
- refinement: Generates a refined query based on initial retrieval and evaluation results. Sets 'refined_query' in state.
- reflection: Evaluates the sufficiency of the current answer. Sets 'reflection_result' in state as 'sufficient' or 'insufficient'.
- multi_agent_debate: Uses multiple agents to debate and synthesize information from documents. Good for complex or ambiguous topics. Requires 'documents', 'num_agents', 'max_rounds'.
- aggregation: Combines outputs from multiple sources (e.g., debate agents) into a final answer. Requires 'inputs'.
- reranking: Re-orders retrieved documents based on specific criteria (e.g., RRA, dynamic reranking). Requires 'query', 'documents'.
- synthesis: Generates a final answer based on retrieved documents or other context. Requires 'context'.

 

これらをLLMで組み合わせるようにすれば良いという単純な発想です。ClineとGithub Copilotを利用しつつ実装を進めました。テストも不十分な不具合上等、動けばOKの精神です。

 

Query

Qwen3とDeepRagの違いは?それぞれの特徴も詳しく述べてください。その結果を詳細にレポート形式で述べてください。

QwenとDeepRagの論文や他の論文をベクトル化したデータベースを参照するようにしています。もちろん、QwenはLLMで、DeepRagはRAGの手法です。あえて情報を混ぜるようにしてみました。

 

経過

Loop 1: Evaluating query: 'Qwen3とDeepRagの違いは?それぞれの特徴も詳しく述べてください。その結果を詳細にレポート形式で述べてください。'

Execution plan: query_decomposition_retrieval_synthesis_reflection
Components to execute: query_decomposition, retrieval, synthesis, reflection
Executing component: query_decomposition
Component 'query_decomposition' executed successfully.
Executing component: retrieval
Component 'retrieval' executed successfully.
Executing component: synthesis
Component 'synthesis' executed successfully.
Executing component: reflection
Component 'reflection' executed successfully.

LLMが考えたプランでは、

  1. query_decomposition
  2. retrieval_synthesis
  3. reflection

を順に利用しています。

 

その結果です。

Final Answer
Here’s a concise comparison of Qwen3 and DeepRAG, followed by a report-style summary:

Key Differences & Features:

Qwen3: Primarily a MoE (Mixture of Experts) based model. It’s focused on achieving high performance with efficient inference through expert activation. It excels in STEM, coding, and reasoning benchmarks.
DeepRAG: Emphasizes iterative retrieval and adaptively utilizes its internal knowledge. It is designed for time-sensitive QA tasks, achieving strong performance by decomposing queries and using faithful intermediate answers.
Report-Style Summary:

Qwen3 vs. DeepRAG: A Comparative Analysis

1. Overview

Qwen3 and DeepRAG represent distinct approaches to Retrieval-Augmented Generation (RAG). Qwen3 is built upon a Mixture of Experts (MoE) architecture, prioritizing efficient inference and leveraging specialized knowledge subsets. Conversely, DeepRAG employs iterative retrieval and adaptive knowledge utilization to address time-sensitive QA scenarios.

2. Architectural Differences

Qwen3 (MoE): Utilizes a MoE structure, allowing only a subset of its parameters to be active during inference. This dramatically reduces computational cost while maintaining high performance. It demonstrates comparable performance to Qwen2.5 at higher parameter scales, particularly in STEM, coding, and reasoning.
DeepRAG: Relies on iterative retrieval – repeatedly querying its internal knowledge base to refine the answer. It adapts its approach based on the query and the retrieved context.
3. Performance Characteristics

Time-Sensitive QA: DeepRAG is designed to excel in time-sensitive scenarios, outperforming Qwen3 and gpt-4o in this category.
STEM/Coding/Reasoning: Qwen3 generally surpasses Qwen2.5 and even some other models in these benchmarks.
Comparable Performance with Less Computation: Qwen3 can achieve similar performance to larger Qwen2.5 models with significantly fewer activated parameters.
4. Key Strengths

Qwen3: Efficiency, particularly in large-scale inference; strong performance in specialized tasks (STEM, coding).
DeepRAG: Robustness in time-sensitive QA, effective knowledge boundary recognition, and adaptability.
5. Conclusion

While Qwen3 presents advantages in efficiency and specific benchmarks, DeepRAG excels in dynamic, time-sensitive QA. The choice of model ultimately depends on the specific application requirements. Further research continues to demonstrate the distinct capabilities and trade-offs between these approaches.

Qwen3がRAGのアプローチの一つの様な書き方も見受けられます。混線した様な部分もあります。生成結果をGPT-4oに入れると問題を指摘してくれました。モデル置き換えればいい感じで答えを生成してくれるかもと期待させます。もっとも、4Bモデルでここまで出来たら立派だなと私は考えます。

 

やってみて色んな気づきがありました。例えば、multi_agent_debateといった一部の機能はなかなかLLMが選択しませんでした。その効果をより明文化する必要があるのかもしれません。他にも気になる点は多々ありました。また、強化すべき点もあります。検索した情報のみから答えるような制限をもっと厳しく判定したい、などなど、実際に利用するとなると、改良点は多々あります。さらに、重要なretrievalの部分は、簡単な実装にしてています。最近のcontext lengthの大きいモデルを考慮したり、構造を意識したデータにしたりする必要もあるでしょう。

 

自分で作らなくても、NotebookLLMのような無料で高機能なツールが使えます。が、仕組みの一端を自分で試せたり、論文を参考に手を動かせるのは楽しいです。

 

今回作成したコードです。

github.com