diff options
Diffstat (limited to 'bash/dds')
-rwxr-xr-x | bash/dds | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/bash/dds b/bash/dds new file mode 100755 index 0000000..f14a79b --- /dev/null +++ b/bash/dds @@ -0,0 +1,121 @@ +#!/bin/bash + +# Daydreaming System +# This script uses a sequence of LLM calls to refine an initial response. + +# --- Model Configuration --- +RESPONSE_MODEL="llama3:8b-instruct-q4_K_M" +CRITIC_MODEL="phi3:3.8b-mini-4k-instruct-q4_K_M" +REFINE_MODEL="llama3:8b-instruct-q4_K_M" + +# --- Defaults --- +DEFAULT_LOOPS=2 + +# --- Argument Validation --- +if [ "$#" -lt 1 ]; then + echo -e "\n\tDaydreaming" + echo -e "\tThis script uses a sequence of LLM calls to refine an initial response." + echo -e "\n\tUsage: $0 [-f <file_path>] \"<your prompt>\" [number_of_refinement_loops]" + echo -e "\n\tExample: $0 -f ./input.txt \"Please summarize this text file\" 2" + echo -e "\n\tIf number_of_refinement_loops is not provided, the program will default to $DEFAULT_LOOPS loops." + 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 + LOOPS=$DEFAULT_LOOPS +else + LOOPS=$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/dds_$(date +%Y%m%d_%H%M%S).txt + +echo "DDS Session Log: ${SESSION_FILE}" +echo "---------------------------------" + +# --- Initial Prompt & Response --- + +# 1. Store the initial user prompt in the session file +echo "USER PROMPT: ${PROMPT}" >> "${SESSION_FILE}" +echo "" >> "${SESSION_FILE}" # Add a newline for readability +echo "Processing initial response..." + +# 2. The RESPONSE model generates the first answer +RESPONSE_PROMPT="You are an expert, curious assistant who isn't afraid to say when they don't know something. Please respond directly to the following prompt: ${PROMPT}" +RESPONSE_OUTPUT=$(ollama run "${RESPONSE_MODEL}" "${RESPONSE_PROMPT}") + +# Append the response to the session file +echo "INITIAL RESPONSE (${RESPONSE_MODEL}):" >> "${SESSION_FILE}" +echo "${RESPONSE_OUTPUT}" >> "${SESSION_FILE}" +echo "" >> "${SESSION_FILE}" + +# --- Refinement Loop --- + +# This variable will hold the most recent response for the next loop iteration +CURRENT_RESPONSE="${RESPONSE_OUTPUT}" + +for i in $(seq 1 "${LOOPS}"); do + echo "Starting refinement loop ${i} of ${LOOPS}..." + + # 3. The CRITIC model reviews the last response + CRITIC_PROMPT="You are a detail oriented, close reading, keenly critical reviewer. Your task is to raise questions, flag potential misunderstandings, and areas for improved clarity in the following text. Provide concise, constructive criticism. Do not rewrite the text, only critique it. TEXT TO CRITIQUE: ${CURRENT_RESPONSE}" + CRITIC_OUTPUT=$(ollama run "${CRITIC_MODEL}" "${CRITIC_PROMPT}") + + # Append the critique to the session file + echo "CRITICISM ${i} (${CRITIC_MODEL}):" >> "${SESSION_FILE}" + echo "${CRITIC_OUTPUT}" >> "${SESSION_FILE}" + echo "" >> "${SESSION_FILE}" + + # 4. The REFINE model reads the original prompt and the critique to generate a new response + REFINE_PROMPT="You are an expert assistant. Your previous response was reviewed and critiqued. Your task now is to generate a refined, improved response to the original prompt based on the feedback provided. ORIGINAL PROMPT: ${PROMPT} CONSTRUCTIVE CRITICISM: ${CRITIC_OUTPUT} Generate the refined response now." + REFINE_OUTPUT=$(ollama run "${REFINE_MODEL}" "${REFINE_PROMPT}") + + # Append the refined response to the session file + echo "REFINED RESPONSE ${i} (${REFINE_MODEL}):" >> "${SESSION_FILE}" + echo "${REFINE_OUTPUT}" >> "${SESSION_FILE}" + echo "" >> "${SESSION_FILE}" + + # Update the current response for the next loop or for the final output + CURRENT_RESPONSE="${REFINE_OUTPUT}" +done + +# --- Final Output --- + +echo "---------------------------------" +echo "DDS process complete." +echo "Final refined answer:" +echo "---------------------------------" + +# Print the final, most refined answer to standard output +echo "${CURRENT_RESPONSE}" |