FastAPI gateway · Groq Llama 3.1 8B / 3.3 70B · Ollama fallback
Trim the context. Reuse the answer. Route the rest.
Semantic Gateway sits in front of Groq-hosted Llama models. Before a call, it splits long context into blocks, scores each block against the question with keyword rules, and drops the low scorers. Repeat questions are answered from a semantic cache, and each remaining prompt goes to Llama 3.1 8B or Llama 3.3 70B depending on what it contains.
# From a clone of the repo $ pip install -r requirements.txt $ uvicorn main:app --port 8000 # Model replies need a Groq key in .env: # GROQ_API_KEY=<your key> # Without one you get a placeholder reply; # trimming, cache and routing still run.
$ curl -X POST http://localhost:8000/v1/compress \ -H "Content-Type: application/json" \ -d '{"context": "## Deploy log\n2026-01-01 10:00:00 INFO health probe ok\n2026-01-01 10:00:05 INFO health probe ok\n\n## Refund policy\nRefunds above $500 need supervisor approval.", "query": "What is the refund policy?"}' # Response, some fields left out { "compressed_text": "## Refund policy\nRefunds above $500 need supervisor approval.", "original_tokens": 39, "kept_tokens": 15 } # kept_blocks and dropped_blocks list every block with a reason
$ curl -i -X POST http://localhost:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{"messages": [{"role": "user", "content": "What is the capital of France?"}]}' # The body has the usual chat completion fields plus # compression, cache, routing, cost, latency and provider. # Send it twice: the X-Cache-Lookup header goes MISS, then HIT.
# Not a published package. Run this from a clone of the repo. from services.compression import compress_for_turn res = compress_for_turn(context=long_text, user_query="What is the refund policy?") print(res["original_tokens"], "->", res["kept_tokens"]) print(res["compressed_text"])
This is the README example sent to /v1/compress: a short context with two sections, where one section answers the question and the other is a repeated log line. It is a single input, not a benchmark. The repo has no benchmark script and no measured average.
Token counts everywhere on this page are estimates (max(words × 1.3, chars ÷ 4)), not output from a real tokenizer. For numbers on your own text, use the playground below.
// How it works
What happens to a request
The steps POST /v1/chat/completions runs, in order, with the file each one lives in. Trimming is done by rules, with no model call.
-
services/compression.py
Drop repeated messages
Messages in the history that repeat an earlier message word for word are removed. The latest message is always kept.
-
services/compression.py
Remove duplicate lines
Repeated lines are dropped. Timestamps and log levels are ignored when comparing, so the same log line at two different times counts as a duplicate. Lines inside code fences are left alone.
-
services/compression.py
Split the context into blocks
The text is cut at markdown headings, code fences, runs of log lines, and paragraph breaks in long passages.
-
services/compression.py
Score each block against the question
The score is the share of the question's keywords found in the block (weight 0.7), plus 0.25 if the block's heading contains one, plus 0.35 for error text when the question is about a failure, minus 0.4 for log blocks with little overlap.
-
services/compression.py
Keep the blocks at or above the cutoff
Blocks that score 0.30 or more are kept and the rest are dropped. At least one block is always kept. The response lists every kept and dropped block with its reason.
-
services/cache.py
Look in the cache
The trimmed text is checked for an exact match first. If there is none, it is embedded with
hit → return the stored reply, skip steps 07 and 08all-MiniLM-L6-v2and compared with stored prompts by cosine similarity. The default threshold is 0.82 (CACHE_SIMILARITY_THRESHOLD). -
services/llm.py
Pick the model
Prompts that contain code markers, are longer than 250 characters, or include a keyword such as
debugorarchitecturego tollama-3.3-70b-versatile. Everything else goes tollama-3.1-8b-instant. -
services/llm.py
Call Groq, with fallbacks
Groq is called first. If that fails, an Ollama server is tried. If both fail, the gateway returns a canned placeholder so the request still completes, and marks it with
provider.simulated: true. The reply is then stored in the cache and the request is counted in the metrics.
Steps 02 to 05 are also available on their own as POST /v1/compress. If a request carries a long system message, that message is what gets trimmed.
// Try it
Playground
Send a context and a question through this server. Every number in this section is read from this server's API responses.
Requests are logged. The Request log tab shows recent prompts to anyone who opens this page, so do not paste secrets or personal data.
Original context · 0 est. tokens
After trimming · 0 est. tokens
Kept blocks (0)
Dropped blocks (0)
| Time | Prompt | Tier | Model | Cache | Est. tokens (in → sent) | Latency | Est. cost diff. |
|---|---|---|---|---|---|---|---|
| Loading requests… | |||||||