• Stars
    star
    112
  • Rank 302,555 (Top 7 %)
  • Language
    C
  • License
    Apache License 2.0
  • Created 9 months ago
  • Updated 16 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add AI capabilities to any readline-enabled command-line program

Build Status

ai-cli-lib: AI help for CLI programs

The ai-cli library detects programs that offer interactive command-line editing through the readline library, and modifies their interface to allow obtaining help from a GPT large language model server, such as Anthropic's or OpenAI's, or one provided through a llama.cpp server. Think of it as a command line copilot.

Demonstration

Build

The ai-cli library has been built and tested under the Debian GNU/Linux (bullseye) distribution (natively under version 11 and the x86_64 and armv7l architectures and under Windows Subsystem for Linux version 2), under macOS (Ventura 13.4) on the arm64 architecture using Homebrew packages and executables linked against GNU Readline (not the macOS-supplied editline compatibility layer), and under Cygwin (3.4.7). On Linux, in addition to make, a C compiler, and the GNU C library, the following packages are required: libcurl4-openssl-dev libjansson-dev libreadline-dev. On macOS, in addition to an Xcode installation, the following Homebrew packages are required: jansson readline. On Cygwin in addition to make, a C compiler, and the GNU C library, the following packages are required: libcurl-devel, libjansson-devel, libreadline-devel. Package names may be different on other systems.

cd src
make

Test

Unit testing

cd src
make unit-test

End-to-end testing

cd src
make e2e-test

This will provide you a simple read-print loop where you can test the ai-cli library's capability to link with the Readline API of third party programs.

Install

cd src

# Global installation for all users
sudo make install

# Local installation for the user executing the command
make install PREFIX=~

Run

  • Configure the ai-cli library to be activated when your bash shell starts up by adding the following lines in your .bashrc file (ideally near its beginning for performance reasons). Adjust the provided path match the ai-cli library installation path; it is currently set for a local installation in your home directory.
    # Initialize the ai-cli library
    source $HOME/share/ai-cli/ai-cli-activate-bash.sh
  • Alternatively, implement one of the following system-specific configurations.
    • Under Linux and Cygwin set the LD_PRELOAD environment variable to load the library using its full path. For example, under bash run export LD_PRELOAD=/usr/local/lib/ai_cli.so (global installation) or export LD_PRELOAD=/home/myname/lib/ai_cli.so (local installation).
    • Under macOS set the DYLD_INSERT_LIBRARIES environment variable to load the library using its full path. For example, under bash run export DYLD_INSERT_LIBRARIES=/Users/myname/lib/ai_cli.dylib. Also set the DYLD_LIBRARY_PATH environment variable to include the Homebrew library directory, e.g. export DYLD_LIBRARY_PATH=/opt/homebrew/lib:$DYLD_LIBRARY_PATH.
  • Perform one of the following.
    • Obtain your Anthropic API key or OpenAI API key and configure it in the .aicliconfig file in your home directory. This is done with a key={key} entry in the file's [anthropic] or [openai] section. In addition, add api=anthropic or api=openai in the file's [general] section. See the file ai-cli-config to understand how configuration files are structured. Anthropic currently provides free trial credits to new users. Note that OpenAI API access requires a different (usage-based) subscription from the ChatGPT one.
    • Configure a llama.cpp server and list its endpoint (e.g. endpoint=http://localhost:8080/completion in the configuration file's [llamacpp] section. In addition, add api=llamacpp in the file's [general] section. In brief running a llama.cpp server involves
      • compiling llama.cpp (ideally with GPU support),
      • downloading, converting, and quantizing suitable model files (use files with more than 7 billion parameters only on GPUs with sufficient memory to hold them),
      • Running the server with a command such as server -m models/llama-2-13b-chat/ggml-model-q4_0.gguf -c 2048 --n-gpu-layers 100.
  • Run the interactive command-line programs, such as bash, mysql, psql, gdb, sqlite3, bc, as you normally would.
  • If the program you want to prompt in natural language isn't linked with the GNU Readline library, you can still make it work with Readline, by invoking it through rlwrap. This however looses the program-specific context provision, because the program's name appears to The ai-cli library as rlwrap.
  • To obtain AI help, enter a natural language prompt and press ^X-a (Ctrl-X followed by a) in the (default) Emacs key binding mode or V if you have configured vi key bindings.
  • Keep in mind that by default ai-cli-lib is sending previously entered commands as context to the model engine you are using. This may leak secrets that you enter, for example by setting an environment variable to contain a key or by configuring a database password. To avoid this problem configure the context setting to zero, or use the command-line program's offered method to avoid storing an entered line. For instance, in bash you can do this by starting the line with a space character.

Note for macOS users

Note that macOS ships with the editline line-editing library, which is currently not compatible with the ai-cli library (it has been designed to tap onto GNU Readline). However, Homebrew tools link with GNU Readline, so they can be used with the ai-cli library. To find out whether a tool you're using links with GNU Readline (libreadline) or with editline (libedit), use the which command to determine the command's full path, and then the otool command to see the libraries it is linked with. In the example below, /usr/bin/sqlite3 isn't linked with GNU Readline, but /opt/homebrew/opt/sqlite/bin/sqlite3 is linked with editline.

$ which sqlite3
/usr/bin/sqlite3

$ otool -L /usr/bin/sqlite3
/usr/bin/sqlite3:
        /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
        /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.100.3)

$ otool -L /opt/homebrew/opt/sqlite/bin/sqlite3
/opt/homebrew/opt/sqlite/bin/sqlite3:
        /opt/homebrew/opt/readline/lib/libreadline.8.dylib (compatibility version 8.2.0, current version 8.2.0)
        /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
        /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.100.3)

Consequently, if you want to use the capabilities of the ai-cli library, configure your system to use the Homebrew commands in preference to the ones supplied with macOS.

Reference documentation

The ai-cli reference documentation is provided as Unix manual pages.

Contribute

Contributions are welcomed through GitHub pull requests. Before working on something substantial, open an issue to signify your interest and coordinate with others. Particular useful are:

  • multi-shot prompts for systems not yet supported (see the ai-cli-config file),
  • support for other large language models (start from the openai_fetch.c file),
  • support for other libraries (mainly editline),
  • ports to other platforms and distributions.

See also

Acknowledgements

  • API requests are made using libcurl.
  • The configuration file parsing is based on inih.
  • Unit testing uses CuTest.
  • JSON is parsed using Jansson.

More Repositories

1

unix-history-repo

Continuous Unix commit history from 1970 until today
Assembly
6,318
star
2

latex-advice

Advice for writing LaTeX documents
TeX
1,050
star
3

git-issue

Git-based decentralized issue management
Shell
711
star
4

awesome-msr

A curated repository of software engineering repository mining data sets
374
star
5

UMLGraph

Declarative specification and drawing of UML diagrams
Java
338
star
6

unix-history-make

Code and data to create a git repository representing the Unix source code history
Roff
317
star
7

dgsh

Shell supporting pipelines to and from multiple processes
C
316
star
8

pmonitor

Progress monitor: monitor a job's progress
Shell
185
star
9

cscout

C code refactoring browser
C
176
star
10

unix-v4man

Typeset the Fourth Research Edition Unix Programmer's Manual
Roff
137
star
11

ckjm

Chidamber and Kemerer Java Metrics
HTML
84
star
12

unix-architecture

Unix architecture evolution diagrams
Python
80
star
13

alexandria3k

Local relational access to openly-available publication data sets
Python
72
star
14

tokenizer

Convert source code into numerical tokens
C++
63
star
15

cqmetrics

C Quality Metrics
C++
56
star
16

effective-debugging

Code examples used in the book Effective Debugging (Addison-Wesley, 2016)
Java
42
star
17

bib2xhtml

Convert BibTeX references into XHTML
HTML
34
star
18

speak

Reviving the Research Edition Unix speak command
Rust
34
star
19

awesome-rest-apis

Currated collaborative list of open RESTful API web services
32
star
20

simple-rolap

Simple relational online analytical processing
Shell
27
star
21

unix-history-man

Manual page availability across major Unix releases
Perl
25
star
22

greek-vat-data

Retrieve the registration data associated with a Greek VAT number
Java
25
star
23

rdbunit

Unit testing for relational database queries
Python
23
star
24

unix-v3man

Typeset the Third Research Edition Unix Programmer's Manual
Roff
22
star
25

outwit

Command-line tools for accessing the Windows clipboard, registry, databases, document properties, and links.
C
20
star
26

lego-power-scratch

Control Lego power functions from Scratch
Python
17
star
27

oral-history-of-unix

Work by the late Michael Sean Mahoney, Professor of the History of Science at Princeton University, to create a history of Unix
HTML
16
star
28

kbd-layout-fix

Auto-correct text entered with the wrong keyboard layout
AutoHotkey
13
star
29

holiday-card

Simple Java AWT application to draw a Christmas card
Java
12
star
30

linux-history-repo

Reconstruction of the Linux kernel history with correct dates; see https://github.com/dspinellis/linux-history-make
C
11
star
31

socketpipe

Super efficient TCP connection between remote processes
C
11
star
32

manview

Unix man pages online viewer
CSS
10
star
33

OpenMIC

Open source implementation of the maximal information coefficient measure
C++
10
star
34

git-subst

Git plugin for substituting a regular expression with some text across all files under revision control
Shell
10
star
35

dostrace

A tool for logging MS-DOS system calls
C
9
star
36

word-master-ancient-greek

Ancient Greek version of the Wordly look-alike Word Master
JavaScript
9
star
37

greek-classifier

Classify surnames as Greek
Emacs Lisp
8
star
38

fileprune

Prune a file set according to a given age distribution
Roff
8
star
39

Kerberos

DSL-Configurable burglar alarm system for the Raspberry Pi
C
8
star
40

alt-truth

Alternative version of truth
C
7
star
41

linux-history-make

Reconstruct the Linux kernel history with correct dates
Shell
7
star
42

inaugural-analysis

Analysis of US inaugural presidential addresses
Python
7
star
43

Secrets-for-Java-SE

Decode Secrets for Android files on a Java SE platform
Java
6
star
44

cas2svg

Visualize Graphic 2 terminal .cas character descriptions
Perl
6
star
45

dgcmodem

Code fixes for the linuxant dgc modem drivers for 3.x kernels.
Shell
5
star
46

bibtools

Extract BibTeX records to standalone file for sharing with others
Perl
5
star
47

montty

Monitor input coming on a serial port
C
4
star
48

phd-reading-list

A reading list for research students (and their supervisors)
4
star
49

swill

Embedded web server interface library by S. Lampoudi and D. Beazley
C
4
star
50

PPS-monitor

Monitor a point-to-point (PPS) heating automation network link
Python
4
star
51

win32-bitmap-print

Demonstration of Win32 bitmap printing issue
C++
3
star
52

top-trumps-cards

"Top Trumps" cards for chemical elements
Perl
3
star
53

fast-libc

Improve C library performance (currently qsort) through multi-threading
C
3
star
54

mpcd

mpcd: Modular Performant Clone Detector
C++
3
star
55

madplay-playlist

Fork of MAD with a few extra features (see the commits)
C
3
star
56

athens-visitor-info

Information for Athens visitors
2
star
57

taru

Process and display space usage in tar files
Python
2
star
58

grconv

Greek character set converter
C++
2
star
59

git-mine-briefing

Presentation and handouts for MSR briefing on Git mining
HTML
2
star
60

rat-name

Rational C++ Naming Conventions
2
star
61

leap-sec

Leap second testing and visualization
C
2
star
62

gi-example

2
star
63

jit-binary

On demand compile and run programs distributed in source code form
Makefile
2
star
64

code-lifetime

Tools for analyzing the lifetime of code lines and tokens
Perl
2
star
65

sandbox

1
star
66

pcsecrets

Desktop client for the Secrets for Android password manager app
Java
1
star
67

bioinformatics

Adventures on Bioinformatics
1
star
68

CAGR

Compound Annual Growth Rate for Software
Perl
1
star
69

BlogRoll

BlogRoll of Software Data Analytics Blog and Mining Software Repositories
1
star
70

ax-178-logger

AXIO MET AX-178 multimeter logger
Python
1
star
71

elf-notes

Verify operation of ELF note section on Travis
C
1
star
72

scratch-joystick

Code that allows Scratch to read a joystick's values
Python
1
star
73

gi-issues

Issue management repository for gi
1
star
74

Favourite-movies

1
star