|
| 1 | +# Streaming Hints |
| 2 | + |
| 3 | +Injects hints from a memory bank into the model's reasoning while it is being generated, inspired by [this idea from Will Brown](https://x.com/willccbb/status/1940557166248972387). Instead of putting every piece of guidance in the prompt up front, the plugin watches the reasoning and adds one targeted hint at the moment the model hesitates or moves to a new step. |
| 4 | + |
| 5 | +This is the inference half of the approach. The retriever is rule-based for now; training the injection policy with RL is future work (see below). |
| 6 | + |
| 7 | +## How it works |
| 8 | + |
| 9 | +1. The model answers the query in segments of at most `segment_tokens` tokens. |
| 10 | +2. Each segment is fed sentence by sentence to the `StreamingHintRetriever`. |
| 11 | +3. A sentence is an **injection point** when at least `min_gap_chars` characters have passed since the last hint and the sentence contains an uncertainty signal ("wait", "hmm", "not sure", "let me check", ...) or ends a paragraph. |
| 12 | +4. At an injection point the retriever queries the `HintMemoryBank` with the problem plus the most recent reasoning. If the best unused hint scores at least `hint_threshold`, the segment is cut after that sentence and the hint is appended as `<hint>...</hint>`. |
| 13 | +5. Generation continues from the cut, with the reasoning so far (hints included) in the prompt. This repeats until the model finishes, `max_segments` is reached, or `max_tokens` is spent. |
| 14 | + |
| 15 | +Segments are used instead of a token stream so the plugin works with every client optillm supports, including the local inference server, which does not stream. The cost is that up to one segment of text after an injection point is generated and then discarded. |
| 16 | + |
| 17 | +The memory bank ranks hints by TF-IDF cosine similarity over the hint text and its keywords. It is pure Python, so the plugin has no extra dependencies. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```python |
| 22 | +from openai import OpenAI |
| 23 | + |
| 24 | +client = OpenAI(api_key="sk-...", base_url="http://localhost:8000/v1") |
| 25 | + |
| 26 | +response = client.chat.completions.create( |
| 27 | + model="streaming_hints-gpt-4o-mini", |
| 28 | + messages=[{"role": "user", "content": "A train travels 120 km in 1.5 hours. What is its speed in m/s?"}], |
| 29 | + extra_body={"hint_threshold": 0.25, "show_hints": True}, |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +It combines with other approaches like any plugin, for example `streaming_hints&cot_reflection-gpt-4o-mini`. |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +All options are read from the request body (`extra_body`). |
| 38 | + |
| 39 | +| Option | Default | Description | |
| 40 | +|---|---|---| |
| 41 | +| `hint_threshold` | `0.2` | Minimum similarity score for a hint to be injected | |
| 42 | +| `max_hints` | `3` | Maximum number of hints injected per request | |
| 43 | +| `min_gap_chars` | `200` | Minimum characters of reasoning between two injections | |
| 44 | +| `segment_tokens` | `512` | Maximum tokens generated per segment | |
| 45 | +| `max_segments` | `8` | Maximum number of segments per request | |
| 46 | +| `hints_path` | bundled `hints.json` | Path to a custom hint bank | |
| 47 | +| `show_hints` | `false` | Return the reasoning with the `<hint>` tags instead of the model text only | |
| 48 | + |
| 49 | +`max_tokens`, if set, caps the completion tokens across all segments. |
| 50 | + |
| 51 | +## Custom hints |
| 52 | + |
| 53 | +A hint bank is a JSON list. `keywords` count twice as much as words in `text` when matching. |
| 54 | + |
| 55 | +```json |
| 56 | +[ |
| 57 | + { |
| 58 | + "id": "math-units", |
| 59 | + "domain": "math", |
| 60 | + "text": "Write down the units of every quantity and check both sides match.", |
| 61 | + "keywords": ["units", "speed", "rate", "convert"] |
| 62 | + } |
| 63 | +] |
| 64 | +``` |
| 65 | + |
| 66 | +The bundled bank has 20 general hints for math, coding and logical reasoning. Domain-specific banks will usually do better. |
| 67 | + |
| 68 | +## Future work |
| 69 | + |
| 70 | +- **RL-trained retriever.** Learn when to inject and which hint to pick from downstream task accuracy, with and without hints as the reward signal. `Hint.uses` is tracked as a starting point for usage statistics. |
| 71 | +- **Embedding retrieval.** A bank backed by sentence embeddings can replace TF-IDF behind the same `search` interface. |
| 72 | +- **Token streaming.** For providers that stream, the retriever can be fed token deltas directly and the stream cancelled at the injection point, which avoids the discarded tail of a segment. |
| 73 | +- **Evaluation.** Benchmark results (GSM8K, MATH, HumanEval, MMLU) against the plain model and against putting all hints in the prompt. |
0 commit comments