Chat with AI Models In Your Terminal Using Shell Ask

Use Shell Ask to interact with ChatGPT, Claude, Gemini, and more directly in your terminal.

Shell Ask is an open-source node.js library that allows you to interact with popular AI models like ChatGPT, Claude, Gemini, and Ollama directly from your terminal.

It is developed entirely in TypeScript and can be installed via npm and configured with ease.

Subscribe to our newsletter and get the top 10 AI tools and apps delivered straight to your inbox. Subscribe now!

Once set up, users can pose queries or issue commands, with the option to pipe outputs from other commands as input.

While Shell Ask itself is free, it relies on large language models (LLMs) from providers like OpenAI, Anthropic, Ollama, and Google Gemini, which may incur usage fees based on the number of tokens and the API pricing.

How to use it:

1. To install Shell Ask, ensure you have Node.js installed on your device. Then, use npm to install the package:

# NPM
$ npm install -g shell-ask

2. Config the Shell Ask and insert your API key and API URL:

ask configure

3. Alternatively, you can manually edit the configuration file at ~/.config/shell-ask/config.json. Refer to the JSON schema or type definition in the package for all available options.

// src/config.ts
...
export type AICommandVariable =
// A shell command to run, the output will be used as the variable value
| string
// Get text input from the user
| { type: "input"; message: string }
// Get a choice from the user
| {
type: "select"
message: string
choices: { value: string; title: string }[]
}

export type AICommand = {
/** the cli command */
command: string
/** description to show in cli help */
description?: string
variables?: Record<string, AICommandVariable>
prompt: string
/** Require piping output from another program to Shell Ask */
require_stdin?: boolean
}

// no addtional property allowed
export type Config = {
default_model?: string
openai_api_key?: string
openai_api_url?: string
gemini_api_key?: string
anthropic_api_key?: string
commands?: AICommand[]
}
...

4. To ask a question, simply use the ask command followed by the question enclosed in double quotes:

ask "your question"

5. To ensure that the output is a command only, use the -c or --command flag:

ask "your question" -c

6. Select an AI model by name or ID:

ask "your question" -m gpt
ask "your question" -m claude
ask "your question" -m ollama
ask "your question" -m gpt-4o
ask "your question" -m claude-3-opus

7. Shell Ask allows you to pipe the output of other programs to ask. For example, you can use cat to add file contents to the LLM context:

cat config.ts | ask "your question"

8. To add multiple files, use bat:

bat src/*.ts | ask "your question"

9. Modify the config file (config.json) to add custom commands, such as the built-in ask cm command:

export const builtinCommands: AICommand[] = [
{
command: "cm",
description: "Generate git commit message based on git diff output",
require_stdin: true,
prompt:
"Generate git commit message following Conventional Commits specification based on the git diff output in stdin\nYou must return a commit message only, without any other text or quotes.",
},
{
command: "type-to-json-schema",
description: "Generate JSON schema from a TypeScript type",
variables: {
typeName: {
type: "input",
message: "Type name",
},
},
require_stdin: true,
prompt: `Generate JSON schema from the "{{typeName}}" type in stdin. You must return the JSON string only, without any other text or quotes.`,
},
]

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest & top AI tools sent directly to your email.

Subscribe now to explore the latest & top AI tools and resources, all in one convenient newsletter. No spam, we promise!