|
398 | 398 | } |
399 | 399 | ], |
400 | 400 | "source": [ |
401 | | - "#import os\n", |
| 401 | + "# import os\n", |
402 | 402 | "import time\n", |
403 | 403 | "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings\n", |
404 | 404 | "from llama_index.llms.llama_cpp import LlamaCPP\n", |
|
411 | 411 | "\n", |
412 | 412 | "# Set the path to your downloaded GGUF model\n", |
413 | 413 | "# IMPORTANT: Use a raw string (r\"...\") for Windows paths\n", |
414 | | - "#MODEL_PATH =r\"D:\\\\Mistral7B\\\\mistral-7b-instruct-v0.2.Q4_K_M.gguf\"\n", |
| 414 | + "# MODEL_PATH =r\"D:\\\\Mistral7B\\\\mistral-7b-instruct-v0.2.Q4_K_M.gguf\"\n", |
415 | 415 | "\n", |
416 | 416 | "# Set the path to your data (PDFs, .txt, etc.)\n", |
417 | 417 | "DATA_PATH = r\"D:\\Mistral7B\\data\"\n", |
|
596 | 596 | ], |
597 | 597 | "source": [ |
598 | 598 | "print(\"Initializing models...\")\n", |
599 | | - "MODEL_PATH =r\"D:\\Mistral7B\\tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf\"\n", |
| 599 | + "MODEL_PATH = r\"D:\\Mistral7B\\tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf\"\n", |
600 | 600 | "\n", |
601 | 601 | "# Load the local LLM (Mistral 7B) with GPU offloading\n", |
602 | 602 | "llm = LlamaCPP(\n", |
|
677 | 677 | "# Create the query engine, passing in the new template\n", |
678 | 678 | "query_engine = index.as_query_engine(\n", |
679 | 679 | " streaming=True,\n", |
680 | | - " text_qa_template=qa_template,# <-- Pass the template here\n", |
| 680 | + " text_qa_template=qa_template, # <-- Pass the template here\n", |
681 | 681 | " similarity_top_k=3,\n", |
682 | | - " include_source_nodes=True\n", |
| 682 | + " include_source_nodes=True,\n", |
683 | 683 | ")\n", |
684 | 684 | "# --- END MODIFICATION ---\n", |
685 | 685 | "\n", |
|
2772 | 2772 | } |
2773 | 2773 | ], |
2774 | 2774 | "source": [ |
2775 | | - "\n", |
2776 | | - "\n", |
2777 | 2775 | "# --- Define the new prompts for the Critic and Refiner ---\n", |
2778 | 2776 | "\n", |
2779 | 2777 | "CRITIC_PROMPT = \"\"\"\n", |
|
2837 | 2835 | "try:\n", |
2838 | 2836 | " while True:\n", |
2839 | 2837 | " query = input(\"Ask a question about your documents: \")\n", |
2840 | | - " if query.lower() == 'exit':\n", |
| 2838 | + " if query.lower() == \"exit\":\n", |
2841 | 2839 | " break\n", |
2842 | 2840 | "\n", |
2843 | 2841 | " # --- Start tracking the entire multi-step process ---\n", |
|
2846 | 2844 | "\n", |
2847 | 2845 | " # --- Step 1: Get the Draft (and Context) ---\n", |
2848 | 2846 | " response_stream = query_engine.query(query)\n", |
2849 | | - " \n", |
| 2847 | + "\n", |
2850 | 2848 | " # Collect the streamed draft text\n", |
2851 | 2849 | " draft_text = \"\"\n", |
2852 | 2850 | " for chunk_text in response_stream.response_gen:\n", |
2853 | 2851 | " draft_text += chunk_text\n", |
2854 | | - " \n", |
| 2852 | + "\n", |
2855 | 2853 | " # Extract the source context\n", |
2856 | 2854 | " context_str = \"\\n---\\n\".join(\n", |
2857 | 2855 | " [node.get_content() for node in response_stream.source_nodes]\n", |
|
2862 | 2860 | " print(textwrap.fill(draft_text, width=80))\n", |
2863 | 2861 | "\n", |
2864 | 2862 | " # --- MODIFICATION: Start Recursive Loop ---\n", |
2865 | | - " \n", |
2866 | | - " current_draft = draft_text # Initialize the loop with the first draft\n", |
2867 | | - " \n", |
| 2863 | + "\n", |
| 2864 | + " current_draft = draft_text # Initialize the loop with the first draft\n", |
| 2865 | + "\n", |
2868 | 2866 | " for i in range(REFINEMENT_CYCLES):\n", |
2869 | 2867 | " print(f\"\\n--- Refinement Cycle {i + 1}/{REFINEMENT_CYCLES} ---\")\n", |
2870 | 2868 | "\n", |
|
2873 | 2871 | " critic_prompt = CRITIC_PROMPT.format(\n", |
2874 | 2872 | " context=context_str,\n", |
2875 | 2873 | " question=query,\n", |
2876 | | - " draft=current_draft # Use the *current* draft\n", |
| 2874 | + " draft=current_draft, # Use the *current* draft\n", |
2877 | 2875 | " )\n", |
2878 | | - " \n", |
| 2876 | + "\n", |
2879 | 2877 | " feedback_response = llm.complete(critic_prompt)\n", |
2880 | 2878 | " feedback_text = feedback_response.text\n", |
2881 | 2879 | " print(feedback_text)\n", |
|
2884 | 2882 | " if \"The draft is perfect\" in feedback_text:\n", |
2885 | 2883 | " print(\"--- Critic approved. Stopping refinement loop. ---\")\n", |
2886 | 2884 | " break # Exit the for loop early\n", |
2887 | | - " \n", |
| 2885 | + "\n", |
2888 | 2886 | " # --- Step 4: Run the Refiner ---\n", |
2889 | 2887 | " print(\"--- Refiner is working... ---\")\n", |
2890 | 2888 | " refiner_prompt = REFINER_PROMPT.format(\n", |
2891 | | - " draft=current_draft,\n", |
2892 | | - " feedback=feedback_text\n", |
| 2889 | + " draft=current_draft, feedback=feedback_text\n", |
2893 | 2890 | " )\n", |
2894 | | - " \n", |
| 2891 | + "\n", |
2895 | 2892 | " refiner_response = llm.complete(refiner_prompt)\n", |
2896 | | - " \n", |
| 2893 | + "\n", |
2897 | 2894 | " # --- Step 5: Update Draft for Next Loop ---\n", |
2898 | | - " current_draft = refiner_response.text # The refined answer becomes the new draft\n", |
| 2895 | + " current_draft = (\n", |
| 2896 | + " refiner_response.text\n", |
| 2897 | + " ) # The refined answer becomes the new draft\n", |
2899 | 2898 | "\n", |
2900 | | - " print(f\"--- Intermediate Refined Draft (Cycle {i+1}) ---\")\n", |
| 2899 | + " print(f\"--- Intermediate Refined Draft (Cycle {i + 1}) ---\")\n", |
2901 | 2900 | " print(textwrap.fill(current_draft, width=80))\n", |
2902 | 2901 | "\n", |
2903 | 2902 | " # --- END OF MODIFIED LOOP ---\n", |
|
2924 | 2923 | " total_emissions_kg = tracker.stop()\n", |
2925 | 2924 | " print(\"\\n\\n--- Total Emissions Summary (Session) ---\")\n", |
2926 | 2925 | " if tracker.emissions_data:\n", |
2927 | | - " print(f\"Total Energy Consumed: {tracker.emissions_data.energy_consumed * 1000:.4f} Wh\")\n", |
| 2926 | + " print(\n", |
| 2927 | + " f\"Total Energy Consumed: {tracker.emissions_data.energy_consumed * 1000:.4f} Wh\"\n", |
| 2928 | + " )\n", |
2928 | 2929 | " print(f\"Total CO2 Emitted: {total_emissions_kg * 1000:.4f} gCO2eq\")\n", |
2929 | 2930 | " print(\"Full report saved to 'emissions.csv'\")" |
2930 | 2931 | ] |
|
0 commit comments