(
messages: list[dict[str, str]],
metadata: Optional[dict] = None,
filename: Optional[str] = None,
)
| 8 | |
| 9 | |
| 10 | def log_conversation( |
| 11 | messages: list[dict[str, str]], |
| 12 | metadata: Optional[dict] = None, |
| 13 | filename: Optional[str] = None, |
| 14 | ) -> None: |
| 15 | functions = {} |
| 16 | conversation = [] |
| 17 | |
| 18 | timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 19 | my_metadata = { |
| 20 | "timestamp": timestamp, |
| 21 | "log_version": 0.2, |
| 22 | } |
| 23 | my_metadata.update(metadata or {}) |
| 24 | timestamp = my_metadata["timestamp"] |
| 25 | for message in messages: |
| 26 | if message["role"] != "assistant": |
| 27 | conversation.append(message) |
| 28 | else: |
| 29 | content = message["content"] |
| 30 | script = parse_script(content)[1] |
| 31 | function_name = f"function_{len(functions)+1}" |
| 32 | functions[function_name] = script |
| 33 | conversation.append({"role": "assistant", "content": function_name}) |
| 34 | |
| 35 | script = f"conversation = {json.dumps(conversation, indent=4)}\n\n" |
| 36 | script += f"metadata = {json.dumps(my_metadata, indent=4)}\n\n\n" |
| 37 | for function_name, function in functions.items(): |
| 38 | script += f"def {function_name}():\n" + indent(function, " ") + "\n\n\n" |
| 39 | |
| 40 | script += dedent(f"""\ |
| 41 | if __name__ == "__main__": |
| 42 | function_{len(functions)}() |
| 43 | """) |
| 44 | |
| 45 | if filename is None: |
| 46 | script_filename = rawdog_dir / f"script_{timestamp}.py" |
| 47 | with open(script_filename, "w") as script_file: |
| 48 | script_file.write(script) |
| 49 | |
| 50 | latest_script_filename = rawdog_dir / "latest.py" |
| 51 | with open(latest_script_filename, "w") as script_file: |
| 52 | script_file.write(script) |
| 53 | else: |
| 54 | with open(filename, "w") as script_file: |
| 55 | script_file.write(script) |
no test coverage detected