#!/bin/bash # Get the directory where this script is located SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Synthesis System # This script combines outputs from multiple thinking mechanisms into a coherent final response. # # APPLICATION LOGIC: # The synthesis process implements a multi-mechanism integration system that combines # outputs from different thinking strategies into a unified, coherent response. The system # operates through three distinct phases designed to maximize comprehensiveness and clarity: # # PHASE 1 - MECHANISM EXECUTION: # - Executes multiple thinking mechanisms on the same prompt # - Collects diverse perspectives and approaches # - Ensures comprehensive coverage of the problem space # - Creates a foundation for synthesis # # PHASE 2 - CONFLICT RESOLUTION: # - Identifies contradictions and conflicts between mechanism outputs # - Resolves disagreements through logical analysis # - Prioritizes information based on confidence and relevance # - Maintains intellectual honesty about uncertainties # # PHASE 3 - SYNTHESIS GENERATION: # - Combines the best elements from each mechanism # - Creates a coherent narrative that addresses all aspects # - Provides a unified response that leverages multiple perspectives # - Ensures the final output is greater than the sum of its parts # # SYNTHESIS MODELING: # The system applies integrative thinking principles to AI response generation: # - Multiple mechanisms provide diverse perspectives on the same problem # - Conflict resolution ensures logical consistency in the final output # - Synthesis leverages the strengths of each individual mechanism # - The process may reveal insights that individual mechanisms miss # - Transparency shows how different perspectives were integrated # - The method may provide more comprehensive and balanced responses # # The synthesis process emphasizes comprehensiveness and coherence, # ensuring users get the benefits of multiple thinking approaches in a unified response. # Source the logging system using absolute path source "${SCRIPT_DIR}/logging.sh" # --- Model Configuration --- SYNTHESIS_MODEL="llama3:8b-instruct-q4_K_M" # --- Defaults --- DEFAULT_MECHANISMS=("consensus" "critique" "socratic") # --- Argument Validation --- if [ "$#" -lt 1 ]; then echo -e "\n\tSynthesis" echo -e "\tThis script combines outputs from multiple thinking mechanisms into a coherent final response." echo -e "\n\tUsage: $0 [-f ] [-m ] \"\" [number_of_rounds]" echo -e "\n\tExample: $0 -f ./input.txt -m consensus,critique \"Please analyze this text\" 2" echo -e "\n\tIf number_of_rounds is not provided, the program will default to 2 rounds." echo -e "\n\t-f (optional): Append the contents of the file to the prompt." echo -e "\n\t-m (optional): Comma-separated list of mechanisms to use (default: consensus,critique,socratic)." echo -e "\n" exit 1 fi # --- Argument Parsing --- FILE_PATH="" MECHANISMS_STR="" while getopts "f:m:" opt; do case $opt in f) FILE_PATH="$OPTARG" ;; m) MECHANISMS_STR="$OPTARG" ;; *) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; esac done shift $((OPTIND -1)) PROMPT="$1" if [ -z "$2" ]; then ROUNDS=2 else ROUNDS=$2 fi # Parse mechanisms if [ -n "$MECHANISMS_STR" ]; then IFS=',' read -ra MECHANISMS <<< "$MECHANISMS_STR" else MECHANISMS=("${DEFAULT_MECHANISMS[@]}") fi # If file path is provided, append its contents to the prompt if [ -n "$FILE_PATH" ]; then if [ ! -f "$FILE_PATH" ]; then echo "File not found: $FILE_PATH" >&2 exit 1 fi FILE_CONTENTS=$(cat "$FILE_PATH") PROMPT="$PROMPT\n[FILE CONTENTS]\n$FILE_CONTENTS\n[END FILE]" fi # --- File Initialization --- mkdir -p ~/tmp SESSION_FILE=~/tmp/synthesis_$(date +%Y%m%d_%H%M%S).txt # Initialize timing SESSION_ID=$(generate_session_id) start_timer "$SESSION_ID" "synthesis" echo "Synthesis Session Log: ${SESSION_FILE}" echo "---------------------------------" # Store the initial user prompt in the session file echo "USER PROMPT: ${PROMPT}" >> "${SESSION_FILE}" echo "MECHANISMS: ${MECHANISMS[*]}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" # --- Phase 1: Mechanism Execution --- echo "Phase 1: Executing thinking mechanisms..." echo "PHASE 1 - MECHANISM EXECUTION:" >> "${SESSION_FILE}" declare -a mechanism_outputs declare -a mechanism_names for i in "${!MECHANISMS[@]}"; do mechanism="${MECHANISMS[$i]}" echo " Executing ${mechanism} mechanism..." # Execute the mechanism using absolute path if [ -f "${SCRIPT_DIR}/${mechanism}" ]; then output=$("${SCRIPT_DIR}/${mechanism}" "${PROMPT}" "${ROUNDS}" 2>&1) mechanism_outputs[$i]="${output}" mechanism_names[$i]="${mechanism}" echo "MECHANISM ${i+1} (${mechanism}):" >> "${SESSION_FILE}" echo "${output}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" else echo " WARNING: Mechanism ${mechanism} not found, skipping..." >&2 fi done # --- Phase 2: Conflict Resolution --- echo "Phase 2: Analyzing and resolving conflicts..." echo "PHASE 2 - CONFLICT RESOLUTION:" >> "${SESSION_FILE}" # Create conflict analysis prompt CONFLICT_PROMPT="You are a conflict resolution specialist. Analyze the following outputs from different thinking mechanisms and identify any contradictions, conflicts, or areas of disagreement. ORIGINAL PROMPT: ${PROMPT} MECHANISM OUTPUTS:" for i in "${!MECHANISMS[@]}"; do if [ -n "${mechanism_outputs[$i]}" ]; then CONFLICT_PROMPT="${CONFLICT_PROMPT} ${mechanism_names[$i]} OUTPUT: ${mechanism_outputs[$i]}" fi done CONFLICT_PROMPT="${CONFLICT_PROMPT} Please identify: 1. Any direct contradictions between the outputs 2. Areas where the mechanisms disagree 3. Information that appears to be conflicting 4. How these conflicts might be resolved Provide a clear analysis of conflicts and potential resolutions." conflict_analysis=$(ollama run "${SYNTHESIS_MODEL}" "${CONFLICT_PROMPT}") echo "CONFLICT ANALYSIS:" >> "${SESSION_FILE}" echo "${conflict_analysis}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" # --- Phase 3: Synthesis Generation --- echo "Phase 3: Generating unified synthesis..." echo "PHASE 3 - SYNTHESIS GENERATION:" >> "${SESSION_FILE}" # Create synthesis prompt SYNTHESIS_PROMPT="You are a synthesis specialist. Your task is to combine the outputs from multiple thinking mechanisms into a coherent, unified response that leverages the strengths of each approach. ORIGINAL PROMPT: ${PROMPT} MECHANISM OUTPUTS:" for i in "${!MECHANISMS[@]}"; do if [ -n "${mechanism_outputs[$i]}" ]; then SYNTHESIS_PROMPT="${SYNTHESIS_PROMPT} ${mechanism_names[$i]} OUTPUT: ${mechanism_outputs[$i]}" fi done SYNTHESIS_PROMPT="${SYNTHESIS_PROMPT} CONFLICT ANALYSIS: ${conflict_analysis} Please create a unified synthesis that: 1. Combines the best insights from each mechanism 2. Resolves any identified conflicts logically 3. Provides a comprehensive response that addresses all aspects 4. Maintains intellectual honesty about uncertainties 5. Creates a coherent narrative that flows naturally 6. Leverages the unique strengths of each thinking approach Your synthesis should be greater than the sum of its parts - it should provide insights that individual mechanisms might miss." final_synthesis=$(ollama run "${SYNTHESIS_MODEL}" "${SYNTHESIS_PROMPT}") echo "FINAL SYNTHESIS:" >> "${SESSION_FILE}" echo "${final_synthesis}" >> "${SESSION_FILE}" # End timing duration=$(end_timer "$SESSION_ID" "synthesis") # --- Final Output --- echo "---------------------------------" echo "Synthesis process complete." echo "Final unified response:" echo "---------------------------------" echo "${final_synthesis}" echo "" echo "Mechanisms used: ${MECHANISMS[*]}" echo "Execution time: ${duration} seconds" echo "" echo "Full synthesis log: ${SESSION_FILE}"