30 lines
1.5 KiB
Python
30 lines
1.5 KiB
Python
import urllib.request, json, threading, queue, sys
|
|
BASE="http://192.168.88.184:8080"
|
|
q=queue.Queue()
|
|
def sse():
|
|
try:
|
|
r=urllib.request.urlopen(BASE+"/sse", timeout=30)
|
|
for raw in r:
|
|
s=raw.decode(errors="replace").strip()
|
|
if s.startswith("data:"): q.put(s[5:].strip())
|
|
except Exception as e: q.put("ERR:"+str(e))
|
|
threading.Thread(target=sse,daemon=True).start()
|
|
try:
|
|
ep=q.get(timeout=10)
|
|
if ep.startswith("ERR:"): print("stash unreachable:",ep); sys.exit(1)
|
|
purl=BASE+ep if ep.startswith("/") else ep
|
|
def post(o):
|
|
urllib.request.urlopen(urllib.request.Request(purl,data=json.dumps(o).encode(),headers={"Content-Type":"application/json"}),timeout=15).read()
|
|
post({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"apropos","version":"1"}}})
|
|
q.get(timeout=10)
|
|
post({"jsonrpc":"2.0","method":"notifications/initialized"})
|
|
query=sys.argv[1] if len(sys.argv)>1 else ""
|
|
limit=int(sys.argv[2]) if len(sys.argv)>2 else 3
|
|
post({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"recall","arguments":{"query":query,"limit":limit}}})
|
|
d=json.loads(q.get(timeout=25))
|
|
txt=d.get("result",{}).get("content",[{}])[0].get("text","[]")
|
|
facts=json.loads(txt)
|
|
if not facts: print("(no memory found for: %s)"%query); sys.exit(0)
|
|
for f in facts: print("- %s (score %.2f)" % (f.get("content","").strip(), f.get("score",0)))
|
|
except Exception as e: print("recall error:",e); sys.exit(1)
|