Simplify AI processor
This commit is contained in:
parent
2ec14f35f2
commit
548be49112
1 changed files with 0 additions and 92 deletions
|
|
@ -86,36 +86,6 @@ def build_processor(cfg: AIConfig) -> GenericAPIProcessor:
|
||||||
return GenericAPIProcessor(cfg)
|
return GenericAPIProcessor(cfg)
|
||||||
|
|
||||||
|
|
||||||
def list_models(base_url: str, api_key: str = "", timeout_sec: int = 10) -> list[str]:
|
|
||||||
if not base_url:
|
|
||||||
return []
|
|
||||||
url = _models_url(base_url)
|
|
||||||
req = urllib.request.Request(url, method="GET")
|
|
||||||
if api_key:
|
|
||||||
req.add_header("Authorization", f"Bearer {api_key}")
|
|
||||||
try:
|
|
||||||
with urllib.request.urlopen(req, timeout=timeout_sec) as resp:
|
|
||||||
body = resp.read()
|
|
||||||
data = json.loads(body.decode("utf-8"))
|
|
||||||
models = []
|
|
||||||
for item in data.get("data", []):
|
|
||||||
model_id = item.get("id")
|
|
||||||
if model_id:
|
|
||||||
models.append(model_id)
|
|
||||||
return models
|
|
||||||
except Exception:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
def load_system_prompt(_path: str | None = None) -> str:
|
|
||||||
return SYSTEM_PROMPT
|
|
||||||
|
|
||||||
|
|
||||||
def _models_url(base_url: str) -> str:
|
|
||||||
root = _root_url(base_url)
|
|
||||||
return root.rstrip("/") + "/v1/models"
|
|
||||||
|
|
||||||
|
|
||||||
def _chat_completions_url(base_url: str) -> str:
|
def _chat_completions_url(base_url: str) -> str:
|
||||||
if not base_url:
|
if not base_url:
|
||||||
return ""
|
return ""
|
||||||
|
|
@ -126,65 +96,3 @@ def _chat_completions_url(base_url: str) -> str:
|
||||||
return trimmed + "/chat/completions"
|
return trimmed + "/chat/completions"
|
||||||
return trimmed + "/v1/chat/completions"
|
return trimmed + "/v1/chat/completions"
|
||||||
|
|
||||||
|
|
||||||
def _root_url(base_url: str) -> str:
|
|
||||||
trimmed = base_url.rstrip("/")
|
|
||||||
if "/v1/" in trimmed:
|
|
||||||
return trimmed.split("/v1/")[0]
|
|
||||||
if trimmed.endswith("/v1"):
|
|
||||||
return trimmed[: -len("/v1")]
|
|
||||||
return trimmed
|
|
||||||
|
|
||||||
|
|
||||||
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="lel: %(asctime)s %(levelname)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),
|
|
||||||
)
|
|
||||||
|
|
||||||
logging.info("system prompt:\n%s", SYSTEM_PROMPT)
|
|
||||||
|
|
||||||
processor = build_processor(
|
|
||||||
AIConfig(
|
|
||||||
model=cfg.ai_cleanup.get("model", ""),
|
|
||||||
base_url=cfg.ai_cleanup.get("base_url", ""),
|
|
||||||
api_key=cfg.ai_cleanup.get("api_key", ""),
|
|
||||||
timeout_sec=25,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
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