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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
#!/bin/bash
# Peer Review System
# This script implements a peer review process where one model provides an initial response
# and other models review and suggest improvements through iterative refinement.
#
# APPLICATION LOGIC:
# The peer review process implements a collaborative refinement system where AI models
# provide structured feedback to improve response quality. The system operates through
# three distinct phases designed to enhance clarity, accuracy, and completeness:
#
# PHASE 1 - INITIAL RESPONSE GENERATION:
# - A randomly selected model generates the first response to the user's prompt
# - Random selection helps prevent bias toward any particular model
# - The initial response serves as the foundation for peer review
# - This creates a starting point that can be improved through collective feedback
#
# PHASE 2 - PEER REVIEW:
# - Other models analyze the initial response and provide structured feedback
# - Reviewers suggest specific edits, clarifications, and improvements
# - Feedback focuses on clarity, completeness, accuracy, and organization
# - Multiple perspectives may identify different areas for improvement
#
# PHASE 3 - RESPONSE REFINEMENT:
# - The original responding model incorporates peer feedback to create an improved response
# - The model may revise, expand, clarify, or reorganize based on suggestions
# - Iterative improvement may address multiple rounds of feedback
# - The process emphasizes collaborative enhancement rather than replacement
#
# PEER REVIEW MODELING:
# The system applies academic peer review principles to AI response refinement:
# - Random author selection helps prevent systematic bias in initial responses
# - Multiple reviewers provide diverse perspectives on the same work
# - Structured feedback focuses on specific improvements rather than general criticism
# - Author retains control over final revisions while considering peer input
# - Transparency through logging shows the evolution of the response
# - The process may help catch errors, improve clarity, or enhance completeness
#
# The peer review process continues for a configurable number of iterations,
# with each cycle potentially improving upon the previous version.
# The system emphasizes collaborative improvement through structured feedback and revision.
# --- Model Configuration ---
MODELS=(
"llama3:8b-instruct-q4_K_M"
"phi3:3.8b-mini-4k-instruct-q4_K_M"
"deepseek-r1:1.5b"
"gemma3n:e2b"
"dolphin3:latest"
)
# --- Defaults ---
DEFAULT_ITERATIONS=1
# --- Argument Validation ---
if [ "$#" -lt 1 ]; then
echo -e "\n\tPeer Review"
echo -e "\tThis script implements a peer review process where one model provides an initial response"
echo -e "\tand other models review and suggest improvements through iterative refinement."
echo -e "\n\tUsage: $0 [-f <file_path>] \"<your prompt>\" [number_of_review_iterations]"
echo -e "\n\tExample: $0 -f ./input.txt \"Please analyze this text\" 1"
echo -e "\n\tIf number_of_review_iterations is not provided, the program will default to $DEFAULT_ITERATIONS iterations."
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
ITERATIONS=$DEFAULT_ITERATIONS
else
ITERATIONS=$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/peer-review_$(date +%Y%m%d_%H%M%S).txt
echo "Peer Review 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 peer review with ${ITERATIONS} iteration(s)..."
# --- Random Author Selection ---
AUTHOR_INDEX=$((RANDOM % ${#MODELS[@]}))
AUTHOR_MODEL="${MODELS[$AUTHOR_INDEX]}"
echo "Author model selected: ${AUTHOR_MODEL}"
echo "AUTHOR MODEL: ${AUTHOR_MODEL}" >> "${SESSION_FILE}"
echo "" >> "${SESSION_FILE}"
# --- 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, clear, and well-organized in your response.
PROMPT: ${PROMPT}"
INITIAL_RESPONSE=$(ollama run "${AUTHOR_MODEL}" "${INITIAL_PROMPT}")
echo "INITIAL RESPONSE (${AUTHOR_MODEL}):" >> "${SESSION_FILE}"
echo "${INITIAL_RESPONSE}" >> "${SESSION_FILE}"
echo "" >> "${SESSION_FILE}"
# --- Peer Review Iterations ---
CURRENT_RESPONSE="${INITIAL_RESPONSE}"
for iteration in $(seq 1 "${ITERATIONS}"); do
echo "Starting peer review iteration ${iteration} of ${ITERATIONS}..."
echo "PEER REVIEW ITERATION ${iteration}:" >> "${SESSION_FILE}"
echo "=================================" >> "${SESSION_FILE}"
# --- Step 1: Generate Peer Reviews ---
echo "Step 1: Generating peer reviews..."
echo "STEP 1 - PEER REVIEWS:" >> "${SESSION_FILE}"
declare -a reviews
declare -a reviewer_names
review_count=0
for i in "${!MODELS[@]}"; do
# Skip the author model
if [ "$i" -eq "$AUTHOR_INDEX" ]; then
continue
fi
model="${MODELS[$i]}"
echo " Getting peer review from ${model}..."
REVIEW_PROMPT="You are a peer reviewer. Your task is to provide constructive feedback on the following response. Focus on specific suggestions for improvement.
REVIEW GUIDELINES:
- Suggest specific edits or clarifications
- Identify areas that could be made clearer or more concise
- Point out any inaccuracies or missing information
- Suggest improvements to organization or structure
- Be constructive and specific in your feedback
RESPONSE TO REVIEW: ${CURRENT_RESPONSE}
Please provide your peer review feedback in a clear, structured format. Focus on actionable suggestions for improvement."
review_output=$(ollama run "${model}" "${REVIEW_PROMPT}")
reviews[$review_count]="${review_output}"
reviewer_names[$review_count]="${model}"
echo "REVIEW ${review_count+1} (${model}):" >> "${SESSION_FILE}"
echo "${review_output}" >> "${SESSION_FILE}"
echo "" >> "${SESSION_FILE}"
review_count=$((review_count + 1))
done
# --- Step 2: Generate Refined Response ---
echo "Step 2: Generating refined response based on peer feedback..."
echo "STEP 2 - RESPONSE REFINEMENT:" >> "${SESSION_FILE}"
# Combine all reviews for the author
COMBINED_REVIEWS=""
for i in $(seq 0 $((review_count - 1))); do
COMBINED_REVIEWS="${COMBINED_REVIEWS}
REVIEW FROM ${reviewer_names[$i]}:
${reviews[$i]}"
done
REFINE_PROMPT="You are the author of the following response. Your peers have provided constructive feedback to help improve your work. Please revise your response based on their suggestions.
ORIGINAL PROMPT: ${PROMPT}
YOUR CURRENT RESPONSE: ${CURRENT_RESPONSE}
PEER REVIEW FEEDBACK: ${COMBINED_REVIEWS}
Please provide a revised version of your response that:
- Incorporates the constructive feedback from your peers
- Addresses specific suggestions for improvement
- Maintains your original insights while enhancing clarity and completeness
- Shows how you've responded to the peer review process"
REFINED_RESPONSE=$(ollama run "${AUTHOR_MODEL}" "${REFINE_PROMPT}")
echo "REFINED RESPONSE (${AUTHOR_MODEL}):" >> "${SESSION_FILE}"
echo "${REFINED_RESPONSE}" >> "${SESSION_FILE}"
echo "" >> "${SESSION_FILE}"
# Update the current response for the next iteration
CURRENT_RESPONSE="${REFINED_RESPONSE}"
echo "Peer review iteration ${iteration} 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 peer review process below, please provide a concise summary of the key improvements made and the overall quality of the final response.
ORIGINAL PROMPT: ${PROMPT}
FINAL REFINED RESPONSE: ${CURRENT_RESPONSE}
Please provide a summary that:
- Highlights the most significant improvements made through peer review
- Notes the quality and effectiveness of the final response
- Captures the collaborative nature of the peer review process
- Is clear, concise, and well-organized"
FINAL_SUMMARY=$(ollama run "${AUTHOR_MODEL}" "${SUMMARY_PROMPT}")
echo "FINAL SUMMARY (${AUTHOR_MODEL}):" >> "${SESSION_FILE}"
echo "${FINAL_SUMMARY}" >> "${SESSION_FILE}"
# --- Final Output ---
echo "---------------------------------"
echo "Peer review process complete."
echo "Final response:"
echo "---------------------------------"
echo "${CURRENT_RESPONSE}"
echo ""
echo "Author: ${AUTHOR_MODEL}"
echo "Peer Review Summary:"
echo "${FINAL_SUMMARY}"
echo ""
echo "Full peer review log: ${SESSION_FILE}"
|