diff --git a/ai-hub/run_chat.sh b/ai-hub/run_chat.sh index b13e8d1..5032780 100644 --- a/ai-hub/run_chat.sh +++ b/ai-hub/run_chat.sh @@ -1,7 +1,7 @@ #!/bin/bash # A script to automatically start the server and run an interactive chat session. -# It now allows the user to specify a model for each turn or use the previous one. +# It now allows the user to specify a model and a FAISS retriever option for each turn. # # REQUIREMENTS: # - 'jq' must be installed (e.g., sudo apt-get install jq). @@ -64,34 +64,56 @@ echo "✅ Session created with ID: $SESSION_ID. The initial model is '$CURRENT_MODEL'." echo "--------------------------------------------------" echo "To switch models, type your message like this: [gemini] " -echo "To use the previous model, just type your message directly." +echo "To enable the FAISS retriever, add the flag: [faiss] " +echo "You can combine them: [gemini] [faiss] " echo "Type 'exit' or 'quit' to end." echo "--------------------------------------------------" # --- 4. Start the Interactive Chat Loop --- while true; do - read -p "You [$CURRENT_MODEL]: " user_input + read -p "You: " user_input if [[ "$user_input" == "exit" || "$user_input" == "quit" ]]; then break fi - # Check for model switch input pattern, e.g., "[model_name] " - if [[ "$user_input" =~ ^\[([a-zA-Z0-9]+)\]\ (.*)$ ]]; then + # Initialize variables for this turn + PROMPT_TEXT="$user_input" + MODEL_TO_USE="$CURRENT_MODEL" + LOAD_FAISS_RETRIEVER="false" + + # Check for and process the model switch + # This regex ensures we only match valid models and they must appear first + if [[ "$PROMPT_TEXT" =~ ^\[(deepseek|gemini)\]\ (.*)$ ]]; then MODEL_TO_USE="${BASH_REMATCH[1]}" PROMPT_TEXT="${BASH_REMATCH[2]}" # Update the current model for the next prompt CURRENT_MODEL="$MODEL_TO_USE" - else - MODEL_TO_USE="$CURRENT_MODEL" - PROMPT_TEXT="$user_input" + fi + + # Check for and process the FAISS flag + # This check is separate to avoid misinterpreting "faiss" as a model + if [[ "$PROMPT_TEXT" =~ \[faiss\] ]]; then + LOAD_FAISS_RETRIEVER="true" + # Use sed to remove the [faiss] flag and trim any leading/trailing spaces + PROMPT_TEXT=$(echo "$PROMPT_TEXT" | sed 's/\[faiss\]//' | xargs) fi - # Construct the JSON payload with the model and prompt + # Check if the prompt is empty after processing flags and models + if [ -z "$PROMPT_TEXT" ]; then + echo "Please enter a message." + continue + fi + + # Construct the JSON payload with the model, prompt, and FAISS flag + # Note the use of --argjson to pass the boolean value correctly json_payload=$(jq -n \ --arg prompt "$PROMPT_TEXT" \ --arg model "$MODEL_TO_USE" \ - '{"prompt": $prompt, "model": $model}') + --argjson faiss_flag "$LOAD_FAISS_RETRIEVER" \ + '{"prompt": $prompt, "model": $model, "load_faiss_retriever": $faiss_flag}') + + echo "Payload: $json_payload" # Optional: for debugging ai_response_json=$(curl -s -X POST "$BASE_URL/sessions/$SESSION_ID/chat" \ -H "Content-Type: application/json" \ @@ -104,9 +126,9 @@ else ai_answer=$(echo "$ai_response_json" | jq -r '.answer') model_used=$(echo "$ai_response_json" | jq -r '.model_used') - echo "AI [$model_used]: $ai_answer" + echo "AI [$model_used] [faiss_enabled: $LOAD_FAISS_RETRIEVER]: $ai_answer" fi done -# The 'trap' will automatically call the cleanup function when the loop breaks. \ No newline at end of file +# The 'trap' will automatically call the cleanup function when the loop breaks.