#!/bin/bash
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

set -e
WORKPATH=$(dirname "$PWD")
export WORKDIR=$WORKPATH/../
echo "WORKDIR=${WORKDIR}"
export ip_address=$(hostname -I | awk '{print $1}')
export host_ip=${ip_address}

export HF_CACHE_DIR=$WORKPATH/hf_cache
if [ ! -d "$HF_CACHE_DIR" ]; then
    echo "Creating HF_CACHE directory"
    mkdir -p "$HF_CACHE_DIR"
fi

function start_retrieval_tool() {
    echo "Starting Retrieval tool"
    cd $WORKPATH/../DocIndexRetriever/docker_compose/intel/cpu/xeon
    host_ip=$(hostname -I | awk '{print $1}')
    export HF_CACHE_DIR=${HF_CACHE_DIR}
    export HF_TOKEN=${HF_TOKEN}
    export no_proxy=${no_proxy}
    export http_proxy=${http_proxy}
    export https_proxy=${https_proxy}
    export EMBEDDING_MODEL_ID="BAAI/bge-base-en-v1.5"
    export RERANK_MODEL_ID="BAAI/bge-reranker-base"
    export TEI_EMBEDDING_ENDPOINT="http://${host_ip}:6006"
    export TEI_RERANKING_ENDPOINT="http://${host_ip}:8808"
    export REDIS_URL="redis://${host_ip}:6379"
    export INDEX_NAME="rag-redis"
    export RERANK_TYPE="tei"
    export MEGA_SERVICE_HOST_IP=${host_ip}
    export EMBEDDING_SERVICE_HOST_IP=${host_ip}
    export RETRIEVER_SERVICE_HOST_IP=${host_ip}
    export RERANK_SERVICE_HOST_IP=${host_ip}
    export BACKEND_SERVICE_ENDPOINT="http://${host_ip}:8889/v1/retrievaltool"
    export DATAPREP_SERVICE_ENDPOINT="http://${host_ip}:6007/v1/dataprep/ingest"
    export DATAPREP_GET_FILE_ENDPOINT="http://${host_ip}:6008/v1/dataprep/get"
    export DATAPREP_DELETE_FILE_ENDPOINT="http://${host_ip}:6009/v1/dataprep/delete"

    docker compose -f compose.yaml up -d
}

echo "==================== Start retrieval tool ===================="
start_retrieval_tool
sleep 20 # needed for downloading the models
echo "==================== Retrieval tool started ===================="
