From e0edea6d51daa79d8565910657a4cfc53bb49dee Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 21 Aug 2025 23:41:19 +0800 Subject: [PATCH] fix notebook print --- cookbook/pageindex_RAG_simple.ipynb | 108 ++++++++++++++++++---------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/cookbook/pageindex_RAG_simple.ipynb b/cookbook/pageindex_RAG_simple.ipynb index 7c87062..3fa21a2 100644 --- a/cookbook/pageindex_RAG_simple.ipynb +++ b/cookbook/pageindex_RAG_simple.ipynb @@ -103,10 +103,9 @@ }, "outputs": [], "source": [ - "import os, json, openai, requests\n", + "import os, json, openai, requests, textwrap\n", "from pageindex import PageIndexClient\n", "from pprint import pprint\n", - "from IPython.display import Markdown, display\n", "\n", "PAGEINDEX_API_KEY = \"YOUR_PAGEINDEX_API_KEY\" # Get your PageIndex API key from https://dash.pageindex.ai/api-keys\n", "OPENAI_API_KEY = \"YOUR_OPENAI_API_KEY\"\n", @@ -125,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, "metadata": { "id": "hmj3POkDcG1N" }, @@ -152,7 +151,10 @@ "\n", "def print_tree(tree, exclude_fields=['text', 'page_index']):\n", " cleaned_tree = remove_fields(tree.copy(), exclude_fields)\n", - " pprint(cleaned_tree, sort_dicts=False, width=150)\n", + " pprint(cleaned_tree, sort_dicts=False, width=100)\n", + "\n", + "def show(text, width=100):\n", + " print(textwrap.fill(text, width=width))\n", "\n", "def create_node_mapping(tree):\n", " \"\"\"Create a mapping of node_id to node for quick lookup\"\"\"\n", @@ -231,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -249,34 +251,47 @@ "[{'title': 'DeepSeek-R1: Incentivizing Reasoning Capability in...',\n", " 'node_id': '0000',\n", " 'prefix_summary': '# DeepSeek-R1: Incentivizing Reasoning Capability ...',\n", - " 'nodes': [{'title': 'Abstract', 'node_id': '0001', 'summary': 'The partial document introduces two reasoning mode...'},\n", - " {'title': 'Contents', 'node_id': '0002', 'summary': 'This partial document provides a detailed table of...'},\n", + " 'nodes': [{'title': 'Abstract',\n", + " 'node_id': '0001',\n", + " 'summary': 'The partial document introduces two reasoning mode...'},\n", + " {'title': 'Contents',\n", + " 'node_id': '0002',\n", + " 'summary': 'This partial document provides a detailed table of...'},\n", " {'title': '1. Introduction',\n", " 'node_id': '0003',\n", " 'prefix_summary': 'The partial document introduces recent advancement...',\n", - " 'nodes': [{'title': '1.1. Contributions', 'node_id': '0004', 'summary': 'This partial document outlines the main contributi...'},\n", + " 'nodes': [{'title': '1.1. Contributions',\n", + " 'node_id': '0004',\n", + " 'summary': 'This partial document outlines the main contributi...'},\n", " {'title': '1.2. Summary of Evaluation Results',\n", " 'node_id': '0005',\n", " 'summary': 'The partial document provides a summary of evaluat...'}]},\n", " {'title': '2. Approach',\n", " 'node_id': '0006',\n", " 'prefix_summary': '## 2. Approach\\n',\n", - " 'nodes': [{'title': '2.1. Overview', 'node_id': '0007', 'summary': '### 2.1. Overview\\n\\nPrevious work has heavily relie...'},\n", + " 'nodes': [{'title': '2.1. Overview',\n", + " 'node_id': '0007',\n", + " 'summary': '### 2.1. Overview\\n\\nPrevious work has heavily relie...'},\n", " {'title': '2.2. DeepSeek-R1-Zero: Reinforcement Learning on t...',\n", " 'node_id': '0008',\n", " 'prefix_summary': '### 2.2. DeepSeek-R1-Zero: Reinforcement Learning ...',\n", " 'nodes': [{'title': '2.2.1. Reinforcement Learning Algorithm',\n", " 'node_id': '0009',\n", - " 'summary': 'This partial document describes the Group Relative...'},\n", + " 'summary': 'This partial document describes the Group '\n", + " 'Relative...'},\n", " {'title': '2.2.2. Reward Modeling',\n", " 'node_id': '0010',\n", - " 'summary': 'This partial document discusses the reward modelin...'},\n", + " 'summary': 'This partial document discusses the reward '\n", + " 'modelin...'},\n", " {'title': '2.2.3. Training Template',\n", " 'node_id': '0011',\n", - " 'summary': '#### 2.2.3. Training Template\\n\\nTo train DeepSeek-R...'},\n", + " 'summary': '#### 2.2.3. Training Template\\n'\n", + " '\\n'\n", + " 'To train DeepSeek-R...'},\n", " {'title': '2.2.4. Performance, Self-evolution Process and Aha...',\n", " 'node_id': '0012',\n", - " 'summary': 'This partial document discusses the performance, s...'}]},\n", + " 'summary': 'This partial document discusses the performance, '\n", + " 's...'}]},\n", " {'title': '2.3. DeepSeek-R1: Reinforcement Learning with Cold...',\n", " 'node_id': '0013',\n", " 'summary': 'This partial document describes the training pipel...'},\n", @@ -292,11 +307,15 @@ " {'title': '3.2. Distilled Model Evaluation',\n", " 'node_id': '0017',\n", " 'summary': 'This partial document presents an evaluation of va...'}]},\n", - " {'title': '4. Discussion', 'node_id': '0018', 'summary': 'This partial document discusses the comparative ef...'},\n", + " {'title': '4. Discussion',\n", + " 'node_id': '0018',\n", + " 'summary': 'This partial document discusses the comparative ef...'},\n", " {'title': '5. Conclusion, Limitations, and Future Work',\n", " 'node_id': '0019',\n", " 'summary': 'This partial document presents the conclusion, lim...'},\n", - " {'title': 'References', 'node_id': '0020', 'summary': 'The partial document consists of a comprehensive r...'},\n", + " {'title': 'References',\n", + " 'node_id': '0020',\n", + " 'summary': 'The partial document consists of a comprehensive r...'},\n", " {'title': 'Appendix', 'node_id': '0021', 'summary': '## Appendix\\n'},\n", " {'title': 'A. Contributions and Acknowledgments',\n", " 'node_id': '0022',\n", @@ -331,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "id": "LLHNJAtTcG1O" }, @@ -371,7 +390,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -386,7 +405,14 @@ "output_type": "stream", "text": [ "Reasoning Process:\n", - "The question asks for the conclusions in the document. Typically, conclusions are found in sections explicitly titled 'Conclusion' or in combined sections such as 'Conclusion, Limitations, and Future Work.' In this document tree, node 0019 ('5. Conclusion, Limitations, and Future Work') is the most directly relevant. Additionally, sometimes the 'Abstract' (node 0001) and 'Discussion' (node 0018) sections may contain summary statements or high-level conclusions, but the primary and most likely location for formal conclusions is node 0019.\n", + "The question asks for the conclusions in the document. Typically, conclusions are found in sections\n", + "explicitly titled 'Conclusion' or in sections summarizing the findings and implications of the work.\n", + "In this document tree, node 0019 ('5. Conclusion, Limitations, and Future Work') is the most\n", + "directly relevant, as it is dedicated to the conclusion and related topics. Additionally, the\n", + "'Abstract' (node 0001) may contain a high-level summary that sometimes includes concluding remarks,\n", + "but it is less likely to contain the full conclusions. Other sections like 'Discussion' (node 0018)\n", + "may discuss implications but are not explicitly conclusions. Therefore, the primary node is 0019.\n", + "\n", "Retrieved Nodes:\n", "Node ID: 0019\t Page: 16\t Title: 5. Conclusion, Limitations, and Future Work\n" ] @@ -397,9 +423,9 @@ "tree_search_result_json = json.loads(tree_search_result)\n", "\n", "print('Reasoning Process:')\n", - "print(tree_search_result_json['thinking'])\n", + "show(tree_search_result_json['thinking'])\n", "\n", - "print('Retrieved Nodes:')\n", + "print('\\nRetrieved Nodes:')\n", "for node_id in tree_search_result_json[\"node_list\"]:\n", " node = node_map[node_id]\n", " print(f\"Node ID: {node['node_id']}\\t Page: {node['page_index']}\\t Title: {node['title']}\")" @@ -423,7 +449,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -438,13 +464,17 @@ "output_type": "stream", "text": [ "Retrieved Context:\n", - "## 5. Conclusion, Limitations, and Future Work\n", - "\n", - "In this work, we share our journey in enhancing model reasoning abilities through reinforcement learning. DeepSeek-R1-Zero represents a pure RL approach without relying on cold-start data, achieving strong performance across various tasks. DeepSeek-R1 is more powerful, leveraging cold-start data alongside iterative RL fine-tuning. Ultimately, DeepSeek-R1 achieves performance comparable to OpenAI-o1-1217 on a range of tasks.\n", - "\n", - "We further explore distillation the reasoning capability to small dense models. We use DeepSeek-R1 as the teacher model to generate 800K training samples, and fine-tune several small dense models. The results are promising: DeepSeek-R1-Distill-Qwen-1.5B outperforms GPT-4o and Claude-3.5-Sonnet on math benchmarks with $28.9 \\%$ on AIME and $83.9 \\%$ on MATH. Other dense models also achieve impressive results, significantly outperforming other instructiontuned models based on the same underlying checkpoints.\n", - "\n", - "In the fut ...\n" + "## 5. Conclusion, Limitations, and Future Work In this work, we share our journey in enhancing\n", + "model reasoning abilities through reinforcement learning. DeepSeek-R1-Zero represents a pure RL\n", + "approach without relying on cold-start data, achieving strong performance across various tasks.\n", + "DeepSeek-R1 is more powerful, leveraging cold-start data alongside iterative RL fine-tuning.\n", + "Ultimately, DeepSeek-R1 achieves performance comparable to OpenAI-o1-1217 on a range of tasks. We\n", + "further explore distillation the reasoning capability to small dense models. We use DeepSeek-R1 as\n", + "the teacher model to generate 800K training samples, and fine-tune several small dense models. The\n", + "results are promising: DeepSeek-R1-Distill-Qwen-1.5B outperforms GPT-4o and Claude-3.5-Sonnet on\n", + "math benchmarks with $28.9 \\%$ on AIME and $83.9 \\%$ on MATH. Other dense models also achieve\n", + "impressive results, significantly outperforming other instructiontuned models based on the same\n", + "underlying checkpoints. In the fut...\n" ] } ], @@ -453,7 +483,7 @@ "relevant_content = \"\\n\\n\".join(node_map[node_id][\"text\"] for node_id in node_list)\n", "\n", "print('Retrieved Context:')\n", - "print(relevant_content[:1000] + ' ...')" + "show(relevant_content[:1000] + '...')" ] }, { @@ -465,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -480,14 +510,14 @@ "output_type": "stream", "text": [ "Generated Answer:\n", - "**Conclusions in this document:**\n", - "\n", - "- DeepSeek-R1-Zero, a pure reinforcement learning (RL) model without cold-start data, achieves strong performance across various tasks.\n", - "- DeepSeek-R1, which combines cold-start data with iterative RL fine-tuning, is more powerful and achieves performance comparable to OpenAI-o1-1217 on a range of tasks.\n", - "- Distilling DeepSeek-R1’s reasoning capabilities into smaller dense models is effective; for example, DeepSeek-R1-Distill-Qwen-1.5B outperforms GPT-4o and Claude-3.5-Sonnet on math benchmarks.\n", - "- Other small dense models fine-tuned with DeepSeek-R1 data also significantly outperform other instruction-tuned models based on the same checkpoints.\n", - "\n", - "These results demonstrate the effectiveness of RL-based approaches and distillation for enhancing model reasoning abilities and performance.\n" + "**Conclusions in this document:** - DeepSeek-R1-Zero, a pure reinforcement learning (RL) model\n", + "without cold-start data, achieves strong performance across various tasks. - DeepSeek-R1, which\n", + "combines cold-start data with iterative RL fine-tuning, is even more powerful and achieves\n", + "performance comparable to OpenAI-o1-1217 on a range of tasks. - Distilling DeepSeek-R1’s reasoning\n", + "capabilities into smaller dense models is effective: DeepSeek-R1-Distill-Qwen-1.5B outperforms\n", + "GPT-4o and Claude-3.5-Sonnet on math benchmarks, and other dense models also show significant\n", + "improvements over similar instruction-tuned models. - Overall, the approaches described demonstrate\n", + "promising results in enhancing model reasoning abilities through RL and distillation.\n" ] } ], @@ -503,7 +533,7 @@ "\n", "print('Generated Answer:')\n", "answer = await call_llm(answer_prompt)\n", - "print(answer)" + "show(answer)" ] }, {