此乃一简Ruby原型,为OpenAI兼容之LLM代理,具可补注之令牌桶。
唯用Ruby标准库:无宝石,无Rack,无WEBrick。
运行
BASE_API_URL=http://192.168.0.124:8888/v1 \ BASE_API_KEY=1mmer \ BASE_MODEL=gemma4 \ ruby llm_proxy.rb
该代理默认监听于0.0.0.0:8899。
欲为本地LLM于192.168.0.124:8888,则运行所存本地设置:
./run_local_proxy.sh
此启Ruby代理于http://127.0.0.1:8899/v1 递至 http://192.168.0.124:8888/v1.
所存本地 curl 检查如次:
./curl_local_proxy.sh
手动等价:
curl -sS -i -m 60 http://127.0.0.1:8899/v1/chat/completions \ -H 'Authorization: Bearer user-a' \ -H 'Content-Type: application/json' \ -d '{ "model": "gemma4", "messages": [{"role": "user", "content": "Reply with exactly: proxy ok"}], "max_tokens": 16 }'
经代理验证:上游应答以 proxy ok,代理返 X-RateLimit-Remaining: 0 于本地测试桶.
运烟试:
ruby test_llm_proxy.rb
令桶之设
MAX_TOKENS=10 # max saved tokens per user REFILL_TOKENS=2 # tokens added each refill REFILL_INTERVAL_SECONDS=300 # 5 minutes REQUEST_TOKEN_COST=1 # cost per accepted completion request
每持证令牌自成一篓。无持证令牌之请,则按远地IP分篓。设PROXY_API_KEYS=key1,key2若代理拒斥未知客键。
桶空之时,/v1/chat/completions且/v1/completions返常之OpenAI式助应
limit reached, wait 5 min
测试之请
curl http://localhost:8888/v1/chat/completions \ -H 'Authorization: Bearer user-a' \ -H 'Content-Type: application/json' \ -d '{ "model": "anything", "messages": [{"role": "user", "content": "hello"}] }'
可選之估計詞元模式
常情之下,一应请求需费REQUEST_TOKEN_COST桶之符。欲依提示之量与预期之果计费:
TOKEN_COST_MODE=estimate RESPONSE_TOKEN_RESERVE=256 ruby llm_proxy.rb
此乃原型之粗略估算耳。












