OpenAI와 호환 가능한 LLM 프록시용 작은 루비 프로토타입, 재충전 가능한 토큰 버킷을 사용합니다.
이는 루비 표준 라이브러리만 사용합니다: gem 없이, 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
그것은 루비 프록시를 시작합니다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
이것은 프로토타입에 대한 근사치일 뿐입니다.












