← Lesson
Finish Reason
Joeven
Run
Reset
Python loads on first run
def finish_reason(completion, max_tokens, stop): words = completion.split() for i, w in enumerate(words): if w == stop: return "stop", " ".join(words[:i]) if i + 1 >= max_tokens: return "length", " ".join(words[:max_tokens]) return "stop", completion def looks_json(text): t = text.strip() return t.startswith("{") and t.endswith("}") raw = '{ "action": "get_job", "job_id": 17' reason, text = finish_reason(raw, max_tokens=6, stop="END") print("reason", reason, "text", text, "valid", looks_json(text)) raw2 = '{ "action": "get_job", "job_id": 17 } END extra' reason2, text2 = finish_reason(raw2, max_tokens=50, stop="END") print("reason", reason2, "text", text2.strip(), "valid", looks_json(text2))
Run to execute this in your browser. Nothing is sent to a server.