Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/clevercli/clevercli

ChatGPT powered CLI utilities. Easily add new prompt types in ~/.clevercli/
https://github.com/clevercli/clevercli

chatgpt cli gpt gpt-3 openai

Last synced: about 2 months ago
JSON representation

ChatGPT powered CLI utilities. Easily add new prompt types in ~/.clevercli/

Lists

README

        

# clevercli

**clevercli** is a CLI that queries OpenAI models (e.g. ChatGPT). New prompt types can easily be added and there is a growing list of community maintained prompts.

Some examples:

```console
$ $(clevercli unix-command "undo the last commit")
$ clevercli unix-command "add a collaborator to a npm package"
$ clevercli eli5 "why is the sky blue?"
$ man du | clevercli summarize
$ clevercli convert-to-rust < index.ts > main.rs
$ clevercli ask "in node.js, how to check if stdin is open?"
```

## Install

```console
$ npm install -g @clevercli/cli
```

Requires Node v16+.

## Usage

```console
$ clevercli
```

Or using `stdin`:

```console
$ echo "" | clevercli
```

List available prompt types:

```
$ clevercli --list
```

Requires an [OpenAI API key](https://platform.openai.com/account/api-keys). Add `export OPENAI_API_KEY=""` to your shell script (e.g. `~/.bashrc`).

## Examples

```console
$ clevercli joke "banana"
Why did the banana go to the doctor? Because it wasn't peeling well!
$ echo "what is stdin?" | clevercli eli5
...
$ man du | clevercli summarize
...
$ clevercli convert-to-rust < index.ts > main.rs
```

Tip: since many answers use markdown, you can pipe to [glow](https://github.com/charmbracelet/glow) to get a nicer rendering:

```console
$ clevercli ask "in node.js, how to check if stdin is open?" | glow
```

![glow-example](./images/glow-example.png)

## Built-in prompts

- [ask](./src/prompts/ask.ts) Just passes through the input directly to ChatGPT.

Example: `clevercli ask "in node.js, how to check if stdin is open?"`

- [eli5](./src/prompts/eli5.ts): Explain Me Like I'm 5.

Example: `clevercli eli5 "why is the sky blue?"`

- [joke](./src/prompts/joke.ts): Tells a joke about the topic.

Example: `clevercli joke "git being difficult to learn"`

- [refactor](./src/prompts/refactor.ts): Asks ChatGPT to refactor code in a file.

Example: `clevercli refactor < index.ts`

- [poem](./src/prompts/poem.ts): Asks ChatGPT to write a small poem on the topic.

Example: `clevercli poem "hacker news"`

- [recipe](./src/prompts/recipe.ts): Outputs recipe suggestions given a list of available ingredients.

Example: `clevercli recipe "ham, cheese, bread"`

- [summarize](./src/prompts/summarize.ts) Outputs a short summary of the text.

Example: `clevercli summarize < README.md`

- [synonyms](./src/prompts/synonyms.ts): List synonyms for the input words.

Example: `clevercli synonyms "cat"`

- [convert-to-rust](./src/prompts/convert-to-rust.ts): Converts file to Rust

Example: `clevercli convert-to-rust < index.ts > main.rs`

- [convert-to-typescript](./src/prompts/convert-to-typescript.ts): Converts file to TypeScript

Example: `clevercli convert-to-typescript < index.js > main.ts`

- [unix-command](./src/prompts/unix-command.ts): Outputs a UNIX command based on the input description.

Example: `bash -c $(clevercli unix-command "list all .md files")`

- [regex](./src/prompts/regex.ts): Outputs a JavaScript-compatible RegEx that matches the input examples.

Example `clevercli regex "http://google.com https://news.ycombinator.com/some/path"`

- [jsdoc](./src/prompts/jsdoc.ts): Adds JSDoc comments to exported functions.

Example `clevercli jsdoc < index.js`

See [./src/prompts/](./src/prompts) for the list of available prompts.

See [Adding a built-in prompt](#adding-a-built-in-prompt) for adding an official prompt.

## Adding a prompt

Create a file `~/.clevercli/.mjs` which returns an object that follows the `PromptConfiguration` interface.

```typescript
export interface PromptConfiguration {
createPrompt(input: string): string;
model?: string;
description?: string;
}
```

For example:

```javascript
// eli5.mjs
export default {
createPrompt(input) {
return `Provide a very detailed explanation but like I am 5 years old (ELI5) on this topic: ${input}.\n###\n`;
},
};
```

## Adding a built-in prompt

1. Fork the repository.

2. Create a new prompt configuration in [./src/prompts/](./src/prompts/). You can use the [eli5](./src/prompts/eli5.ts) prompt configuration as a base.

3. Send a pull request!

Here's a sample prompt configuration:

```typescript
import { ParsedResponse, PromptConfiguration } from "../types.js";

const promptConfiguration: PromptConfiguration = {
createPrompt(input: string) {
return `Provide a very detailed explanation but like I am 5 years old (ELI5) on this topic: ${input}.\n###\n`;
},
parseResponse(response: string): ParsedResponse {
return { message: response };
},
};

export default promptConfiguration;
```

## Cache

Query results are cached on your filesystem's cache directory.

## Debugging

```
DEBUG="clevercli:*" clevercli eli5 "friendship"
```

## TODO

- [x] Streaming API.
- [ ] Proper CLI arg parsing and options
- [ ] GH workflow + tests
- [ ] Support older Node.js versions?
- [ ] Interactively prompt OpenAI API key and save to filesystem (when OPENAPI_KEY is not set)
- [ ] Distributed cache (not just local)