全部產品
Search
文件中心

Alibaba Cloud Service Mesh:外部客戶端通過ASM入口網關訪問LLM服務

更新時間:Dec 27, 2024

ASM支援通過入口網關訪問外部LLM服務。在請求鏈路中引入ASM網關之後,您可以使用ASM網關提供的諸多能力,比如流量按比例分流、請求可觀測以及豐富的認證、授權能力。本文主要介紹叢集外用戶端如何通過ASM入口網關訪問外部LLM服務。

功能概述

通過入口網關訪問外部LLM服務主要適用於叢集外用戶端訪問LLM服務的情境,ASM網關提供了諸多的路由、安全以及可觀測的能力,並且支援LLM流量管理。通過ASM網關,您可以快速、安全地對接外部LLM服務。

本文樣本的請求鏈路如下所示:

前提條件

步驟一:建立LLMProvider

  1. 使用以下內容,建立如下LLMProvider.yaml。

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: LLMProvider
    metadata:  
      name: dashscope-qwen
      namespace: istio-system
    spec:
      workloadSelector:
        labels:
          istio: ingressgateway
      host: dashscope.aliyuncs.com
      path: /compatible-mode/v1/chat/completions
      configs:
        defaultConfig:
          openAIConfig:
            model: qwen1.5-72b-chat  # 千問開源系列大模型
            stream: false
            apiKey: ${API_KEY}
  2. 使用ASM叢集kubeconfig,執行以下命令,建立LLMProvider。

    kubectl apply -f LLMProvider.yaml

步驟二:建立網關規則

  1. 使用以下內容,建立如下網關規則ingress-gw.yaml。

    apiVersion: networking.istio.io/v1beta1
    kind: Gateway
    metadata:
      name: ingress-gw
      namespace: istio-system
    spec:
      selector:
        istio: ingressgateway
      servers:
        - hosts:
            - '*'
          port:
            name: http
            number: 80
            protocol: HTTP
  2. 執行以下命令,建立網關規則。

    kubectl apply -f ingress-gw.yaml

步驟三:建立LLMRoute

  1. 使用以下內容,建立dashscope-route.yaml。

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: LLMRoute
    metadata:  
      name: dashscope-route
    spec:
      host: "*"
      gateways:
      - istio-system/ingress-gw
      rules:
      - name: ingress-route
        matches:
        - headers:
            host:
              exact: dashscope.aliyuncs.com  # 處理dashscope.aliyuncs.com的路由,否則會返回404。
        - headers:
            host:
              exact: test.com # 處理test.com的請求。請求被ASM LLM外掛程式處理後,會重新觸發路由匹配,進入上面的匹配條件。
        backendRefs:
        - providerHost: dashscope.aliyuncs.com
  2. 執行以下命令,建立LLMRoute。

    kubectl apply -f dashscope-route.yaml

步驟四:測試

在本地終端執行以下命令進行測試。

curl --location '${ASM網關IP}:80' \
--header 'Content-Type: application/json' \
--header "host: test.com" \
--data '{
    "messages": [
        {"role": "user", "content": "請介紹你自己"}
    ]
}'

預期輸出:

{"choices":[{"message":{"role":"assistant","content":"我是來自阿里雲的大規模語言模型,我叫通義千問。我的主要功能是回答使用者的問題、提供資訊和進行對話交流。我可以理解使用者的提問,並基於自然語言產生相應的答案或建議。我也可以學習新的知識,並將其應用於各種情境中。如果您有任何問題或需要協助,請隨時告訴我,我會儘力為您提供支援。"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":3,"completion_tokens":72,"total_tokens":75},"created":1720682745,"system_fingerprint":null,"model":"qwen1.5-72b-chat","id":"chatcmpl-3d117bd7-9bfb-9121-9fc2-xxxxxxxxxxxx"}

可以看到請求鏈路已經打通。

步驟五:使用ASM網關安全能力

本步驟將建立一個簡單的授權策略,拒絕本機IP通過ASM網關訪問LLM服務。

  1. 使用以下內容建立auth-policy.yaml。

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      labels:
        gateway: ingressgateway
      name: auth-policy
      namespace: istio-system
    spec:
      action: DENY
      rules:
        - from:
            - source:
                ipBlocks:
                  - ${本機IP}
          to:
            - operation:
                hosts:
                  - test.com
      selector:
        matchLabels:
          istio: ingressgateway
  2. 執行以下命令,部署授權策略。

    kubectl apply -f auth-policy.yaml
  3. 再次執行步驟四中的測試命令進行測試,可以看到如下結果:

    RBAC: access denied
說明

ASM網關針對普通HTTP請求所具備的安全能力同樣適用於LLM請求,包括完整的授權策略、JWT認證和自訂授權服務等。通過在出口網關上應用這些策略,可以更有效地保障您的應用安全。