about summary refs log tree commit diff stats
path: root/bash/socratic
blob: 8da0e3d5cabe25bf7c710ab118c15d3b582be5ff (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
#!/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.

# --- Model Configuration ---
RESPONSE_MODEL="llama3:8b-instruct-q4_K_M"
QUESTION_MODEL="phi3:3.8b-mini-4k-instruct-q4_K_M"

# --- Defaults ---
DEFAULT_ROUNDS=2

# --- 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 <file_path>] \"<your prompt>\" [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 <file_path> (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
        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 ---
# 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}")

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}")
    
    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}")
    
    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}")

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}"