-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
63 lines (52 loc) · 1.74 KB
/
cli.py
File metadata and controls
63 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import argparse
import asyncio
import sys
import dotenv
from src.simulacrum import Simulacrum
from src.utilities import parse_value
dotenv.load_dotenv()
def _parse_overrides(items: list[str]) -> dict:
overrides: dict = {}
for item in items:
key, sep, value = item.partition("=")
if not sep:
raise argparse.ArgumentTypeError(f"Expected key=value: {item!r}")
target = overrides
*parents, leaf = key.split(".")
for part in parents:
target = target.setdefault(part, {})
target[leaf] = parse_value(value)
return overrides
def main() -> None:
parser = argparse.ArgumentParser(
description="Get a single response from a character"
)
parser.add_argument("context_file", help="Path to the context YAML file")
parser.add_argument(
"prompt", nargs="?", help="User prompt (reads stdin if omitted)"
)
parser.add_argument(
"--set",
dest="overrides",
action="append",
default=[],
metavar="KEY=VALUE",
help="Override a context value (dotted keys for nesting). Repeatable.",
)
parser.add_argument(
"--keep-tags",
action="store_true",
help="Keep tags in the response instead of stripping them.",
)
args = parser.parse_args()
prompt = args.prompt or sys.stdin.read().strip()
if not prompt:
parser.error("No prompt provided")
overrides = _parse_overrides(args.overrides)
sim = Simulacrum(args.context_file, ephemeral=True, overrides=overrides)
response = asyncio.run(sim.chat(prompt, None, None))
if args.keep_tags:
response = sim.context.conversation_messages[-1].content or ""
print(response)
if __name__ == "__main__":
main()