Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple qdrant servers #30

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"embedding": "https://huggingface.co/second-state/All-MiniLM-L6-v2-Embedding-GGUF/resolve/main/all-MiniLM-L6-v2-ggml-model-f16.gguf",
"embedding_ctx_size": "384",
"snapshot": "https://huggingface.co/datasets/gaianet/paris/resolve/main/paris_384_all-minilm-l6-v2_f16.snapshot",
"embedding_collection_name": "default",
"rag_prompt": "Use the following pieces of context to answer the user's question.\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\n----------------\n",
"domain": "gaianet.xyz",
"llamaedge_port": "8080"
Expand Down
58 changes: 36 additions & 22 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ cwd=$(pwd)
reinstall=0
# url to the config file
config_url=""
# path to the gaianet base directory
gaianet_base_dir="$HOME/gaianet"

function print_usage {
printf "Usage:\n"
printf " ./install.sh [Options]\n\n"
printf "Options:\n"
printf " --config <Url>: specify a url to the config file\n"
printf " --base <Path>: specify a path to the gaianet base directory\n"
printf " --reinstall: install and download all required deps\n"
printf " --help: Print usage\n"
}
Expand All @@ -28,6 +31,11 @@ while [[ $# -gt 0 ]]; do
shift
shift
;;
--base)
gaianet_base_dir="$2"
shift
shift
;;
--reinstall)
reinstall=1
shift
Expand All @@ -44,12 +52,8 @@ while [[ $# -gt 0 ]]; do
esac
done


printf "\n"

# Set "gaianet_base_dir" to $HOME/gaianet
gaianet_base_dir="$HOME/gaianet"

# if need to reinstall, remove the $gaianet_base_dir directory
if [ "$reinstall" -eq 1 ] && [ -d "$gaianet_base_dir" ]; then
printf "[+] Removing the existing $gaianet_base_dir directory ...\n\n"
Expand Down Expand Up @@ -240,11 +244,13 @@ fi
# 10. recover from the given qdrant collection snapshot =======================
printf "[+] Initializing the Qdrant server ...\n\n"

# check 6333 port is in use or not
qdrant_pid=0
qdrant_already_running=false
if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
if lsof -Pi :6333 -sTCP:LISTEN -t >/dev/null ; then
printf "It appears that the GaiaNet node is running. Please stop it first.\n\n"
exit 1
# printf "It appears that the GaiaNet node is running. Please stop it first.\n\n"
# exit 1
qdrant_already_running=true
fi
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
printf "For Windows users, please run this script in WSL.\n"
Expand All @@ -254,15 +260,18 @@ else
exit 1
fi

# start qdrant
cd $gaianet_base_dir/qdrant
nohup $gaianet_base_dir/bin/qdrant > $log_dir/init-qdrant.log 2>&1 &
sleep 15
qdrant_pid=$!
if [ "$qdrant_already_running" = false ]; then
# start qdrant
cd $gaianet_base_dir/qdrant
nohup $gaianet_base_dir/bin/qdrant > $log_dir/init-qdrant.log 2>&1 &
sleep 15
qdrant_pid=$!
fi

cd $gaianet_base_dir
url_snapshot=$(awk -F'"' '/"snapshot":/ {print $4}' config.json)
url_document=$(awk -F'"' '/"document":/ {print $4}' config.json)
embedding_collection_name=$(awk -F'"' '/"embedding_collection_name":/ {print $4}' config.json)

if [ -n "$url_snapshot" ]; then
# 10.1 recover from the given qdrant collection snapshot
Expand All @@ -271,18 +280,18 @@ if [ -n "$url_snapshot" ]; then
curl --progress-bar -L $url_snapshot -o default.snapshot

cd $gaianet_base_dir
# remove the 'default' collection if it exists
del_response=$(curl -s -X DELETE http://localhost:6333/collections/default \
# remove the collection if it exists
del_response=$(curl -s -X DELETE http://localhost:6333/collections/$embedding_collection_name \
-H "Content-Type: application/json")
status=$(echo "$del_response" | grep -o '"status":"[^"]*"' | cut -d':' -f2 | tr -d '"')
if [ "$status" != "ok" ]; then
printf " Failed to remove the 'default' collection. $del_response\n\n"
printf " Failed to remove the $embedding_collection_name collection. $del_response\n\n"
kill $qdrant_pid
exit 1
fi

# Import the default.snapshot file
response=$(curl -s -X POST 'http://localhost:6333/collections/default/snapshots/upload?priority=snapshot' \
response=$(curl -s -X POST http://localhost:6333/collections/$embedding_collection_name/snapshots/upload?priority=snapshot \
-H 'Content-Type:multipart/form-data' \
-F 'snapshot=@default.snapshot')
sleep 5
Expand All @@ -301,14 +310,14 @@ elif [ -n "$url_document" ]; then

printf "[+] Creating a Qdrant collection from the given document ...\n\n"

# Remove the 'default' collection if it exists
printf " * Removing 'default' collection if it exists ...\n\n"
# Remove the collection if it exists
printf " * Removing collection if it exists ...\n\n"
# remove the 'default' collection if it exists
del_response=$(curl -s -X DELETE http://localhost:6333/collections/default \
del_response=$(curl -s -X DELETE http://localhost:6333/collections/$embedding_collection_name \
-H "Content-Type: application/json")
status=$(echo "$del_response" | grep -o '"status":"[^"]*"' | cut -d':' -f2 | tr -d '"')
if [ "$status" != "ok" ]; then
printf " Failed to remove the 'default' collection. $del_response\n\n"
printf " Failed to remove the collection. $del_response\n\n"
kill $qdrant_pid
exit 1
fi
Expand Down Expand Up @@ -337,6 +346,8 @@ elif [ -n "$url_document" ]; then
embedding_model_stem=$(basename "$embedding_model_name" .gguf)
# parse context size for embedding model
embedding_ctx_size=$(awk -F'"' '/"embedding_ctx_size":/ {print $4}' config.json)
# parse cli options for embedding vector collection name
embedding_collection_name=$(awk -F'"' '/"embedding_collection_name":/ {print $4}' config.json)
# parse port for LlamaEdge API Server
llamaedge_port=$(awk -F'"' '/"llamaedge_port":/ {print $4}' config.json)

Expand Down Expand Up @@ -370,6 +381,7 @@ elif [ -n "$url_document" ]; then
rag-api-server.wasm -p $prompt_type \
--model-name $chat_model_stem,$embedding_model_stem \
--ctx-size $chat_ctx_size,$embedding_ctx_size \
--qdrant-collection-name $embedding_collection_name \
--web-ui ./dashboard \
--socket-addr 0.0.0.0:$llamaedge_port \
--log-prompts \
Expand Down Expand Up @@ -424,8 +436,10 @@ else
fi
printf "\n"

# stop qdrant
kill $qdrant_pid
if [ "$qdrant_already_running" = false ]; then
# stop qdrant
kill $qdrant_pid
fi

# ======================================================================================

Expand Down
51 changes: 32 additions & 19 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash

# default is to use frpc
local_only=0
# path to the gaianet base directory
gaianet_base_dir="$HOME/gaianet"

while [[ $# -gt 0 ]]; do
key="$1"
Expand All @@ -9,6 +12,11 @@ while [[ $# -gt 0 ]]; do
local_only=1
shift
;;
--base)
gaianet_base_dir="$2"
shift
shift
;;
*)
echo "Unknown argument: $key"
print_usage
Expand All @@ -21,13 +29,11 @@ done
# represents the directory where the script is located
script_dir=$(pwd)

# Check if "gaianet" directory exists in $HOME
if [ ! -d "$HOME/gaianet" ]; then
printf "Not found $HOME/gaianet\n"
# Check if "gaianet" home directory exists
if [ ! -d "$gaianet_base_dir" ]; then
printf "Not found $gaianet_base_dir\n"
exit 1
fi
# Set "gaianet_base_dir" to $HOME/gaianet
gaianet_base_dir="$HOME/gaianet"

# check if `log` directory exists or not
if [ ! -d "$gaianet_base_dir/log" ]; then
Expand All @@ -44,11 +50,13 @@ fi
# 1. start a Qdrant instance
printf "[+] Starting Qdrant instance ...\n"

qdrant_already_running=false
if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
if lsof -Pi :6333 -sTCP:LISTEN -t >/dev/null ; then
printf " Port 6333 is in use. Stopping the process on 6333 ...\n\n"
pid=$(lsof -t -i:6333)
kill -9 $pid
# printf " Port 6333 is in use. Stopping the process on 6333 ...\n\n"
# pid=$(lsof -t -i:6333)
# kill -9 $pid
qdrant_already_running=true
fi
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
printf "For Windows users, please run this script in WSL.\n"
Expand All @@ -58,17 +66,19 @@ else
exit 1
fi

qdrant_executable="$gaianet_base_dir/bin/qdrant"
if [ -f "$qdrant_executable" ]; then
cd $gaianet_base_dir/qdrant
nohup $qdrant_executable > $log_dir/start-qdrant.log 2>&1 &
sleep 2
qdrant_pid=$!
echo $qdrant_pid > $gaianet_base_dir/qdrant.pid
printf "\n Qdrant instance started with pid: $qdrant_pid\n\n"
else
printf "Qdrant binary not found at $qdrant_executable\n\n"
exit 1
if [ "$qdrant_already_running" = false ]; then
qdrant_executable="$gaianet_base_dir/bin/qdrant"
if [ -f "$qdrant_executable" ]; then
cd $gaianet_base_dir/qdrant
nohup $qdrant_executable > $log_dir/start-qdrant.log 2>&1 &
sleep 2
qdrant_pid=$!
echo $qdrant_pid > $gaianet_base_dir/qdrant.pid
printf "\n Qdrant instance started with pid: $qdrant_pid\n\n"
else
printf "Qdrant binary not found at $qdrant_executable\n\n"
exit 1
fi
fi

# 2. start a LlamaEdge instance
Expand All @@ -94,6 +104,8 @@ rag_prompt=$(awk -F'"' '/"rag_prompt":/ {print $4}' config.json)
reverse_prompt=$(awk -F'"' '/"reverse_prompt":/ {print $4}' config.json)
# parse cli options for embedding model
url_embedding_model=$(awk -F'"' '/"embedding":/ {print $4}' config.json)
# parse cli options for embedding vector collection name
embedding_collection_name=$(awk -F'"' '/"embedding_collection_name":/ {print $4}' config.json)
# gguf filename
embedding_model_name=$(basename $url_embedding_model)
# stem part of the filename
Expand Down Expand Up @@ -133,6 +145,7 @@ cmd=(wasmedge --dir .:./dashboard \
--model-name $chat_model_stem,$embedding_model_stem \
--ctx-size $chat_ctx_size,$embedding_ctx_size \
--prompt-template $prompt_type \
--qdrant-collection-name $embedding_collection_name \
--web-ui ./ \
--socket-addr 0.0.0.0:$llamaedge_port \
--log-prompts \
Expand Down
16 changes: 11 additions & 5 deletions stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

# 1: stop WasmEdge, Qdrant and frpc processes
force_stop=0
# path to the gaianet base directory
gaianet_base_dir="$HOME/gaianet"

function print_usage {
printf "Usage:\n"
printf " ./stop.sh [--force-stop]\n\n"
printf " --force: stop WasmEdge, Qdrant and frpc processes\n"
printf " --base <Path>: stop GaiaNet node installed in a specified home directory\n"
}

while [[ $# -gt 0 ]]; do
Expand All @@ -16,6 +19,11 @@ while [[ $# -gt 0 ]]; do
force_stop=1
shift
;;
--base)
gaianet_base_dir="$2"
shift
shift
;;
--help)
print_usage
exit 0
Expand All @@ -29,12 +37,10 @@ while [[ $# -gt 0 ]]; do
done

# Check if "gaianet" directory exists in $HOME
if [ ! -d "$HOME/gaianet" ]; then
printf "Not found $HOME/gaianet\n"
if [ ! -d "$gaianet_base_dir" ]; then
printf "Not found $gaianet_base_dir\n"
exit 1
fi
# Set "gaianet_base_dir" to $HOME/gaianet
gaianet_base_dir="$HOME/gaianet"

if [ $force_stop -eq 1 ]; then
printf "Force stopping WasmEdge, Qdrant and frpc processes ...\n"
Expand Down Expand Up @@ -84,4 +90,4 @@ else
fi
fi

exit 0
exit 0
Loading