diff --git a/tools/configure_llms.sh b/tools/configure_llms.sh index 57190c7..83dde0f 100644 --- a/tools/configure_llms.sh +++ b/tools/configure_llms.sh @@ -22,8 +22,8 @@ tmp_AWS_DEFAULT_REGION="" tmp_AWS_ACCESS_KEY_ID="" tmp_AWS_SECRET_ACCESS_KEY="" tmp_OPENAI_API_KEY="" -tmp_OPENAI_API_VERSION="" tmp_AZURE_OPENAI_API_KEY="" +tmp_OPENAI_API_VERSION="" tmp_AZURE_OPENAI_ENDPOINT="" # Function to print colored messages @@ -63,6 +63,20 @@ validate_openai_api_key() { return 1 } +# Function to validate Azure OpenAI endpoint +validate_azure_endpoint() { + local endpoint=$1 + [[ $endpoint =~ ^https://[a-zA-Z0-9-]+\.openai\.azure\.com/?$ ]] && return 0 + return 1 +} + +# Function to validate API version +validate_api_version() { + local version=$1 + [[ $version =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && return 0 + return 1 +} + # Function to read existing configuration into temporary variables read_existing_config() { if [ -f "$CONFIG_FILE" ]; then @@ -73,8 +87,8 @@ read_existing_config() { "AWS_ACCESS_KEY_ID") tmp_AWS_ACCESS_KEY_ID="$value" ;; "AWS_SECRET_ACCESS_KEY") tmp_AWS_SECRET_ACCESS_KEY="$value" ;; "OPENAI_API_KEY") tmp_OPENAI_API_KEY="$value" ;; - "OPENAI_API_VERSION") tmp_OPENAI_API_VERSION="$value" ;; "AZURE_OPENAI_API_KEY") tmp_AZURE_OPENAI_API_KEY="$value" ;; + "OPENAI_API_VERSION") tmp_OPENAI_API_VERSION="$value" ;; "AZURE_OPENAI_ENDPOINT") tmp_AZURE_OPENAI_ENDPOINT="$value" ;; esac fi @@ -89,20 +103,21 @@ save_configuration() { # Create or truncate the config file echo "" > "$CONFIG_FILE" || { print_color "$RED" "Error: Cannot write to '$CONFIG_FILE'"; return 1; } - if [ "$provider" = "bedrock" ]; then - # Update AWS variables - tmp_AWS_DEFAULT_REGION="$AWS_DEFAULT_REGION" - tmp_AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" - tmp_AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" - elif [ "$provider" = "openai" ]; then - # Update OpenAI variables - tmp_OPENAI_API_KEY="$OPENAI_API_KEY" - tmp_OPENAI_API_VERSION="$OPENAI_API_VERSION" - elif [ "$provider" = "azure" ]; then - # Update Azure variables - tmp_AZURE_OPENAI_API_KEY="$AZURE_OPENAI_API_KEY" - tmp_AZURE_OPENAI_ENDPOINT="$AZURE_OPENAI_ENDPOINT" - fi + case "$provider" in + "bedrock") + tmp_AWS_DEFAULT_REGION="$AWS_DEFAULT_REGION" + tmp_AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" + tmp_AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" + ;; + "openai") + tmp_OPENAI_API_KEY="$OPENAI_API_KEY" + ;; + "azure") + tmp_AZURE_OPENAI_API_KEY="$AZURE_OPENAI_API_KEY" + tmp_OPENAI_API_VERSION="$OPENAI_API_VERSION" + tmp_AZURE_OPENAI_ENDPOINT="$AZURE_OPENAI_ENDPOINT" + ;; + esac # Save all configurations if [ -n "$tmp_AWS_ACCESS_KEY_ID" ]; then @@ -113,11 +128,11 @@ save_configuration() { if [ -n "$tmp_OPENAI_API_KEY" ]; then echo "OPENAI_API_KEY=$tmp_OPENAI_API_KEY" >> "$CONFIG_FILE" - echo "OPENAI_API_VERSION=$tmp_OPENAI_API_VERSION" >> "$CONFIG_FILE" fi - + if [ -n "$tmp_AZURE_OPENAI_API_KEY" ]; then echo "AZURE_OPENAI_API_KEY=$tmp_AZURE_OPENAI_API_KEY" >> "$CONFIG_FILE" + echo "OPENAI_API_VERSION=$tmp_OPENAI_API_VERSION" >> "$CONFIG_FILE" echo "AZURE_OPENAI_ENDPOINT=$tmp_AZURE_OPENAI_ENDPOINT" >> "$CONFIG_FILE" fi @@ -130,11 +145,11 @@ save_configuration() { if [ -n "$tmp_OPENAI_API_KEY" ]; then export OPENAI_API_KEY="$tmp_OPENAI_API_KEY" - export OPENAI_API_VERSION="$tmp_OPENAI_API_VERSION" fi - + if [ -n "$tmp_AZURE_OPENAI_API_KEY" ]; then export AZURE_OPENAI_API_KEY="$tmp_AZURE_OPENAI_API_KEY" + export OPENAI_API_VERSION="$tmp_OPENAI_API_VERSION" export AZURE_OPENAI_ENDPOINT="$tmp_AZURE_OPENAI_ENDPOINT" fi @@ -152,4 +167,222 @@ check_existing_config() { cp "$CONFIG_FILE" "${CONFIG_FILE}.backup" print_color "$GREEN" "Backup created at ${CONFIG_FILE}.backup" fi -} \ No newline at end of file +} + +# Function to display current configuration file +display_config() { + if [ ! -f "$CONFIG_FILE" ]; then + print_color "$YELLOW" "No configuration file found at $CONFIG_FILE" + return + fi + + read_existing_config + + print_header "Current Configuration File" + + print_color "$GREEN" "AWS Bedrock Configuration:" + if [ -n "$tmp_AWS_ACCESS_KEY_ID" ]; then + echo "AWS_DEFAULT_REGION=${tmp_AWS_DEFAULT_REGION}" + echo "AWS_ACCESS_KEY_ID=${tmp_AWS_ACCESS_KEY_ID}" + echo "AWS_SECRET_ACCESS_KEY=********" + else + print_color "$YELLOW" "Bedrock is not configured" + fi + + echo + print_color "$GREEN" "OpenAI Configuration:" + if [ -n "$tmp_OPENAI_API_KEY" ]; then + echo "OPENAI_API_KEY=${tmp_OPENAI_API_KEY}" + else + print_color "$YELLOW" "OpenAI is not configured" + fi + + echo + print_color "$GREEN" "Azure OpenAI Configuration:" + if [ -n "$tmp_AZURE_OPENAI_API_KEY" ]; then + echo "AZURE_OPENAI_API_KEY=********" + echo "OPENAI_API_VERSION=${tmp_OPENAI_API_VERSION}" + echo "AZURE_OPENAI_ENDPOINT=${tmp_AZURE_OPENAI_ENDPOINT}" + else + print_color "$YELLOW" "Azure OpenAI is not configured" + fi + echo +} + +# Function to display current environment variables +display_env_vars() { + print_header "Current Environment Variables" + + print_color "$GREEN" "AWS Bedrock Environment Variables:" + echo "AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-(not set)}" + echo "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-(not set)}" + if [ -n "$AWS_SECRET_ACCESS_KEY" ]; then + echo "AWS_SECRET_ACCESS_KEY=********" + else + echo "AWS_SECRET_ACCESS_KEY=(not set)" + fi + + echo + print_color "$GREEN" "OpenAI Environment Variables:" + echo "OPENAI_API_KEY=${OPENAI_API_KEY:-(not set)}" + + echo + print_color "$GREEN" "Azure OpenAI Environment Variables:" + if [ -n "$AZURE_OPENAI_API_KEY" ]; then + echo "AZURE_OPENAI_API_KEY=********" + else + echo "AZURE_OPENAI_API_KEY=(not set)" + fi + echo "OPENAI_API_VERSION=${OPENAI_API_VERSION:-(not set)}" + echo "AZURE_OPENAI_ENDPOINT=${AZURE_OPENAI_ENDPOINT:-(not set)}" + echo + + # Compare configuration with environment variables + if [ -f "$CONFIG_FILE" ]; then + read_existing_config + + local has_mismatch=false + if [ -n "$tmp_AWS_DEFAULT_REGION" ] && [ "$tmp_AWS_DEFAULT_REGION" != "$AWS_DEFAULT_REGION" ]; then + has_mismatch=true + fi + if [ -n "$tmp_AWS_ACCESS_KEY_ID" ] && [ "$tmp_AWS_ACCESS_KEY_ID" != "$AWS_ACCESS_KEY_ID" ]; then + has_mismatch=true + fi + if [ -n "$tmp_AWS_SECRET_ACCESS_KEY" ] && [ "$tmp_AWS_SECRET_ACCESS_KEY" != "$AWS_SECRET_ACCESS_KEY" ]; then + has_mismatch=true + fi + if [ -n "$tmp_OPENAI_API_KEY" ] && [ "$tmp_OPENAI_API_KEY" != "$OPENAI_API_KEY" ]; then + has_mismatch=true + fi + if [ -n "$tmp_AZURE_OPENAI_API_KEY" ] && [ "$tmp_AZURE_OPENAI_API_KEY" != "$AZURE_OPENAI_API_KEY" ]; then + has_mismatch=true + fi + if [ -n "$tmp_OPENAI_API_VERSION" ] && [ "$tmp_OPENAI_API_VERSION" != "$OPENAI_API_VERSION" ]; then + has_mismatch=true + fi + if [ -n "$tmp_AZURE_OPENAI_ENDPOINT" ] && [ "$tmp_AZURE_OPENAI_ENDPOINT" != "$AZURE_OPENAI_ENDPOINT" ]; then + has_mismatch=true + fi + + if [ "$has_mismatch" = true ]; then + print_color "$YELLOW" "Warning: Some environment variables don't match the configuration file." + print_color "$YELLOW" "Run 'source $CONFIG_FILE' to sync them." + fi + fi +} + +# Function to configure providers +configure_provider() { + print_color "$GREEN" "Select your LLM provider to configure:" + echo "1) AWS Bedrock" + echo "2) OpenAI" + echo "3) Azure OpenAI" + echo -n "Enter your choice (1/2/3): " + read provider_choice + + case $provider_choice in + 1) + print_color "$BLUE" "\nConfiguring AWS Bedrock..." + + while true; do + echo -n "Enter your AWS region (e.g., us-east-1): " + read AWS_DEFAULT_REGION + if validate_aws_region "$AWS_DEFAULT_REGION"; then + break + fi + print_color "$RED" "Invalid AWS region. Please enter a valid region." + done + + echo -n "Enter your AWS Access Key ID: " + read AWS_ACCESS_KEY_ID + echo -n "Enter your AWS Secret Access Key: " + read -s AWS_SECRET_ACCESS_KEY + echo + + save_configuration "bedrock" + ;; + + 2) + print_color "$BLUE" "\nConfiguring OpenAI..." + + while true; do + echo -n "Enter your OpenAI API Key (starts with sk-): " + read OPENAI_API_KEY + if validate_openai_api_key "$OPENAI_API_KEY"; then + break + fi + print_color "$RED" "Invalid OpenAI API Key format. Please try again." + done + + print_color "$RED" "Note: Free-tier OpenAI accounts may be subject to rate limits." + print_color "$RED" "We recommend using a paid OpenAI API key for seamless functionality." + + save_configuration "openai" + ;; + + 3) + print_color "$BLUE" "\nConfiguring Azure OpenAI..." + + echo -n "Enter your Azure OpenAI API Key: " + read -s AZURE_OPENAI_API_KEY + echo + + while true; do + echo -n "Enter the API version (YYYY-MM-DD format): " + read OPENAI_API_VERSION + if validate_api_version "$OPENAI_API_VERSION"; then + break + fi + print_color "$RED" "Invalid API version format. Please use YYYY-MM-DD format." + done + + while true; do + echo -n "Enter your Azure OpenAI endpoint (https://.openai.azure.com): " + read AZURE_OPENAI_ENDPOINT + if validate_azure_endpoint "$AZURE_OPENAI_ENDPOINT"; then + break + fi + print_color "$RED" "Invalid endpoint format. Please enter a valid Azure OpenAI endpoint." + done + + save_configuration "azure" + ;; + + *) + print_color "$RED" "Invalid choice. Exiting." + return 1 + ;; + esac + + print_color "$GREEN" "\nConfiguration complete!" + display_config +} + +# Main script +clear +print_color "$BLUE" "=== AGA LLM Configuration Tool ===" +echo +print_color "$GREEN" "Select an option:" +echo "1) Configure LLM providers" +echo "2) View current configuration file" +echo "3) View current environment variables" +echo -n "Enter your choice (1/2/3): " +read main_choice + +case $main_choice in + 1) + check_existing_config + read_existing_config + configure_provider + ;; + 2) + display_config + ;; + 3) + display_env_vars + ;; + *) + print_color "$RED" "Invalid choice. Exiting." + return 1 + ;; +esac