okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""A tiny terminal chat for a running Strata server (start it with run-<model>.bat / run-<model>.sh first). 3 4python tools/chat.py [--port 8080] [--think none|low|medium|high] 5 6Type a message and press Enter. /image <path> attaches a picture to your next message (when the server was set up 7with images), /think <none|low|medium|high> sets how long the model thinks first, /reset starts a new conversation, 8/quit leaves. Standard library only. 9""" 10from __future__import annotations 11 12import argparse 13import base64 14import json 15import mimetypes 16import os 17import sys 18import time 19import urllib .request 20 21from highlight import StreamHighlighter 22 23 24def stream (url ,messages ,think ,max_tokens ): 25body = {"model" :"strata" ,"messages" :messages ,"stream" :True ,"max_tokens" :max_tokens , 26"reasoning_effort" :think } 27req = urllib .request .Request (url ,data = json .dumps (body ).encode (),headers = {"Content-Type" :"application/json" }) 28with urllib .request .urlopen (req ,timeout = 3600 )as r : 29for raw in r : 30line = raw .decode ("utf-8" ,"replace" ).strip () 31if not line .startswith ("data: " )or line == "data: [DONE]" : 32continue 33d = json .loads (line [6 :])["choices" ][0 ]["delta" ] 34yield d .get ("reasoning_content" )or "" ,d .get ("content" )or "" 35 36 37def main ()-> int : 38ap = argparse .ArgumentParser (description = __doc__ ,formatter_class = argparse .RawDescriptionHelpFormatter ) 39ap .add_argument ("--port" ,type = int ,default = 8080 ) 40ap .add_argument ("--host" ,default = "127.0.0.1" ) 41ap .add_argument ("--think" ,default = "medium" ,choices = ["none" ,"low" ,"medium" ,"high" ], 42help = "how long the model thinks before answering (none = answer directly)" ) 43ap .add_argument ("--no-think" ,action = "store_true" ,help = "same as --think none" ) 44ap .add_argument ("--max-tokens" ,type = int ,default = 4096 ) 45a = ap .parse_args () 46url = f"http:// { a . host } : { a . port } /v1/chat/completions" 47gray ,reset = ("\033[90m" ,"\033[0m" )if sys .stdout .isatty ()else ("" ,"" ) 48hl = StreamHighlighter (color = sys .stdout .isatty ()) 49messages ,pending = [], [] 50think = "none" if a .no_think else a .think 51f"Strata chat ( { url } ). /image <path> = attach a picture, /think none|low|medium|high (now: { think } ), " 52f"/reset = new conversation, /quit = leave." ) 53while True : 54try : 55user = input ("\nyou> " ).strip () 56except (EOFError ,KeyboardInterrupt ): 57return 0 59if user in ("/quit" ,"/exit" ): 60return 0 61if user == "/reset" : 62messages ,pending = [], [] 63"(new conversation)" ) 64continue 65if user .startswith ("/think" ): 66level = user [6 :].strip ().lower () 67if level in ("none" ,"low" ,"medium" ,"high" ): 68think = level 69f"(thinking: { think } )" ) 70else : 71"(usage: /think none|low|medium|high)" ) 72continue 73if user .startswith ("/image" ): 74path = user [6 :].strip ().strip ('"' ).strip ("'" ) 75if not os .path .isfile (path ): 76f"(no such file: { path or '?' } - usage: /image <path to a picture>)" ) 77continue 78mime = mimetypes .guess_type (path )[0 ]or "image/jpeg" 79data = base64 .b64encode (open (path ,"rb" ).read ()).decode () 80pending .append ({"type" :"image_url" ,"image_url" : {"url" :f"data: { mime } ;base64, { data } " }}) 81f"(picture attached: { os . path . basename ( path ) } - now type your question)" ) 82continue 83if not user : 84continue 85if pending : 86messages .append ({"role" :"user" ,"content" : [* pending , {"type" :"text" ,"text" :user }]}) 87pending = [] 88else : 89messages .append ({"role" :"user" ,"content" :user }) 90answer ,n ,t0 = [],0 ,time .time () 91"model> " ,end = "" ,flush = True ) 92try : 93in_think = False 94for reasoning ,content in stream (url ,messages ,think ,a .max_tokens ): 95if reasoning : 96if not in_think : 97gray ,end = "" ) 98in_think = True 99reasoning ,end = "" ,flush = True ) 100if content : 101if in_think : 102reset + "\n" ,end = "" ) 103in_think = False 104hl .feed (content ),end = "" ,flush = True ) 105answer .append (content ) 106n += 1 107if in_think : 108reset ,end = "" ) 109out = hl .flush () 110if out : 111out ,end = "" ) 112except OSError as e : 113f"\n(could not reach the server at { url } : { e } )" ) 114messages .pop () 115continue 116dt = time .time ()- t0 117f"\n { gray } [ { n } chunks in { dt :.1f } s] { reset } " ) 118messages .append ({"role" :"assistant" ,"content" :"" .join (answer )}) 119 120 121if __name__ == "__main__" : 122sys .exit (main ())