Add context capture and rules
This commit is contained in:
parent
34ecdcbfde
commit
0e79edfa20
7 changed files with 247 additions and 80 deletions
|
|
@ -1,6 +1,9 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
import urllib.request
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
|
@ -65,3 +68,59 @@ def build_processor(cfg: AIConfig) -> GenericAPIProcessor:
|
|||
if not cfg.base_url:
|
||||
raise ValueError("ai_base_url is required for generic API")
|
||||
return GenericAPIProcessor(cfg)
|
||||
|
||||
|
||||
def _read_text(arg_text: str) -> str:
|
||||
if arg_text:
|
||||
return arg_text
|
||||
return sys.stdin.read()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
from config import load, redacted_dict
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--config", default="", help="path to config.json")
|
||||
parser.add_argument("text", nargs="?", default="", help="text to process (or stdin)")
|
||||
args = parser.parse_args()
|
||||
|
||||
logging.basicConfig(stream=sys.stderr, level=logging.INFO, format="ai: %(asctime)s %(message)s")
|
||||
cfg = load(args.config)
|
||||
|
||||
logging.info(
|
||||
"config (%s):\n%s",
|
||||
args.config or str(Path.home() / ".config" / "lel" / "config.json"),
|
||||
json.dumps(redacted_dict(cfg), indent=2),
|
||||
)
|
||||
|
||||
if not cfg.ai_enabled:
|
||||
logging.warning("ai_enabled is false; proceeding anyway")
|
||||
|
||||
prompt = load_system_prompt(cfg.ai_system_prompt_file)
|
||||
logging.info("system prompt:\n%s", prompt)
|
||||
|
||||
processor = build_processor(
|
||||
AIConfig(
|
||||
model=cfg.ai_model,
|
||||
temperature=cfg.ai_temperature,
|
||||
system_prompt_file=cfg.ai_system_prompt_file,
|
||||
base_url=cfg.ai_base_url,
|
||||
api_key=cfg.ai_api_key,
|
||||
timeout_sec=cfg.ai_timeout_sec,
|
||||
)
|
||||
)
|
||||
|
||||
text = _read_text(args.text).strip()
|
||||
if not text:
|
||||
logging.error("no input text provided")
|
||||
return 2
|
||||
|
||||
output = processor.process(text)
|
||||
sys.stdout.write(output)
|
||||
if not output.endswith("\n"):
|
||||
sys.stdout.write("\n")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue