Skip to content

Commit 51d564d

Browse files
committed
refactor: improve code for demo purpose
Signed-off-by: Xin Liu <sam@secondstate.io>
1 parent 28d8ad8 commit 51d564d

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

gaianet

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,53 @@ register_chat_server() {
10481048
fi
10491049
}
10501050

1051+
register_chat_server_for_demo() {
1052+
# parse port for chat server
1053+
# llamaedge_chat_port=$(awk -F'"' '/"llamaedge_chat_port":/ {print $4}' $gaianet_base_dir/config.json)
1054+
1055+
# parse port for gaia-nexus
1056+
llamaedge_port=$(awk -F'"' '/"llamaedge_port":/ {print $4}' $gaianet_base_dir/config.json)
1057+
1058+
# register chat server
1059+
local response
1060+
local status_code
1061+
1062+
# Create temporary file to store response headers
1063+
local headers_file=$(mktemp)
1064+
1065+
# Execute curl command, save response content to variable and get status code
1066+
response=$(curl -s -w "\n%{http_code}" -X POST http://localhost:$llamaedge_port/admin/servers/register \
1067+
-H "Content-Type: application/json" \
1068+
-D $headers_file \
1069+
-d '{"url": "'$DEFAULT_CHAT_SERVICE'", "kind": "chat", "api_key": "'$DEFAULT_CHAT_SERVICE_API_KEY'"}')
1070+
1071+
# Extract status code from response (last line)
1072+
status_code=$(echo "$response" | tail -n1)
1073+
# Get actual response body (excluding the last line)
1074+
response_body=$(echo "$response" | sed '$d')
1075+
1076+
# Check if status code indicates success (2xx means success)
1077+
if [[ $status_code -ge 200 && $status_code -lt 300 ]]; then
1078+
info " 👍 Successfully registered chat server"
1079+
1080+
# Delete temporary file
1081+
rm -f $headers_file
1082+
1083+
else
1084+
error " ❌ Failed to register chat server. Status code: $status_code"
1085+
error " Response: $response_body"
1086+
error " Headers: $(cat $headers_file)"
1087+
1088+
# Delete temporary file
1089+
rm -f $headers_file
1090+
1091+
# stop
1092+
stop_force
1093+
1094+
exit 1
1095+
fi
1096+
}
1097+
10511098
# start rag-api-server and a qdrant instance
10521099
start() {
10531100
local_only=$1
@@ -1082,13 +1129,14 @@ start() {
10821129
# 2. start downstream servers
10831130
printf "[2/4] Starting downstream servers ...\n\n"
10841131

1085-
# start chat server
1086-
printf " * Start chat server ...⏳\n\n"
1087-
start_chat_server
1132+
# Both environment variables are unset or empty, use normal mode
1133+
if [ -z "$DEFAULT_CHAT_SERVICE" ] && [ -z "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1134+
printf " * Start chat server ...⏳\n\n"
1135+
start_chat_server
10881136

1089-
# start embedding server
1090-
printf " * Start embedding server ...⏳\n\n"
1091-
start_embedding_server
1137+
printf " * Start embedding server ...⏳\n\n"
1138+
start_embedding_server
1139+
fi
10921140

10931141
# start Qdrant instance if rag mode is true
10941142
if [ "$rag_mode" = true ]; then
@@ -1114,17 +1162,48 @@ start() {
11141162
printf " The GaiaNet node is started in local mode at: http://localhost:$llamaedge_port\n\n"
11151163
fi
11161164

1117-
11181165
# 4. start gaia-nexus
11191166
printf "[4/4] Starting gaia-nexus ...\n"
11201167
printf " ❗ The process may take a few minutes. Please wait ...\n\n"
11211168
start_gaia_nexus
11221169

11231170
# register downstream servers
11241171
printf " * Registering chat server ...⏳\n\n"
1125-
register_chat_server
1172+
if [ -n "$DEFAULT_CHAT_SERVICE" ] && [ -n "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1173+
# Both environment variables are set and non-empty, use demo mode
1174+
register_chat_server_for_demo
1175+
elif [ -z "$DEFAULT_CHAT_SERVICE" ] && [ -z "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1176+
# Both environment variables are unset or empty, use normal mode
1177+
register_chat_server
1178+
else
1179+
# One is set but the other is not, show error message
1180+
error "Environment variable configuration error:"
1181+
if [ -z "$DEFAULT_CHAT_SERVICE" ]; then
1182+
error " DEFAULT_CHAT_SERVICE is not set or empty"
1183+
fi
1184+
if [ -z "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1185+
error " DEFAULT_CHAT_SERVICE_API_KEY is not set or empty"
1186+
fi
1187+
error "Both DEFAULT_CHAT_SERVICE and DEFAULT_CHAT_SERVICE_API_KEY must be set together, or both left unset"
1188+
exit 1
1189+
fi
1190+
11261191
printf " * Register embedding server ...⏳\n\n"
1127-
register_embedding_server
1192+
if [ -z "$DEFAULT_CHAT_SERVICE" ] && [ -z "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1193+
# Both environment variables are unset or empty, use normal mode
1194+
register_embedding_server
1195+
elif [ -z "$DEFAULT_CHAT_SERVICE" ] || [ -z "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1196+
# One is set but the other is not, show error message
1197+
error "Environment variable configuration error:"
1198+
if [ -z "$DEFAULT_CHAT_SERVICE" ]; then
1199+
error " DEFAULT_CHAT_SERVICE is not set or empty"
1200+
fi
1201+
if [ -z "$DEFAULT_CHAT_SERVICE_API_KEY" ]; then
1202+
error " DEFAULT_CHAT_SERVICE_API_KEY is not set or empty"
1203+
fi
1204+
error "Both DEFAULT_CHAT_SERVICE and DEFAULT_CHAT_SERVICE_API_KEY must be set together, or both left unset"
1205+
exit 1
1206+
fi
11281207

11291208
if [ "$local_only" -eq 0 ]; then
11301209
# Extract the subdomain from frpc.toml

0 commit comments

Comments
 (0)