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

feat: Improve ai-proxy configuration template #84

Merged
merged 1 commit into from
May 29, 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
3 changes: 2 additions & 1 deletion all-in-one/config/mcpbridges/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ spec:
name: higress-console
port: 80
type: static
# INSERTION_POINT
# AI_REGISTRIES_START
# AI_REGISTRIES_END
status: {}
8 changes: 7 additions & 1 deletion all-in-one/scripts/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ case $MODE in
;;
esac

echo "Mode=$MODE"
echo "Mode=$MODE"

CONSOLE_USED_MARKER='/data/.console-used'
CONSOLE_USED='false'
if [ -f "$CONSOLE_USED_MARKER" ]; then
CONSOLE_USED='true'
fi
217 changes: 79 additions & 138 deletions all-in-one/scripts/config-template/ai-proxy.sh
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@
#!/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:
annotations:
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
Expand Down Expand Up @@ -111,7 +116,8 @@ spec:
}

function initializeMcpBridge() {
read -r -d '' AI_REGISTRIES <<EOF
read -r -d '' AI_REGISTRIES <<EOF
# AI_REGISTRIES_START
- domain: api.moonshot.cn
name: moonshot
port: 443
Expand All @@ -128,117 +134,52 @@ function initializeMcpBridge() {
name: openai
port: 443
type: dns
# AI_REGISTRIES_END
EOF
cd /data/mcpbridges
awk -v r="$AI_REGISTRIES" '{gsub(/# INSERTION_POINT/,r)}1' default.yaml > 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 <<EOF > /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 <<EOF > /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 <<EOF > /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 <<EOF > /data/ingresses/openai.yaml
cat <<EOF > "$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
Expand Down
2 changes: 2 additions & 0 deletions all-in-one/scripts/start-console.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
Loading