litellm/litellm-rust/loadtest/cmd/loadgen/body.go
narmstrong-berry 25064726cc
feat(gateway): serve /v1/decisions
Port the proxy's OpenAI-format Decisions endpoint to the Rust gateway at /v1/decisions and
/decisions, for OpenAI and Azure OpenAI deployments. Requests are validated as the proxy
validates them before hooks count them, the provider body is rebuilt from the decision fields,
and answers are aligned to the questions with refusals for what the provider left out. Box
questions follow model_info.supports_box_questions and drop_params.

Decisions run through the hooks pipeline like chat and messages: virtual keys and allowed routes,
rate limits, budgets, spend logs, prometheus and the S3 log. Cost uses the cost map's
_decisions price overrides on the decisions route.

The loadtest kit gains decisions support in mockllm, loadgen and genconfig, and a
system-one-auto workload of 2,000 input and 20 output tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FDwtmKWH21oDCJn1MFbd3
2026-11-11 15:38:40 +00:00

128 lines
4.1 KiB
Go

package main
import (
"bytes"
"fmt"
"math/rand/v2"
"strconv"
)
// bytesPerToken approximates English text under common tokenizers; only the byte count matters to
// the proxy, so this sets body size, not exact token counts.
const bytesPerToken = 4
// turnTokens is the size of each conversation turn; a large context becomes many messages, the way
// long agent sessions look, rather than one giant string.
const turnTokens = 8000
var vocabulary = []string{
"the", "of", "and", "to", "in", "is", "that", "for", "it", "as", "was", "with", "be", "by", "on", "not",
"this", "are", "or", "from", "at", "which", "but", "have", "an", "they", "you", "were", "there", "been",
"one", "all", "would", "their", "will", "more", "when", "who", "what", "about", "into", "than", "them",
"can", "only", "other", "new", "some", "could", "time", "these", "two", "may", "then", "first", "any",
"request", "latency", "gateway", "proxy", "token", "stream", "provider", "router", "deployment", "budget",
"cache", "metric", "handler", "response", "payload", "context", "window", "function", "result", "value",
}
// textPool is a block of lowercase words and spaces, so it can be copied into JSON strings without
// escaping. Bodies are slices of it at random offsets.
type textPool []byte
func newTextPool(size int, seed uint64) textPool {
r := rand.New(rand.NewPCG(seed, 7))
var b bytes.Buffer
b.Grow(size + 32)
for b.Len() < size {
b.WriteString(vocabulary[r.IntN(len(vocabulary))])
b.WriteByte(' ')
}
return textPool(b.Bytes()[:size])
}
func (p textPool) slice(r *rand.Rand, n int) []byte {
n = min(n, len(p))
off := r.IntN(len(p) - n + 1)
return p[off : off+n]
}
const maxQuestions = 128
var decisionQuestions = []string{
`{"type":"predicate","name":"q%d","instructions":"Is the customer asking for a refund?"}`,
`{"type":"choice","name":"q%d","instructions":"How urgent is this?","choices":[{"value":"low","description":"can wait a week"},{"value":"high","description":"needs action today"}]}`,
`{"type":"score","name":"q%d","instructions":"How frustrated is the customer?","levels":[{"label":"calm"},{"label":"mildly annoyed"},{"label":"angry"}]}`,
}
func buildDecisionsBody(pool textPool, r *rand.Rand, res result, outTokens, questions int, nonce string) []byte {
if questions == 0 {
questions = len(decisionQuestions)
}
text := res.CtxTokens * bytesPerToken
var b bytes.Buffer
b.Grow(text + questions*256 + 256)
b.WriteString(`{"model":`)
b.WriteString(strconv.Quote(res.Model))
b.WriteString(`,"questions":[`)
for i := range questions {
if i > 0 {
b.WriteByte(',')
}
fmt.Fprintf(&b, decisionQuestions[i%len(decisionQuestions)], i)
}
b.WriteString(`],"input":"case `)
b.WriteString(nonce)
b.WriteByte(' ')
b.Write(pool.slice(r, text))
fmt.Fprintf(&b, " [[mock ttft_ms=%d dur_ms=%d out=%d in=%d]]", res.TTFTPlanMs, res.DurPlanMs, outTokens, res.CtxTokens)
b.WriteString(`"}`)
return b.Bytes()
}
func buildBody(pool textPool, r *rand.Rand, res result, outTokens int, nonce string) []byte {
total := res.CtxTokens * bytesPerToken
var b bytes.Buffer
b.Grow(total + 1024)
b.WriteString(`{"model":`)
b.WriteString(strconv.Quote(res.Model))
b.WriteString(`,"max_tokens":`)
b.WriteString(strconv.Itoa(outTokens))
if res.Stream {
b.WriteString(`,"stream":true`)
if res.Endpoint == "chat" {
b.WriteString(`,"stream_options":{"include_usage":true}`)
}
} else {
b.WriteString(`,"stream":false`)
}
b.WriteString(`,"messages":[`)
remaining := total
role := "user"
first := true
for {
n := min(remaining, turnTokens*bytesPerToken)
if !first {
b.WriteByte(',')
}
b.WriteString(`{"role":"` + role + `","content":"`)
if first {
b.WriteString("conversation " + nonce + " ")
first = false
}
b.Write(pool.slice(r, n))
remaining -= n
if remaining <= 0 && role == "user" {
// The directive goes last so mockllm finds it by scanning only the tail.
fmt.Fprintf(&b, " [[mock ttft_ms=%d dur_ms=%d out=%d in=%d]]", res.TTFTPlanMs, res.DurPlanMs, outTokens, res.CtxTokens)
b.WriteString(`"}`)
break
}
b.WriteString(`"}`)
if role == "user" {
role = "assistant"
} else {
role = "user"
}
}
b.WriteString("]}")
return b.Bytes()
}