Skip to content

Commit a1a95dd

Browse files
committed
Add ability to cat chat-template | llms as an alternative to llms --chat template.json
1 parent 1be24a0 commit a1a95dd

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

llms/main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4548,8 +4548,20 @@ async def start_background_tasks(app):
45484548
print(f"\nDefault model set to: {default_model}")
45494549
return ExitCode.SUCCESS
45504550

4551+
# Read chat template from stdin if data is piped (e.g. cat template.json | llms)
4552+
stdin_chat = None
4553+
if not sys.stdin.isatty():
4554+
stdin_data = sys.stdin.read().strip()
4555+
if stdin_data:
4556+
try:
4557+
stdin_chat = json.loads(stdin_data)
4558+
except json.JSONDecodeError:
4559+
print(f"Invalid JSON from stdin")
4560+
return ExitCode.FAILED
4561+
45514562
if (
45524563
cli_args.chat is not None
4564+
or stdin_chat is not None
45534565
or cli_args.image is not None
45544566
or cli_args.audio is not None
45554567
or cli_args.file is not None
@@ -4580,6 +4592,9 @@ async def start_background_tasks(app):
45804592
with open(chat_path) as f:
45814593
chat_json = f.read()
45824594
chat = json.loads(chat_json)
4595+
elif stdin_chat is not None:
4596+
_log(f"Using chat from stdin")
4597+
chat = stdin_chat
45834598

45844599
if cli_args.system is not None:
45854600
chat["messages"].insert(0, {"role": "system", "content": cli_args.system})

0 commit comments

Comments
 (0)