Minimal CLI wrapper around a configured1 LLM service to ask quick one‑off questions while keeping an incremental conversation context per shell session.
- Context is preserved between runs within the lifetime of the parent shell process.
- Simple JSON context log (
-l) for inspection or tooling. - Automatic garbage collection of stale context files from previous runs.
- No external Python dependencies; only the standard library + the system
secret-tool(GNOME Keyring / libsecret). - Supports tool use (e.g.
dicethrowing tool) via LLM's function calling. - Input prompt can be provided as a command line argument or piped from a file or both.
- Ensure Python 3.11+ (tested on 3.12) and
secret-tool(part oflibsecret/ GNOME Keyring) are available. - Clone repo and make script executable:
git clone https://github.com/zvchei/q.git cd q chmod +x main.py # Recommended: symlink into PATH ln -s "$PWD/main.py" ~/.local/bin/q
- Store your Gemini API key in the desktop secret store:
secret-tool store --label "Gemini API Key" gemini api-key # When asked, paste the key and press Enter
Basic question:
q What is the capital of France?Show the help message:
q -hIf the prompt contains special characters it should be quoted, or the characters escaped, e.g.:
q "What does T&C stand for?"Pipe text input:
echo "Explain TCP slow start" | qCombine piped and argument input:
lscpu | q "How many cores do I have?"Use it in an interactive way:
$ q "How to tell what is the CUDA version on my system?"
nvidia-smi
$ nvidia-smi | q
12.8Redirect stderr from a failing command to get an explanation:
cat non-existent-file 2>&1 | qShow (dump) current context without querying:
q -lReset context (clears prior conversation):
q -rOr reset the context and run a new prompt in a single command:
q -r "Start fresh: give me 3 bullet ideas for a dev blog post"Enable debug logging (file operations, GC):
q --debug "Why is the sky blue?"Run a tool:
q -t dice "Roll one 12-sided die and two 6-sided dice"A tool for rolling dice.
q -t dice "Roll one 12-sided die and two 6-sided dice"
q -t dice "Roll 5 4-sided dice"
q -t dice "2d6, 2d8"
q -t dice "Roll a die"A tool for executing shell commands.
q -t console "ls"
q -t console "List all files modified in the last hour"
q -t console "What is my public IP address?"
q -t console "Create a cron job that runs backup.sh every day at midnight."
q -t console "Find the most frequent IP address in access.log"By default prints only the model answer. With -l prints full JSON context to stdout (after any new inference if a prompt was provided).
- Files are created in the system temp directory (e.g.
/tmp) named:q_context_<parent_shell_pid>_<parent_shell_starttime>.json. - Start time (from
/proc/<pid>/stat) ensures uniqueness across reused PIDs after shell restarts. - Garbage collection removes any context file whose originating shell process no longer exists.
- Resetting (
-r) clears the context for the current shell both in-memory and on-disk.
- 0 on success.
- Non‑zero if the LLM API returns HTTP error (propagated) or local runtime errors occur.
- API key is retrieved at runtime from the local secret storage (never written to disk by the tool).
- Context files contain your prompts & model replies in plain JSON. Avoid placing sensitive information in prompts.
Edit main.py and change the model variable at the llm construction line to your desired model name (e.g. llm = Gemini('gemini-1.5-pro')).
MIT-0 (see LICENSE).
- Context pruning strategy when approaching context limits.
- Support for image/file parts beyond plain text.
- Configurable system prompt and model.
- Support more LLM backends.
- Support other operating systems by using alternative credentials providers.
- Missing key: ensure
secret-tool lookup gemini api-keyreturns your key. - Permission denied: verify
main.pyis executable and/or symlink path in$PATH. - Run
q --debugto see debug logs.
Enjoy concise terminal Q&A with persistent lightweight context.
Footnotes
-
Currently supports only Google Gemini via their REST API. ↩