From 188cbf3a6597c90f9fe7705e9f827ed8a392735b Mon Sep 17 00:00:00 2001 From: CH3CHO Date: Wed, 29 May 2024 12:26:23 +0800 Subject: [PATCH] feat: Improve ai-proxy configuration template 1. Always reset ai-proxy configurations if Higress Console has never been started. 2. Mark ai-proxy as a built-in plugin. --- all-in-one/config/mcpbridges/default.yaml | 3 +- all-in-one/scripts/base.sh | 8 +- .../scripts/config-template/ai-proxy.sh | 217 +++++++----------- all-in-one/scripts/start-console.sh | 2 + 4 files changed, 90 insertions(+), 140 deletions(-) diff --git a/all-in-one/config/mcpbridges/default.yaml b/all-in-one/config/mcpbridges/default.yaml index 7d8b47d..b655452 100644 --- a/all-in-one/config/mcpbridges/default.yaml +++ b/all-in-one/config/mcpbridges/default.yaml @@ -11,5 +11,6 @@ spec: name: higress-console port: 80 type: static - # INSERTION_POINT + # AI_REGISTRIES_START + # AI_REGISTRIES_END status: {} diff --git a/all-in-one/scripts/base.sh b/all-in-one/scripts/base.sh index 8cc0c07..27de79b 100644 --- a/all-in-one/scripts/base.sh +++ b/all-in-one/scripts/base.sh @@ -40,4 +40,10 @@ case $MODE in ;; esac -echo "Mode=$MODE" \ No newline at end of file +echo "Mode=$MODE" + +CONSOLE_USED_MARKER='/data/.console-used' +CONSOLE_USED='false' +if [ -f "$CONSOLE_USED_MARKER" ]; then + CONSOLE_USED='true' +fi \ No newline at end of file diff --git a/all-in-one/scripts/config-template/ai-proxy.sh b/all-in-one/scripts/config-template/ai-proxy.sh index 399a961..59e4c11 100644 --- a/all-in-one/scripts/config-template/ai-proxy.sh +++ b/all-in-one/scripts/config-template/ai-proxy.sh @@ -1,54 +1,59 @@ #!/bin/bash +cd "$(dirname -- "$0")" +ROOT=$(pwd) +cd - >/dev/null +source $ROOT/base.sh + AZ_PROXY_VERSION=${AI_PROXY_VERSION:-1.0.0} if [ -n "$AZURE_OPENAI_SERVICE_URL" ]; then - AZURE_OPENAI_SERVICE_DOMAIN=$(echo "$AZURE_OPENAI_SERVICE_URL" | awk -F[/:] '{print $4}') + AZURE_OPENAI_SERVICE_DOMAIN=$(echo "$AZURE_OPENAI_SERVICE_URL" | awk -F[/:] '{print $4}') else - AZURE_OPENAI_SERVICE_DOMAIN="YOUR_RESOURCE_NAME.openai.azure.com" + AZURE_OPENAI_SERVICE_DOMAIN="YOUR_RESOURCE_NAME.openai.azure.com" fi function initializeWasmPlugins() { - mkdir -p /data/wasmplugins - WASM_PLUGIN_CONFIG_FILE="/data/wasmplugins/ai-proxy-$AZ_PROXY_VERSION.yaml" - - if [ -f "$WASM_PLUGIN_CONFIG_FILE" ]; then - return - fi - - DASHSCOPE_API_KEY=${DASHSCOPE_API_KEY:-YOUR_DASHSCOPE_API_KEY} - DASHSCOPE_API_KEYS=(${DASHSCOPE_API_KEY//,/ }) - DASHSCOPE_API_KEY_CONFIG="" - for key in "${DASHSCOPE_API_KEYS[@]}" - do - DASHSCOPE_API_KEY_CONFIG="${DASHSCOPE_API_KEY_CONFIG}\n - \"${key}\"" - done - - AZURE_OPENAI_API_KEY=${AZURE_OPENAI_API_KEY:-YOUR_AZURE_OPENAI_API_KEY} - AZURE_OPENAI_API_KEYS=(${AZURE_OPENAI_API_KEY//,/ }) - AZURE_OPENAI_API_KEY_CONFIG="" - for key in "${AZURE_OPENAI_API_KEYS[@]}" - do - AZURE_OPENAI_API_KEY_CONFIG="${AZURE_OPENAI_API_KEY_CONFIG}\n - \"${key}\"" - done - - OPENAI_API_KEY=${OPENAI_API_KEY:-YOUR_OPENAI_API_KEY} - OPENAI_API_KEYS=(${OPENAI_API_KEY//,/ }) - OPENAI_API_KEY_CONFIG="" - for key in "${OPENAI_API_KEYS[@]}" - do - OPENAI_API_KEY_CONFIG="${OPENAI_API_KEY_CONFIG}\n - \"${key}\"" - done - - MOONSHOT_API_KEY=${MOONSHOT_API_KEY:-YOUR_MOONSHOT_API_KEY} - MOONSHOT_API_KEYS=(${MOONSHOT_API_KEY//,/ }) - MOONSHOT_API_KEY_CONFIG="" - for key in "${MOONSHOT_API_KEYS[@]}" - do - MOONSHOT_API_KEY_CONFIG="${MOONSHOT_API_KEY_CONFIG}\n - \"${key}\"" - done - - echo -e "\ + mkdir -p /data/wasmplugins + WASM_PLUGIN_CONFIG_FILE="/data/wasmplugins/ai-proxy-$AZ_PROXY_VERSION.yaml" + + if [ "$CONSOLE_USED" == 'true' -a -f "$WASM_PLUGIN_CONFIG_FILE" ]; then + return + fi + + DASHSCOPE_API_KEY=${DASHSCOPE_API_KEY:-YOUR_DASHSCOPE_API_KEY} + DASHSCOPE_API_KEYS=(${DASHSCOPE_API_KEY//,/ }) + DASHSCOPE_API_KEY_CONFIG="" + for key in "${DASHSCOPE_API_KEYS[@]}" + do + DASHSCOPE_API_KEY_CONFIG="${DASHSCOPE_API_KEY_CONFIG}\n - \"${key}\"" + done + + AZURE_OPENAI_API_KEY=${AZURE_OPENAI_API_KEY:-YOUR_AZURE_OPENAI_API_KEY} + AZURE_OPENAI_API_KEYS=(${AZURE_OPENAI_API_KEY//,/ }) + AZURE_OPENAI_API_KEY_CONFIG="" + for key in "${AZURE_OPENAI_API_KEYS[@]}" + do + AZURE_OPENAI_API_KEY_CONFIG="${AZURE_OPENAI_API_KEY_CONFIG}\n - \"${key}\"" + done + + OPENAI_API_KEY=${OPENAI_API_KEY:-YOUR_OPENAI_API_KEY} + OPENAI_API_KEYS=(${OPENAI_API_KEY//,/ }) + OPENAI_API_KEY_CONFIG="" + for key in "${OPENAI_API_KEYS[@]}" + do + OPENAI_API_KEY_CONFIG="${OPENAI_API_KEY_CONFIG}\n - \"${key}\"" + done + + MOONSHOT_API_KEY=${MOONSHOT_API_KEY:-YOUR_MOONSHOT_API_KEY} + MOONSHOT_API_KEYS=(${MOONSHOT_API_KEY//,/ }) + MOONSHOT_API_KEY_CONFIG="" + for key in "${MOONSHOT_API_KEYS[@]}" + do + MOONSHOT_API_KEY_CONFIG="${MOONSHOT_API_KEY_CONFIG}\n - \"${key}\"" + done + + echo -e "\ apiVersion: extensions.higress.io/v1alpha1 kind: WasmPlugin metadata: @@ -56,7 +61,7 @@ metadata: higress.io/wasm-plugin-title: AI Proxy labels: higress.io/resource-definer: higress - higress.io/wasm-plugin-built-in: \"false\" + higress.io/wasm-plugin-built-in: \"true\" higress.io/wasm-plugin-category: custom higress.io/wasm-plugin-name: ai-proxy higress.io/wasm-plugin-version: $AZ_PROXY_VERSION @@ -111,7 +116,8 @@ spec: } function initializeMcpBridge() { - read -r -d '' AI_REGISTRIES < default-new.yaml - mv default-new.yaml default.yaml - cd - + + cd /data/mcpbridges + + if [ "$CONSOLE_USED" == 'true' -a -f "./default.yaml" ]; then + return + fi + + awk -v r="$AI_REGISTRIES" '{gsub(/# AI_REGISTRIES_START.*# AI_REGISTRIES_END/,r)}1' default.yaml > default-new.yaml + mv default-new.yaml default.yaml + cd - } function initializeIngresses() { - mkdir -p /data/ingresses + mkdir -p /data/ingresses - cat < /data/ingresses/qwen.yaml -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - annotations: - higress.io/backend-protocol: HTTPS - higress.io/destination: qwen.dns - $([ "$DEFAULT_AI_SERVICE" == "qwen" ] && echo -n "disabled." || echo -n "")higress.io/exact-match-header-Authorization: Bearer qwen - higress.io/ignore-path-case: "false" - higress.io/proxy-ssl-name: dashscope.aliyuncs.com - higress.io/proxy-ssl-server-name: "on" - labels: - higress.io/resource-definer: higress - name: qwen - namespace: higress-system -spec: - ingressClassName: higress - rules: - - http: - paths: - - backend: - resource: - apiGroup: networking.higress.io - kind: McpBridge - name: default - path: / - pathType: Prefix -EOF + generateAiIngress "moonshot" "api.moonshot.cn" + generateAiIngress "qwen" "dashscope.aliyuncs.com" + generateAiIngress "azure-openai" "$AZURE_OPENAI_SERVICE_DOMAIN" + generateAiIngress "openai" "api.openai.com" +} - cat < /data/ingresses/moonshot.yaml -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - annotations: - higress.io/backend-protocol: HTTPS - higress.io/destination: moonshot.dns - $([ "$DEFAULT_AI_SERVICE" == "moonshot" ] && echo -n "disabled." || echo -n "")higress.io/exact-match-header-Authorization: Bearer moonshot - higress.io/ignore-path-case: "false" - higress.io/proxy-ssl-name: api.moonshot.cn - higress.io/proxy-ssl-server-name: "on" - labels: - higress.io/resource-definer: higress - name: moonshot - namespace: higress-system -spec: - ingressClassName: higress - rules: - - http: - paths: - - backend: - resource: - apiGroup: networking.higress.io - kind: McpBridge - name: default - path: / - pathType: Prefix -EOF +function generateAiIngress() { + PROVIDER_NAME="$1" + SERVICE_DOMAIN="$2" - cat < /data/ingresses/azure-openai.yaml -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - annotations: - higress.io/backend-protocol: HTTPS - higress.io/destination: azure-openai.dns - $([ "$DEFAULT_AI_SERVICE" == "azure-openai" ] && echo -n "disabled." || echo -n "")higress.io/exact-match-header-Authorization: Bearer azure-openai - higress.io/ignore-path-case: "false" - higress.io/proxy-ssl-name: $AZURE_OPENAI_SERVICE_DOMAIN - higress.io/proxy-ssl-server-name: "on" - labels: - higress.io/resource-definer: higress - name: azure-openai - namespace: higress-system -spec: - ingressClassName: higress - rules: - - http: - paths: - - backend: - resource: - apiGroup: networking.higress.io - kind: McpBridge - name: default - path: / - pathType: Prefix -EOF + INGRESS_FILE="/data/ingresses/$PROVIDER_NAME.xml" + if [ "$CONSOLE_USED" == 'true' -a -f "$INGRESS_FILE" ]; then + return + fi - cat < /data/ingresses/openai.yaml + cat < "$INGRESS_FILE" apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: higress.io/backend-protocol: HTTPS - higress.io/destination: openai.dns - $([ "$DEFAULT_AI_SERVICE" == "openai" ] && echo -n "disabled." || echo -n "")higress.io/exact-match-header-Authorization: Bearer openai + higress.io/destination: $PROVIDER_NAME.dns + $([ "$DEFAULT_AI_SERVICE" == "$PROVIDER_NAME" ] && echo -n "disabled." || echo -n "")higress.io/exact-match-header-Authorization: Bearer $PROVIDER_NAME higress.io/ignore-path-case: "false" - higress.io/proxy-ssl-name: api.openai.com + higress.io/proxy-ssl-name: $SERVICE_DOMAIN higress.io/proxy-ssl-server-name: "on" labels: higress.io/resource-definer: higress - name: openai + name: moonshot namespace: higress-system spec: ingressClassName: higress diff --git a/all-in-one/scripts/start-console.sh b/all-in-one/scripts/start-console.sh index 6e4d024..6d346fb 100644 --- a/all-in-one/scripts/start-console.sh +++ b/all-in-one/scripts/start-console.sh @@ -20,6 +20,8 @@ echo "CONSOLE_PORT=$CONSOLE_PORT" waitForApiServer waitForController +touch "$CONSOLE_USED_MARKER" + set -e HIGRESS_CONSOLE_KUBE_CONFIG="/app/kubeconfig" SERVER_PORT="$CONSOLE_PORT" \