about summary refs log tree commit diff stats
path: root/bash/synthesis
blob: 417279e27c1f78e29b2623559eac5446a432a459 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/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 <file_path>] [-m <mechanism1,mechanism2,...>] \"<your prompt>\" [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 <file_path> (optional): Append the contents of the file to the prompt."
    echo -e "\n\t-m <mechanisms> (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}"