#!/bin/bash # Socratic System # This script uses the Socratic method to refine responses through AI-generated questions and dialogue. # # APPLICATION LOGIC: # The Socratic process implements an iterative questioning system where AI models # engage in dialogue to explore, clarify, and refine responses. The system operates # through three distinct phases designed to deepen understanding and identify limitations: # # PHASE 1 - INITIAL RESPONSE GENERATION: # - A response model generates the first answer to the user's prompt # - The model provides a comprehensive initial response as the foundation # - This creates the starting point for Socratic exploration # - The response serves as the subject of subsequent questioning # # PHASE 2 - SOCRATIC QUESTIONING: # - A question model analyzes the initial response and generates probing questions # - Questions focus on clarifying assumptions, exploring implications, and considering alternatives # - The question model identifies areas that need deeper examination # - Questions are designed to reveal limitations, gaps, or unclear aspects # # PHASE 3 - RESPONSE REFINEMENT: # - The original response model addresses the Socratic questions # - The model may revise, expand, or clarify its initial response # - This creates a dialogue that deepens the analysis # - The process may reveal what cannot be determined or requires additional information # # SOCRATIC MODELING: # The system applies Socratic questioning principles to AI response refinement: # - Separate models for questioning and responding may provide different perspectives # - Probing questions help identify assumptions and limitations in the initial response # - Iterative dialogue may reveal deeper insights or expose knowledge gaps # - The process emphasizes intellectual honesty about what can and cannot be determined # - Transparency through logging shows the evolution of understanding # - The method may help catch overconfident claims or identify areas needing clarification # # The Socratic process continues for a configurable number of rounds, # with each iteration potentially revealing new insights or limitations. # The system emphasizes depth of analysis and intellectual honesty over definitive answers. # Initialize common functionality source "$(dirname "${BASH_SOURCE[0]}")/common.sh" init_thinking_mechanism "${BASH_SOURCE[0]}" # Use centralized model configuration RESPONSE_MODEL="$SOCRATIC_RESPONSE_MODEL" QUESTION_MODEL="$SOCRATIC_QUESTION_MODEL" # Validate and set models with standardized error handling if ! validate_and_set_model "RESPONSE_MODEL" "$RESPONSE_MODEL" "$FALLBACK_MODEL"; then handle_model_error "$RESPONSE_MODEL" "$(basename "$0")" fi if ! validate_and_set_model "QUESTION_MODEL" "$QUESTION_MODEL" "$FALLBACK_MODEL"; then handle_model_error "$QUESTION_MODEL" "$(basename "$0")" fi # --- Argument Validation --- if [ "$#" -lt 1 ]; then echo -e "\n\tSocratic" echo -e "\tThis script uses the Socratic method to refine responses through AI-generated questions and dialogue." echo -e "\n\tUsage: $0 [-f ] \"\" [number_of_questioning_rounds]" echo -e "\n\tExample: $0 -f ./input.txt \"Please analyze this text\" 2" echo -e "\n\tIf number_of_questioning_rounds is not provided, the program will default to $DEFAULT_ROUNDS rounds." echo -e "\n\t-f (optional): Append the contents of the file to the prompt." echo -e "\n" exit 1 fi # --- Argument Parsing --- FILE_PATH="" while getopts "f:" opt; do case $opt in f) FILE_PATH="$OPTARG" ;; *) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; esac done shift $((OPTIND -1)) PROMPT="$1" if [ -z "$2" ]; then ROUNDS=$DEFAULT_ROUNDS else ROUNDS=$2 fi # If file path is provided, append its contents to the prompt if [ -n "$FILE_PATH" ]; then if [ ! -f "$FILE_PATH" ]; then handle_file_error "$FILE_PATH" "find" "$(basename "$0")" fi if [ ! -r "$FILE_PATH" ]; then handle_file_error "$FILE_PATH" "read" "$(basename "$0")" fi FILE_CONTENTS=$(cat "$FILE_PATH" 2>/dev/null) || handle_file_error "$FILE_PATH" "read contents of" "$(basename "$0")" PROMPT="$PROMPT\n[FILE CONTENTS]\n$FILE_CONTENTS\n[END FILE]" fi # --- File Initialization --- # Create a temporary directory if it doesn't exist mkdir -p ~/tmp # Create a unique file for this session based on the timestamp SESSION_FILE=~/tmp/socratic_$(date +%Y%m%d_%H%M%S).txt echo "Socratic Session Log: ${SESSION_FILE}" echo "---------------------------------" # Store the initial user prompt in the session file echo "USER PROMPT: ${PROMPT}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" echo "Processing Socratic dialogue with ${ROUNDS} questioning rounds..." # --- Initial Response Generation --- echo "Generating initial response..." echo "INITIAL RESPONSE GENERATION:" >> "${SESSION_FILE}" echo "============================" >> "${SESSION_FILE}" INITIAL_PROMPT="You are an expert assistant. Please provide a comprehensive response to the following prompt. Be thorough but also honest about any limitations in your knowledge or areas where you cannot provide definitive answers. PROMPT: ${PROMPT}" INITIAL_RESPONSE=$(ollama run "${RESPONSE_MODEL}" "${INITIAL_PROMPT}") INITIAL_RESPONSE=$(guard_output_quality "$INITIAL_RESPONSE" "$PROMPT" "$MECHANISM_NAME" "$RESPONSE_MODEL") echo "INITIAL RESPONSE (${RESPONSE_MODEL}):" >> "${SESSION_FILE}" echo "${INITIAL_RESPONSE}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" # --- Socratic Dialogue Rounds --- CURRENT_RESPONSE="${INITIAL_RESPONSE}" for round in $(seq 1 "${ROUNDS}"); do echo "Starting Socratic round ${round} of ${ROUNDS}..." echo "SOCRATIC ROUND ${round}:" >> "${SESSION_FILE}" echo "=======================" >> "${SESSION_FILE}" # --- Step 1: Generate Socratic Questions --- echo "Step 1: Generating Socratic questions..." echo "STEP 1 - QUESTION GENERATION:" >> "${SESSION_FILE}" QUESTION_PROMPT="You are a Socratic questioner. Your task is to analyze the following response and generate 2-3 probing questions that will help clarify, refine, or explore the response more deeply. Focus on questions that: - Clarify assumptions or definitions - Explore implications or consequences - Consider alternative perspectives - Identify areas where the response may be incomplete or uncertain - Flag what cannot be determined with the given information RESPONSE TO QUESTION: ${CURRENT_RESPONSE} Generate your questions in a clear, numbered format. Be specific and avoid yes/no questions." QUESTIONS=$(ollama run "${QUESTION_MODEL}" "${QUESTION_PROMPT}") QUESTIONS=$(guard_output_quality "$QUESTIONS" "$PROMPT" "$MECHANISM_NAME" "$QUESTION_MODEL") echo "QUESTIONS (${QUESTION_MODEL}):" >> "${SESSION_FILE}" echo "${QUESTIONS}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" # --- Step 2: Generate Refined Response --- echo "Step 2: Generating refined response to questions..." echo "STEP 2 - RESPONSE REFINEMENT:" >> "${SESSION_FILE}" REFINE_PROMPT="You are an expert assistant. Your previous response has been analyzed and the following Socratic questions have been raised. Please provide a refined, expanded, or clarified response that addresses these questions. ORIGINAL PROMPT: ${PROMPT} YOUR PREVIOUS RESPONSE: ${CURRENT_RESPONSE} SOCRATIC QUESTIONS: ${QUESTIONS} Please provide a comprehensive response that: - Addresses each question raised - Clarifies any assumptions or definitions - Explores implications and alternatives - Honestly acknowledges what cannot be determined - Refines or expands your original response based on the questioning" REFINED_RESPONSE=$(ollama run "${RESPONSE_MODEL}" "${REFINE_PROMPT}") REFINED_RESPONSE=$(guard_output_quality "$REFINED_RESPONSE" "$PROMPT" "$MECHANISM_NAME" "$RESPONSE_MODEL") echo "REFINED RESPONSE (${RESPONSE_MODEL}):" >> "${SESSION_FILE}" echo "${REFINED_RESPONSE}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" # Update the current response for the next round CURRENT_RESPONSE="${REFINED_RESPONSE}" echo "Socratic round ${round} complete." echo "" >> "${SESSION_FILE}" done # --- Final Summary Generation --- echo "Generating final summary..." echo "FINAL SUMMARY GENERATION:" >> "${SESSION_FILE}" echo "========================" >> "${SESSION_FILE}" SUMMARY_PROMPT="You are an expert analyst. Based on the Socratic dialogue below, please provide a concise summary of the key insights, conclusions, and limitations that emerged from the questioning process. ORIGINAL PROMPT: ${PROMPT} FINAL REFINED RESPONSE: ${CURRENT_RESPONSE} Please provide a summary that: - Highlights the most important insights discovered - Identifies key conclusions that can be drawn - Notes any limitations or areas that cannot be determined - Captures the evolution of understanding through the dialogue - Is clear, concise, and well-organized" FINAL_SUMMARY=$(ollama run "${RESPONSE_MODEL}" "${SUMMARY_PROMPT}") FINAL_SUMMARY=$(guard_output_quality "$FINAL_SUMMARY" "$PROMPT" "$MECHANISM_NAME" "$RESPONSE_MODEL") echo "FINAL SUMMARY (${RESPONSE_MODEL}):" >> "${SESSION_FILE}" echo "${FINAL_SUMMARY}" >> "${SESSION_FILE}" # --- Final Output --- echo "---------------------------------" echo "Socratic process complete." echo "Final summary:" echo "---------------------------------" echo "${FINAL_SUMMARY}" echo "" echo "Full Socratic dialogue log: ${SESSION_FILE}"