Datasets:
File size: 1,074 Bytes
1b15694 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | """A tiny stand-in for the OpenAI and Anthropic APIs, for testing run_api.py with no key and no spending."""
import json, re, sys
from http.server import BaseHTTPRequestHandler, HTTPServer
LOG = []
class H(BaseHTTPRequestHandler):
def log_message(self, *a): pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
with open(sys.argv[2], "a") as f: f.write(json.dumps({"path": self.path, "body": body}) + "\n")
text = json.dumps(body)
ans = "DECISION: YES" if "DECISION" in text else "OK"
if "CHOICE:" in text: ans = "CHOICE: A"
if "OWED:" in text: ans = "OWED: A, B"
if self.path.endswith("/messages"):
out = {"content": [{"type": "text", "text": ans}]}
else:
out = {"choices": [{"message": {"content": ans}}]}
b = json.dumps(out).encode()
self.send_response(200); self.send_header("Content-Type", "application/json"); self.end_headers(); self.wfile.write(b)
HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
|