mill.py/mill_cli.py

74 lines
2.5 KiB
Python

#! /usr/bin/env python
#
# mill.py, Markdown interface for llama.cpp
# Copyright (C) 2024 unworriedsafari <unworriedsafari@tilde.club>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
r"""
## CLI install + usage
1. Clone the Git repo or download a release tarball and unpack it.
2. Set the environment variable `MILL_LLAMACPP_MAIN` to the path of
`llama.cpp/main` or your wrapper around it.
3. Pipe your Markdown document to `mill_cli.py`.
```bash
export MILL_LLAMACPP_MAIN=/path/to/llama.cpp/main
python /path/to/mill_cli.py <document.md
```
The result printed on stdout is the original document with the generated text
from the LLM added.
Like any CLI tool, you can also use it through SSH:
```bash
cat document.md | ssh <host> \
"MILL_LLAMACPP_MAIN=/path/to/llama.cpp/main python /path/to/mill_cli.py" \
2>/dev/null
```
Use the command-line arguments to select a different language or LLM engine.
You can use `-h` for a usage description. """
import argparse, mill, mill_readme, sys
if __name__ == '__main__':
argparser = argparse.ArgumentParser(
description='Markdown interface for llama.cpp')
argparser.add_argument('-r', '--readme', action='store_true',
help='display the README')
argparser.add_argument('-l', '--language', default=mill.default_language(),
help='language')
argparser.add_argument('-e', '--llm-engine',
default=mill.default_llm_engine(),
help='LLM engine')
args = argparser.parse_args()
language = mill.load_module(f'lang_{args.language}')
llm_engine = mill.load_module(f'llm_{args.llm_engine}')
input_lines = sys.stdin.readlines()
if args.readme or not ''.join([line.strip() for line in input_lines]):
mill_readme.print_readme(args.language, args.llm_engine)
exit(0)
exit_code = mill.main(language, llm_engine, input_lines)
exit(exit_code)