Unix native interface for interacting with LLMs.
cogni
brings language model scripting (prompting) into familiar Unix
environment by focusing on:
- Ergonomics and accessibility in Unix shell
- Composability and interop with other programs - including
cogni
itself - Ease of language model programming in both ad-hoc and repeatable manner
For example, designing for IO redirection (stdin
, stdout
) allows cogni
to
work with files, editor buffers, clipboards, syslogs, sockets, and many external
tools without bespoke integrations.
- Unix-minded Design (IO redirection, composability, interop)
- Ad-hoc Language Model Scripting
- Flexible input and output formats (Text, JSON, NDJSON, 🚧 Transcript)
- Standalone binary - No Python required
- 🚧 Repeatable Scripts via Templates
- Interactive use - instead, invoke
cogni
from within interactive environments (REPLs, emacs, etc)
# Install from crates.io
$ cargo install cogni
# From source
$ cargo install --path .
cogni
expects an OpenAI API Key to be supplied via --apikey
option or more
conveniently OPENAI_API_KEY
environment variable:
# in shell configuration
export OPENAI_API_KEY=sk-DEADBEEF
See cogni --help
for documentation
# Via stdin
$ echo "What is 50 + 50?" | cogni
50 + 50 equals 100.
# Via file
$ echo "What is 50 + 50?" > input.txt
$ cogni input.txt
50 + 50 equals 100.
# Via flags
# -s, --system <MSG> Sets system prompt (Always first)
# -a, --assistant <MSG> Appends assistant message
# -u, --user <MSG> Appends user message
$ cogni --system "Solve the following math problem" --user "50 + 50"
50 + 50 equals 100.
# Via repetitions of same flags. Useful for few-shot prompting
$ cogni --system "Solve the following math problem" \
-u "1 + 1" \
-a "2" \
-u "22 + 20" \
-a "42" \
-u "50 + 50"
100
# Via both flags and stdin. Flag messages come before stdin / file
$ echo "50 + 50" | cogni --system "Solve the following math problem" \
-u "1 + 1" \
-a "2" \
-u "22 + 20" \
-a "42"
100
An gallery of examples to get the inspiration flowing
⚠️ cogni
uses the OpenAI API, thus any data fed into program will be sent to OpenAI.
# Creating Summary of Meeting Transcripts
$ cat meeting_saved_chat.txt \
| cogni -s "Extract the links mentioned in this transcript, and provide a high level summary of the discussion points"
# Narrate Weather Summary
$ curl -s "wttr.in/?1" \
| cogni -s "Summarize today's weather using the output. Respond in 1 short sentence." \
| say
# Create a ffmpeg cheatsheet from man page
$ man ffmpeg \
| cogni -T 300 -s "Create a cheatsheet given a man page. Output should be in Markdown, and should be a set of example usages under headings." \
> cheatsheet.md
# Create a commit message for staged changes
$ git diff --staged \
| cogni -s "Create a commit message for the given staged changes. Use conventional commit format. Answer in a single-line raw plaintext. Don't use markdown." \
| git commit -F -
Emacs can use shell-command-on-region
to pipe buffer regions to cogni
.
For example, the following defines a command that plumbs region to cogni
, optionally replacing original contents:
(defun leoshimo/cogni-on-region (start end prompt replace)
"Run cogni on region. Prefix arg means replace region, instead of separate output buffer"
(interactive "r\nsPrompt: \nP")
(shell-command-on-region start end
(format "cogni -s \"%s\"" prompt)
nil replace))
(global-set-key (kbd "M-c") #'leoshimo/cogni-on-region)
This binding is useful across a wide range of tasks, for example:
- Normalizing non-uniform text - e.g. unstructured logs to structured JSON events.
- Editing or organizing text semantically - e.g. rewording or grouping by category.
- Generating summary for an Org Agenda doc.
Vim can run external shell commands on entire buffer or visual selection to
power similar workflows possible from Emacs. See h :!
in vim.
For example, given a bulleted list of fruits, it an be sorted by color by:
- Selecting the list of fruits in visual mode
- Type
:!cogni -s "Sort this list by color"