เกตเวย์ APISIX ที่ควบคุมโมเดลของตนให้อยู่ในงบประมาณโทเค็น
ทีมcore/period-budget
หน้า
พื้นที่
สิ่งที่ตัวอย่างนี้บิลด์
| อินสแตนซ์ | Component ของ | สิ่ง |
|---|---|---|
gateway | core/gateway/apisix | เกตเวย์assistant จากchat และ summarizer จากmini |
chat | core/ai-model/openai | gpt-4o ของ OpenAI ที่ |
mini | core/ai-model/openai | gpt-4o-mini ของ OpenAI ที่ |
เส้นuses สองmodels ของcredential โดยopenai_key ซึ่ง Release ให้budget: Aggregate เขียนtotal_tokens และgateway — ทุก
Manifest แมปapisix-chart ซึ่ง
ร่างตัวอย่าง
คุณ
เขียน
aggrega draft --example apisix-token-budgetจาก
cd apisix-token-budgetบิลด์
aggrega build --release release.yaml --auto-pullRelease ต้อง
aggrega pull --release release.yaml # resolve once, then build with no flagaggrega build --release release.yaml --auto-pull # or resolve during the buildการ.aggrega-config.yaml ระบุ
บิลด์<workspace> แทน
[… omitted: two warnings, one per model connection's credential, that the declared-sensitive origin reaches the target with no per-key sensitivity marking, because the realization maps it through a valuesMapping, the resolution's summary, a notice that core/referenced-secret declares origin in both of its pillars, and a total build time, which differs per run]Build completed: 8 files written to <workspace>/distกับผู้ช่วย AI
ผู้
ใช้เครื่องมือของ Aggrega เท่านั้น และอย่าสร้างโฟลเดอร์เอง หากโฟลเดอร์ของพื้นที่ทำงานยังไม่มีโฟลเดอร์ `apisix-token-budget` เรียกเครื่องมือ `draft` หนึ่งครั้งโดยตั้ง `example` เป็น `apisix-token-budget` และไม่ระบุ `dir`: เครื่องมือจะเขียนตัวอย่างลงในโฟลเดอร์ `apisix-token-budget` ใหม่ จากนั้นเรียกเครื่องมือ `build` โดยตั้ง `workspace_root` เป็น `apisix-token-budget` ตั้ง `release` เป็น `release.yaml` ตั้ง `auto_pull` เป็น true และตั้ง `write` เป็น true แล้วบอกฉันว่าบิลด์เขียนไฟล์ใดบ้าง ไฟล์เหล่านั้นอยู่ใต้ `apisix-token-budget/dist/`ทรีที่เรนเดอร์
dist/helm/├── chat-service.yaml├── gateway--assistant-zdcredential--gateway-secret-gateway-assistant-credential.yaml├── gateway--summarizer-zdcredential--gateway-secret-gateway-summarizer-credential.yaml├── gateway-configmap-default-gateway.yaml├── gateway-deployment-default-gateway.yaml├── gateway-service-default-gateway-gateway.yaml├── mini-service.yaml└── gateway/ └── model-routes.yamlงบdist/helm/gateway/model-routes.yaml ซึ่ง
งบประมาณตามที่เกตเวย์อ่าน
นี่apisix.yaml ของ ConfigMap นั้น:
services: - id: "model-budget" plugins: ai-rate-limiting: limit: 2000000 time_window: 86400 limit_strategy: "total_tokens"routes: - id: "model-assistant-chat" uri: "/assistant/v1/chat/completions" service_id: "model-budget" methods: - POST plugins: ai-proxy: provider: openai-compatible auth: header: Authorization: "$env://AI_PROXY_ASSISTANT_AUTHORIZATION" options: model: "gpt-4o" override: endpoint: "https://api.openai.com/v1/chat/completions" - id: "model-summarizer-chat" uri: "/summarizer/v1/chat/completions" service_id: "model-budget" [… omitted: the same route for gpt-4o-mini, presenting AI_PROXY_SUMMARIZER_AUTHORIZATION]#ENDมี
งบai-rate-limiting เก็บmodel-budget ซึ่งservice_id จะgateway ที่scope: model Recipe จะmodel-assistant-budget และ model-summarizer-budget แต่ละ
ช่วง1d เขียน86400 หน้าต่าง1mo ดัง
หน่วยtotal_tokens คือprompt_tokens และ completion_tokens นับ
แต่ละ/<alias>/v1/completions ด้วย APISIX อ่านprompt แทนที่messages ไม่ใช่404 Route Not Found
ไม่มี$env:// ของ APISIX จาก dist/helm/gateway-deployment-default-gateway.yaml นี่env ของ
env: - name: AI_PROXY_ASSISTANT_API_KEY valueFrom: secretKeyRef: key: api-key name: gateway-assistant.credential - name: AI_PROXY_ASSISTANT_AUTHORIZATION value: Bearer $(AI_PROXY_ASSISTANT_API_KEY) [… omitted: the same two variables for summarizer]Deployment เดียวกันreplicas: 1 การ
สิ่งที่เกตเวย์ทำขณะรัน
ในlimit เมื่อ503 Service Temporarily Unavailable จนกว่าX-AI-RateLimit-Limit-…, X-AI-RateLimit-Remaining-… และ X-AI-RateLimit-Reset-… ของ
ผล
- การ
รีสตาร์ต การเริ่ม หน้าต่าง ใหม่ นับ อยู่ ใน หน่วย ความ จำ ของ กระบวนการ เกตเวย์ pod ที่ ถูก หมุนเวียน ใหม่ หรือ รีสตาร์ต จึง เริ่ม นับ จาก ศูนย์ - การ
ตอบ การกลับ ที่ ไม่ รายงาน โทเค็น ไม่ นับ สิ่ง ใด นับ คือ สิ่ง ที่ แต่ละ โมเดล รายงาน เกตเวย์ ขอ ให้ คำขอ แชต แบบ สตรีม รายงาน การ ใช้ งาน และ โมเดล ที่ ไม่ รายงาน จะ ไม่ เพิ่ม สิ่ง ใด
อะไรที่ทำให้บิลด์หยุด
แต่ละ
-
ต้นทุน
เป็น ตั้งดอลลาร์ สหรัฐ unit: usdในการ แนบ แล้ว บิลด์ จะ หยุด ก่อน ที่ จะ เรนเดอร์ สิ่ง ใด: [… omitted: what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0033]Recipe/FeatureRecipe assertionscaused by: FeatureRecipe "period-budget-apisix" instance "gateway" assertion "unit_is_a_token_count": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: core/period-budget attachment "budget" on "gateway" asks for unit usd, but core/gateway/apisix, which declares dataplane.aggrega.ai/period-budget: tokens, counts the tokens each model's reply reports and prices nothing, so it keeps no budget in US dollars; set unit to total_tokens, prompt_tokens or completion_tokens, or attach the Feature to a gateway that counts cost -
งบ
ประมาณ คือต่อ ผู้ เรียก scope: callerขณะที่ Release ไม่ ประกาศ ผู้ เรียก เกตเวย์ แยกแยะ ผู้ เรียก จาก คีย์ ที่ แต่ละ ราย ส่ง มา และ เมื่อ ไม่มี infra.callersทุกเส้น ทาง เปิด อยู่ เมื่อ ประกาศ ผู้ เรียก แล้ว ดัง ใน หัวข้อ ถัด ไป แต่ละ ราย มี การ นับ ของ ตนเอง ข้าม ทุก เส้น ทาง -
เดือน
ปฏิทิน คือperiod: 1moและ ขีดจำกัด ที่ ไม่ใช่ จำนวนเต็ม ของ โทเค็น -
การ
แนบ core/period-budgetครั้งที่ ซึ่งสอง บน เกตเวย์ รัน การ จำกัด อัตรา โทเค็น หนึ่ง รายการ บน เส้น ทาง -
ไม่มี
สิ่ง ใด เชื่อม กับ ซ็อกเก็ต modelsซึ่งทำให้ ไม่มี เส้น ทาง โมเดล ให้ นับ ลบ การ เชื่อม ต่อ ทั้ง สอง รายการ เพียง อย่าง เดียว แล้ว Component ของ เกตเวย์ เอง จะ หยุด ก่อน โดย ขอ ที่ จัด เก็บ etcd และ คีย์ Admin API ที่ เกตเวย์ ซึ่ง ไม่มี โมเดล ใช้ เก็บ เส้น ทาง ของ ตน -
realization อีก
แบบ แมปcore/gateway/apisixไปยัง helm/apisixใน Manifest แล้วบิลด์ จะ หยุด ที่ การ เชื่อม ต่อ modelsโดยระบุ helm/apisix-chart: realization นั้นรัน APISIX 3.17.0 โดย เก็บ เส้น ทาง ไว้ ใน etcd และ ไม่ เรนเดอร์ เส้น ทาง ใด เลย และ ยัง หยุด ที่ การ แนบ งบ ประมาณ ด้วย เพราะ ไม่มี เส้น ทาง ให้ เขียน งบ ประมาณ ลง ไป และ ที่ 3.17.0 แต่ละ กระบวนการ เกตเวย์ นับ ด้วย ตนเอง
แคชเชิงความหมายต้องมีผู้เรียก
APISIX 3.18.0 มีcore/semantic-caching แนบfeatures ของaggregate.yaml:
answers: type: core/semantic-caching values: embedding_model: assistant similarity_threshold: 0.9 ttl_seconds: 3600Manifest แมป Feature ไป
[… omitted: what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions caused by: FeatureRecipe "semantic-caching-apisix" instance "gateway" assertion "scope_is_one_the_gateway_keeps": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: core/semantic-caching attachment "answers" on "gateway" asks for scope end_user, which keeps each end user the gateway identifies apart, but core/gateway/apisix tells its callers apart only by the key each presents, and cannot tell apart the end users behind one key: a value a request names for one is whatever the caller sent, which the gateway does not verify. Set scope to key, which keeps each caller infra.callers declares apart, or attach the Feature to a gateway that identifies end usersแคชscope: key แล้ว
[… omitted: what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions caused by: FeatureRecipe "semantic-caching-apisix" instance "gateway" assertion "callers_are_declared": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: core/semantic-caching attachment "answers" on "gateway" asks for scope key, but infra.callers of "gateway" declares no caller, so the gateway authenticates no key and every request on a route would share one cache, a reply generated for one caller served to another; declare the callers in infra.callers, with their keys through the caller_keys slotการ
gateway: infra: callers: [support-agent, billing-agent] features: caller_keys: type: core/referenced-secret-set infra: entries: - key: support-agent value: support-agent-key-change-me - key: billing-agent value: billing-agent-key-change-me budget: [… omitted: the budget's size, as above]Aggregate เชื่อมembedding_model: embedder พร้อม scope: key:
models: [… omitted: assistant and summarizer, as above] embedder: target: embeddings features: credential: type: core/referenced-secret values: key: api-key origin: "{{ .parameters.openai_key }}" cache_store: replies: target: cacheพร้อมembeddings (core/ai-model/openai-embedding โดยvalues.model เป็น text-embedding-3-small) และcache (core/datastore/redis) ที่core/referenced-secret-set ไปhelm/k8s-secret-set ซึ่ง
consumers: - username: "support-agent" plugins: key-auth: key: "$env://CALLER_SUPPORT_AGENT_AUTHORIZATION" [… omitted: billing-agent, the same way]routes: - id: "model-assistant-chat" uri: "/assistant/v1/chat/completions" service_id: "model-budget" methods: - POST plugins: key-auth: header: Authorization hide_credentials: true ai-proxy: [… omitted: the route to gpt-4o, as above] ai-cache: layers: - exact - semantic exact: ttl: 3600 cache_key: share_across_routes: false include_consumer: true policy: redis redis_host: "cache-default" redis_port: 6379 semantic: similarity_threshold: 0.9 ttl: 3600 embedding: openai: endpoint: "https://api.openai.com/v1/embeddings" model: "text-embedding-3-small" api_key: "$env://AI_PROXY_EMBEDDER_API_KEY" vector_search: redis: index: "gateway-answers" [… omitted: the same for the other connections]ไม่มีcaller_keys เรนเดอร์ ภาย
- name: CALLER_SUPPORT_AGENT_KEY valueFrom: secretKeyRef: key: support-agent name: gateway-caller-keys - name: CALLER_SUPPORT_AGENT_AUTHORIZATION value: Bearer $(CALLER_SUPPORT_AGENT_KEY) [… omitted: the same two variables for billing-agent]ขณะAuthorization: Bearer <key> จะembedder ได้models
ตำแหน่งที่คอมไพเลอร์ไม่ตีความ
คอมไพเลอร์
การปรับแต่งตัวอย่างนี้
- ขีด
จำกัด ตั้งต่อ โมเดล scope: model: แต่ละ alias ได้บริการ และ การ นับ ของ ตนเอง - ขีด
จำกัด ประกาศต่อ ผู้ เรียก ผู้ เรียก และ คีย์ ของ แต่ละ ราย ใน Release ดัง ใน “แคช เชิง ความ หมาย ต้อง มี ผู้ เรียก” ข้าง ต้น และ ตั้ง scope: caller: ผู้เรียก แต่ละ ราย ได้การ นับ ของ ตนเอง ข้าม ทุก เส้น ทาง - นับ
ด้าน ตั้งเดียว unit: prompt_tokensเพื่อนับ สิ่ง ที่ โมเดล อ่าน หรือ completion_tokensสำหรับสิ่ง ที่ โมเดล เขียน - หน้าต่าง
ที่ ตั้งสั้น หรือ ยาว ขึ้น periodเป็นจำนวน วินาที นาที ชั่วโมง หรือ วัน ใน Release เช่น 6hหรือ30d