diff --git a/config.json b/config.json index 4971775..172b4c6 100644 --- a/config.json +++ b/config.json @@ -1,16 +1,16 @@ { "address": "", - "description": "The default GaiaNet node config with a Llama-3.2-3B-Instruct model and a Paris tour guide knowledge base.", + "description": "The default GaiaNet node config with a Qwen3-4B model and a Paris tour guide knowledge base.", "domain": "gaia.domains", "llamaedge_chat_port": "9068", "llamaedge_embedding_port": "9069", "llamaedge_port": "8080", - "chat": "https://huggingface.co/second-state/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q5_K_M.gguf", - "chat_name": "Llama-3.2-3B-Instruct", - "chat_ctx_size": "16384", + "chat": "https://huggingface.co/second-state/Llama-3-Groq-8B-Tool-Use-GGUF/resolve/main/Llama-3-Groq-8B-Tool-Use-Q5_K_M.gguf", + "chat_name": "Llama-3-Groq-8B", + "chat_ctx_size": "8192", "chat_batch_size": "128", "chat_ubatch_size": "128", - "prompt_template": "llama-3-chat", + "prompt_template": "groq-llama3-tool", "reverse_prompt": "", "system_prompt": "You are a tour guide in Paris, France. Please answer the question from a Paris visitor accurately.", "context_window": "1", diff --git a/gaianet b/gaianet index d2f1111..72a0eca 100755 --- a/gaianet +++ b/gaianet @@ -9,6 +9,15 @@ version="0.5.0" # path to the default gaianet base directory. It could be changed by the --base option gaianet_base_dir="$HOME/gaianet" +# port of kw-search-server +kwsearch_server_port=12306 + +# port of qdrant mcp server +qdrant_mcp_server_port=8003 + +# port of kw-search mcp server +kwsearch_mcp_server_port=8005 + # We will make sure that the path is setup in case the user runs gaianet immediately after init source $HOME/.wasmedge/env @@ -27,6 +36,88 @@ if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ] ulimit -n 10000 fi +# Preset all service corresponding variables, initially set to false +gaia_calculator=false +gaia_weather=false +gaia_qdrant=false +gaia_keyword_search=false + +# Check MCP servers +check_mcp_servers() { + # Usage hint + if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 + fi + + # Define configuration file path + # CONFIG_FILE="$gaianet_base_dir/mcp_config.toml" + CONFIG_FILE="$1" + + # Parse configuration file + current_name="" + while IFS= read -r line; do + # Match name field + if [[ $line =~ name\ *=\ *\"([^\"]+)\" ]]; then + # Replace "-" in name with "_" to use as variable name + current_name=$(echo "${BASH_REMATCH[1]}" | sed 's/-/_/g') + fi + + # Match enable field + if [[ $line =~ enable\ *=\ *(true|false) ]]; then + enable_value="${BASH_REMATCH[1]}" + if [[ "$enable_value" == "true" ]]; then + eval "${current_name}=true" + fi + fi + done < "$CONFIG_FILE" +} + +# Enable/disable specified mcp-server in `gaianet/mcp_config.toml` +toggle_mcp_server() { + # Usage hint + if [ $# -ne 2 ]; then + echo "Usage: $0 " + exit 1 + fi + + CONFIG_FILE="$1" + TARGET_SERVICE="$2" + TMP_FILE="${CONFIG_FILE}.tmp" + + found_service=false + + # Process file + while IFS= read -r line; do + # Check if this is the name line for the target service + if [[ $line =~ name\ *=\ *\"${TARGET_SERVICE}\" ]]; then + found_service=true + echo "$line" >> "$TMP_FILE" + continue + fi + + # If in target service block and encountered enable field + if $found_service && [[ $line =~ enable\ *=\ *(true|false) ]]; then + current_status="${BASH_REMATCH[1]}" + if [[ "$current_status" == "true" ]]; then + new_status="false" + else + new_status="true" + fi + echo "enable = ${new_status}" >> "$TMP_FILE" + found_service=false + # echo "Toggled ${TARGET_SERVICE} to ${new_status}" + continue + fi + + # Write other lines directly + echo "$line" >> "$TMP_FILE" + done < "$CONFIG_FILE" + + # Replace original file + mv "$TMP_FILE" "$CONFIG_FILE" +} + info() { printf "${GREEN}$1${NC}\n\n" } @@ -770,6 +861,130 @@ start_qdrant_instance() { } +start_qdrant_mcp_server() { + qdrant_mcp_server_running=false + if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + if lsof -Pi :$qdrant_mcp_server_port -sTCP:LISTEN -t >/dev/null ; then + qdrant_mcp_server_running=true + fi + elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then + printf "For Windows users, please run this script in WSL.\n" + + # stop + stop_force + + exit 1 + else + printf "Only support Linux, MacOS and Windows.\n" + + # stop + stop_force + + exit 1 + fi + + if [ "$qdrant_mcp_server_running" = false ]; then + qdrant_mcp_server_executable="$gaianet_base_dir/bin/gaia-qdrant-mcp-server" + if [ -f "$qdrant_mcp_server_executable" ]; then + nohup $qdrant_mcp_server_executable > $log_dir/start-qdrant-mcp-server.log 2>&1 & + sleep 5 + qdrant_mcp_server_pid=$! + echo $qdrant_mcp_server_pid > $gaianet_base_dir/qdrant-mcp-server.pid + info "\n 👍 Done! Qdrant MCP server started with pid: $qdrant_mcp_server_pid" + else + error " ❌ Qdrant MCP server binary not found at $qdrant_mcp_server_executable\n\n" + + # stop + stop_force + + exit 1 + fi + fi + +} + +start_kw_search_server() { + kwsearch_server_running=false + if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + if lsof -Pi :$kwsearch_server_port -sTCP:LISTEN -t >/dev/null ; then + kwsearch_server_running=true + fi + elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then + printf "For Windows users, please run this script in WSL.\n" + + # stop + stop_force + + exit 1 + else + printf "Only support Linux, MacOS and Windows.\n" + + # stop + stop_force + + exit 1 + fi + + if [ "$kwsearch_server_running" = false ]; then + kwsearch_server_executable="$gaianet_base_dir/bin/kw-search-server" + if [ -f "$kwsearch_server_executable" ]; then + nohup $kwsearch_server_executable > $log_dir/start-kw-search-server.log 2>&1 & + sleep 2 + kwsearch_server_pid=$! + echo $kwsearch_server_pid > $gaianet_base_dir/kw-search-server.pid + info "\n 👍 Done! kw-search-server started with pid: $kwsearch_server_pid" + else + error " ❌ kw-search-server binary not found at $kwsearch_server_executable\n\n" + + # stop + stop_force + + exit 1 + fi + fi +} + +start_kw_search_mcp_server() { + kwsearch_mcp_server_running=false + if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + if lsof -Pi :$kwsearch_mcp_server_port -sTCP:LISTEN -t >/dev/null ; then + kwsearch_mcp_server_running=true + fi + elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then + printf "For Windows users, please run this script in WSL.\n" + + # stop + stop_force + + exit 1 + else + printf "Only support Linux, MacOS and Windows.\n" + + # stop + stop_force + + exit 1 + fi + + if [ "$kwsearch_mcp_server_running" = false ]; then + kwsearch_mcp_server_executable="$gaianet_base_dir/bin/gaia-kwsearch-mcp-server" + if [ -f "$kwsearch_mcp_server_executable" ]; then + nohup $kwsearch_mcp_server_executable > $log_dir/start-kwsearch-mcp-server.log 2>&1 & + sleep 5 + kwsearch_mcp_server_pid=$! + echo $kwsearch_mcp_server_pid > $gaianet_base_dir/kwsearch-mcp-server.pid + info "\n 👍 Done! kwsearch-mcp-server started with pid: $kwsearch_mcp_server_pid" + else + error " ❌ kw-search-mcp-server binary not found at $kwsearch_mcp_server_executable\n\n" + + # stop + stop_force + + exit 1 + fi + fi +} + start_gaia_nexus() { # parse domain for gaia-nexus @@ -824,6 +1039,7 @@ start_gaia_nexus() { cd $gaianet_base_dir if [ "$rag_mode" = true ]; then nohup $gaianet_base_dir/bin/gaia-nexus \ + --mcp \ --rag \ --check-health \ --check-health-interval 60 \ @@ -835,12 +1051,13 @@ start_gaia_nexus() { --vdb-collection-name $qdrant_collection_name \ --vdb-limit $qdrant_limit \ --vdb-score-threshold $qdrant_score_threshold \ - --rag-prompt "'$rag_prompt'" \ + --rag-prompt "$rag_prompt" \ --rag-context-window $context_window \ --rag-policy $rag_policy \ --port $llamaedge_port > $log_dir/start-gaia-nexus.log 2>&1 & else nohup $gaianet_base_dir/bin/gaia-nexus \ + --mcp \ --check-health \ --check-health-interval 60 \ --web-ui $gaianet_base_dir/dashboard \ @@ -995,9 +1212,13 @@ start() { cd $gaianet_base_dir wasmedge --dir .:. registry.wasm + # 2. check mcp servers + printf "[2/4] Checking the mcp servers ...\n" + check_mcp_servers $gaianet_base_dir/mcp_config.toml + info "\n 👍 Done!" - # 2. start downstream servers - printf "[2/4] Starting downstream servers ...\n\n" + # 3. start downstream servers + printf "[3/4] Starting downstream servers ...\n\n" # start chat server printf " * Start chat server ...⏳\n\n" @@ -1011,12 +1232,42 @@ start() { if [ "$rag_mode" = true ]; then printf " * Starting Qdrant instance ...⏳\n" start_qdrant_instance + + if [ "$gaia_qdrant" = false ]; then + # enable gaia_qdrant in mcp_config.toml + toggle_mcp_server $gaianet_base_dir/mcp_config.toml "gaia-qdrant" + fi + + printf " * Starting Qdrant MCP server ...⏳\n" + start_qdrant_mcp_server + + else + if [ "$gaia_qdrant" = true ]; then + # disable gaia_qdrant in mcp_config.toml + toggle_mcp_server $gaianet_base_dir/mcp_config.toml "gaia-qdrant" + fi + fi + + # start kw-search-mcp-server + if [ "$rag_mode" = true ]; then + if [ "$gaia_keyword_search" = true ]; then + printf " * Starting kw-search-server ...⏳\n" + start_kw_search_server + + printf " * Starting kw-search-mcp-server ...⏳\n" + start_kw_search_mcp_server + fi + else + if [ "$gaia_keyword_search" = true ]; then + # disable gaia_keyword_search in mcp_config.toml + toggle_mcp_server $gaianet_base_dir/mcp_config.toml "gaia-keyword-search" + fi fi - # 3. start gaia-frp + # 4. start gaia-frp if [ "$local_only" -eq 0 ]; then # start gaia-frp - printf "[3/4] Starting gaia-frp ...\n\n" + printf "[4/4] Starting gaia-frp ...\n\n" nohup $gaianet_base_dir/bin/frpc -c $gaianet_base_dir/gaia-frp/frpc.toml > $log_dir/start-gaia-frp.log 2>&1 & sleep 2 @@ -1029,16 +1280,18 @@ start() { fi - # 4. start gaia-nexus - printf "[4/4] Starting gaia-nexus ...\n\n" + # 5. start gaia-nexus + printf "[5/5] Starting gaia-nexus ...\n\n" + printf " ❗ The process may take a few seconds. Please wait ...\n\n" start_gaia_nexus # register downstream servers printf " * Register embedding server ...⏳\n\n" register_embedding_server + sleep 3 printf " * Registering chat server ...⏳\n\n" register_chat_server - + sleep 3 if [ "$local_only" -eq 0 ]; then @@ -1065,15 +1318,34 @@ stop_force() { printf "[+] Stopping WasmEdge, Qdrant and frpc ...\n" pkill -9 qdrant || true + pkill -9 gaia-qdrant-mcp-server || true + pkill -9 kw-search-server || true + pkill -9 gaia-kwsearch-mcp-server || true pkill -9 gaia-nexus || true pkill -9 wasmedge || true pkill -9 frpc || true + qdrant_pid=$gaianet_base_dir/qdrant.pid if [ -f $qdrant_pid ]; then rm $qdrant_pid fi + qdrant_mcp_server_pid=$gaianet_base_dir/qdrant-mcp-server.pid + if [ -f $qdrant_mcp_server_pid ]; then + rm $qdrant_mcp_server_pid + fi + + kw_search_server_pid=$gaianet_base_dir/kw-search-server.pid + if [ -f $kw_search_server_pid ]; then + rm $kw_search_server_pid + fi + + kw_search_mcp_server_pid=$gaianet_base_dir/kwsearch-mcp-server.pid + if [ -f $kw_search_mcp_server_pid ]; then + rm $kw_search_mcp_server_pid + fi + chat_server_pid=$gaianet_base_dir/chat_server.pid if [ -f $chat_server_pid ]; then rm $chat_server_pid diff --git a/install.sh b/install.sh index d9be109..2533b2b 100644 --- a/install.sh +++ b/install.sh @@ -8,16 +8,18 @@ target=$(uname -m) # represents the directory where the script is located cwd=$(pwd) -repo_branch="main" +repo_branch="feat-nexus-mcp-groq" version="0.5.0" -llama_api_server_version="0.17.0" -gaia_nexus_version="0.1.0" +llama_api_server_version="0.18.3" +gaia_nexus_version="0.2.1" wasmedge_version="0.14.1" ggml_bn="b5201" vector_version="0.38.0" dashboard_version="v3.1" -assistant_version="0.4.3" +# assistant_version="0.4.3" qdrant_version="v1.13.4" +mcp_version="0.1.1" +kw_search_server_version="0.1.1" # 0: do not reinstall, 1: reinstall reinstall=0 @@ -589,9 +591,8 @@ fi # 7. Download LlamaEdge API server printf "[+] Downloading LlamaEdge API server ...\n" # download llama-api-server.wasm -# check_curl https://github.com/LlamaEdge/LlamaEdge/releases/download/$llama_api_server_version/llama-api-server.wasm $gaianet_base_dir/llama-api-server.wasm +check_curl https://github.com/LlamaEdge/LlamaEdge/releases/download/$llama_api_server_version/llama-api-server.wasm $gaianet_base_dir/llama-api-server.wasm # check_curl https://github.com/GaiaNet-AI/gaia-nexus-release/releases/download/$gaia_nexus_version/llama-api-server.wasm $gaianet_base_dir/llama-api-server.wasm -check_curl https://github.com/GaiaNet-AI/gaianet-node/releases/download/$version/llama-api-server.wasm $gaianet_base_dir/llama-api-server.wasm info " 👍 Done! The llama-api-server.wasm is downloaded in $gaianet_base_dir" @@ -630,12 +631,94 @@ else fi # extract the gaia-nexus binary tar -xzf $bin_dir/gaia-nexus.tar.gz -C $bin_dir gaia-nexus +tar -xzf $bin_dir/gaia-nexus.tar.gz -C $gaianet_base_dir mcp_config.toml +# mv $bin_dir/mcp_config.toml $gaianet_base_dir/mcp_config.toml rm $bin_dir/gaia-nexus.tar.gz info " 👍 Done! The gaia-nexus is downloaded in $bin_dir" -# 9. Download dashboard to $gaianet_base_dir + +# 9 Download MCP Servers +printf "[+] Downloading MCP Servers ...\n" +if [ "$(uname)" == "Darwin" ]; then + + if [ "$target" = "x86_64" ]; then + check_curl https://github.com/apepkuss/mcp-examples/releases/download/$mcp_version/gaia-mcp-servers-apple-darwin-x86_64.tar.gz $bin_dir/gaia-mcp-servers.tar.gz + + elif [ "$target" = "arm64" ]; then + check_curl https://github.com/apepkuss/mcp-examples/releases/download/$mcp_version/gaia-mcp-servers-apple-darwin-aarch64.tar.gz $bin_dir/gaia-mcp-servers.tar.gz + + else + error " * Unsupported architecture: $target, only support x86_64 and arm64 on MacOS" + exit 1 + fi + +elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + + if [ "$target" = "x86_64" ]; then + check_curl https://github.com/apepkuss/mcp-examples/releases/download/$mcp_version/gaia-mcp-servers-unknown-linux-gnu-x86_64.tar.gz $bin_dir/gaia-mcp-servers.tar.gz + + # elif [ "$target" = "aarch64" ]; then + # check_curl https://github.com/apepkuss/mcp-examples/releases/download/$mcp_version/gaia-mcp-servers-unknown-linux-gnu-aarch64.tar.gz $bin_dir/gaia-mcp-servers.tar.gz + + else + error " * Unsupported architecture: $target, only support x86_64 on Linux" + exit 1 + fi + +else + error "Only support Linux, MacOS and Windows(WSL)." + exit 1 +fi +# extract the gaia-mcp-servers binary +tar -xzvf $bin_dir/gaia-mcp-servers.tar.gz -C $bin_dir +rm $bin_dir/gaia-mcp-servers.tar.gz + +info " 👍 Done! The MCP Servers are downloaded in $bin_dir" + + +# 10. Download kw-search-server +printf "[+] Downloading kw-search-server ...\n" +if [ "$(uname)" == "Darwin" ]; then + + if [ "$target" = "x86_64" ]; then + check_curl https://github.com/LlamaEdge/kw-search-server/releases/download/$kw_search_server_version/kw-search-server-x86_64-apple-darwin.tar.gz $bin_dir/kw-search-server.tar.gz + + elif [ "$target" = "arm64" ]; then + check_curl https://github.com/LlamaEdge/kw-search-server/releases/download/$kw_search_server_version/kw-search-server-aarch64-apple-darwin.tar.gz $bin_dir/kw-search-server.tar.gz + + else + error " * Unsupported architecture: $target, only support x86_64 and arm64 on MacOS" + exit 1 + fi + +elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + + if [ "$target" = "x86_64" ]; then + check_curl https://github.com/LlamaEdge/kw-search-server/releases/download/$kw_search_server_version/kw-search-server-x86_64-unknown-linux-gnu.tar.gz $bin_dir/kw-search-server.tar.gz + + # elif [ "$target" = "aarch64" ]; then + # check_curl https://github.com/LlamaEdge/kw-search-server/releases/download/$kw_search_server_version/kw-search-server-aarch64-unknown-linux-gnu.tar.gz $bin_dir/kw-search-server.tar.gz + + else + error " * Unsupported architecture: $target, only support x86_64 on Linux" + exit 1 + fi + +else + error "Only support Linux, MacOS and Windows(WSL)." + exit 1 +fi +# extract the gaia-mcp-servers binary +tar -xzvf $bin_dir/kw-search-server.tar.gz -C $bin_dir kw-search-server +rm $bin_dir/kw-search-server.tar.gz + +info " 👍 Done! The kw-search-server is downloaded in $bin_dir" + + + +# 11. Download dashboard to $gaianet_base_dir if ! command -v tar &> /dev/null; then echo "tar could not be found, please install it." exit 1 @@ -655,7 +738,7 @@ else warning " ❗ Use the cached dashboard in $gaianet_base_dir" fi -# 10. Download registry.wasm +# 12. Download registry.wasm if [ ! -f "$gaianet_base_dir/registry.wasm" ] || [ "$reinstall" -eq 1 ]; then printf "[+] Downloading registry.wasm ...\n" check_curl https://github.com/GaiaNet-AI/gaianet-node/raw/main/utils/registry/registry.wasm $gaianet_base_dir/registry.wasm @@ -664,7 +747,7 @@ else warning " ❗ Use the cached registry.wasm in $gaianet_base_dir" fi -# 11. Generate node ID +# 13. Generate node ID if [ "$upgrade" -eq 1 ]; then printf "[+] Recovering node ID ...\n" @@ -715,7 +798,7 @@ else info " 👍 Done!" fi -# 12. Install gaia-frp +# 14. Install gaia-frp printf "[+] Installing gaia-frp...\n" # Check if the directory exists, if not, create it if [ ! -d "$gaianet_base_dir/gaia-frp" ]; then @@ -778,7 +861,7 @@ printf " * Install frpc binary\n" cp $gaianet_base_dir/gaia-frp/frpc $gaianet_base_dir/bin/ info " 👍 Done! frpc binary is installed in $gaianet_base_dir/bin" -# 13. Download frpc.toml, generate a subdomain and print it +# 15. Download frpc.toml, generate a subdomain and print it if [ "$upgrade" -eq 1 ]; then # recover the frpc.toml if [ -f "$gaianet_base_dir/backup/frpc.toml" ]; then