|
| 1 | +""" |
| 2 | +ollama_sigma_feed.py |
| 3 | +-------------------- |
| 4 | +Streams tokens from a local Ollama model and writes a rolling "sigma" proxy |
| 5 | +to a text file for the Rlang autopoietic bridge to read. |
| 6 | +
|
| 7 | +Requirements: |
| 8 | + - Python 3.9+ |
| 9 | + - `requests` library |
| 10 | + - Ollama running locally (default http://localhost:11434) |
| 11 | +
|
| 12 | +Usage: |
| 13 | + python ollama_sigma_feed.py --model llama3:8b --prompt "Explain resonance in one paragraph." --sigma-file sigma_feed.txt --window 64 |
| 14 | +
|
| 15 | +Sigma proxy (no logprobs mode): |
| 16 | + We compute a rolling token-distribution entropy H over the last N tokens. |
| 17 | + Then map H into a bounded coherence measure sigma ~= 1 + k*(H0 - H), |
| 18 | + where H0 is a reference entropy (approx upper bound for your model/tokenizer window). |
| 19 | +
|
| 20 | +If your Ollama build exposes token logprobs in the stream, you can enable |
| 21 | +`--use-logprobs` and we will compute proper entropy from probabilities. |
| 22 | +""" |
| 23 | +import argparse, time, json, collections, math, requests |
| 24 | + |
| 25 | +def rolling_entropy_from_tokens(tokens): |
| 26 | + # Simple character-level fallback: frequency of initial char of token text |
| 27 | + # (works acceptably on common tokenizers for a quick proxy without logprobs). |
| 28 | + freq = collections.Counter(t[:1] for t in tokens if t) |
| 29 | + total = sum(freq.values()) or 1 |
| 30 | + p = [c/total for c in freq.values()] |
| 31 | + H = -sum(pi*math.log(max(pi,1e-12)) for pi in p) # nats |
| 32 | + return H |
| 33 | + |
| 34 | +def map_entropy_to_sigma(H, H_ref=2.0, k=0.25): |
| 35 | + # sigma ~ 1 when H == H_ref; >1 if H < H_ref (more coherent); <1 if H > H_ref |
| 36 | + return 1.0 + k*(H_ref - H) |
| 37 | + |
| 38 | +def stream_ollama(model, prompt, host="http://localhost:11434", use_logprobs=False): |
| 39 | + url = f"{host}/api/generate" |
| 40 | + payload = { |
| 41 | + "model": model, |
| 42 | + "prompt": prompt, |
| 43 | + "stream": True, |
| 44 | + # You may set options here if desired: |
| 45 | + # "options": {"temperature": 0.8, "top_p": 0.9} |
| 46 | + } |
| 47 | + with requests.post(url, json=payload, stream=True) as r: |
| 48 | + r.raise_for_status() |
| 49 | + for line in r.iter_lines(): |
| 50 | + if not line: |
| 51 | + continue |
| 52 | + try: |
| 53 | + data = json.loads(line.decode("utf-8")) |
| 54 | + except Exception: |
| 55 | + continue |
| 56 | + yield data |
| 57 | + |
| 58 | +def main(): |
| 59 | + ap = argparse.ArgumentParser() |
| 60 | + ap.add_argument("--model", default="llama3:8b") |
| 61 | + ap.add_argument("--prompt", default="Explain resonance as breathing of a system.") |
| 62 | + ap.add_argument("--sigma-file", default="sigma_feed.txt") |
| 63 | + ap.add_argument("--window", type=int, default=64, help="rolling window (tokens)") |
| 64 | + ap.add_argument("--host", default="http://localhost:11434") |
| 65 | + ap.add_argument("--use-logprobs", action="store_true", help="if available in your Ollama stream") |
| 66 | + args = ap.parse_args() |
| 67 | + |
| 68 | + tokens = collections.deque(maxlen=args.window) |
| 69 | + # Try a rough upper reference entropy; adjust if your tokenizer is different |
| 70 | + H_ref = 2.0 |
| 71 | + with open(args.sigma_file, "w", encoding="utf-8") as sf: |
| 72 | + sf.write("1.00\n") # initial neutral value |
| 73 | + |
| 74 | + print(f"[ollama_sigma_feed] connecting to {args.host}, model={args.model}") |
| 75 | + for msg in stream_ollama(args.model, args.prompt, args.host, args.use_logprobs): |
| 76 | + if msg.get("done"): |
| 77 | + break |
| 78 | + tok_text = msg.get("response", "") |
| 79 | + if not tok_text: |
| 80 | + continue |
| 81 | + # Split coarse to capture sub-tokens quickly |
| 82 | + for ch in tok_text.split(): |
| 83 | + tokens.append(ch) |
| 84 | + H = rolling_entropy_from_tokens(tokens) |
| 85 | + sigma = map_entropy_to_sigma(H, H_ref=H_ref, k=0.25) |
| 86 | + # clamp to reasonable living range [0.5, 1.5] |
| 87 | + sigma = max(0.5, min(1.5, sigma)) |
| 88 | + with open(args.sigma_file, "w", encoding="utf-8") as sf: |
| 89 | + sf.write(f"{sigma:.4f}\n") |
| 90 | + print("[ollama_sigma_feed] stream complete; final sigma written.") |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments