iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🧠

Interactive Chat AI CLI for Terminal

に公開

If you're a programmer, you probably want to chat with AI in the terminal you already have at hand, rather than through the https://chat.openai.com Web UI.

Features I want

  • Streaming output
  • Save conversation context
  • Select models from multiple platforms
    • OpenAI
    • AnthropicAI
    • CloudflareAI
  • Since multi-line input is difficult as-is, use --edit to open and edit in VS Code
  • Prepare several system prompts and select them when creating conversation contexts

I made it

So, I built a prototype with Deno.

https://github.com/mizchi/previs/tree/main/chat

By default, it installs as chat, but you can adjust this using the --name part.

# install deno on mac
# curl -fsSL https://deno.land/x/install/install.sh | sh
$ deno install -AF https://deno.land/x/previs@v0.1.7/chat/chat.ts --name chat
✅ Successfully installed chat

$ chat -h    
Usage:
  $ chat [cmd] [...options] [message]
  $ chat "hello" # chat with message
  $ chat -e # edit message with vscode

COMMANDS:
  $ chat history [context]      # Show chat history
  $ chat system new <system-name>       # system with vscode
  $ chat context [context]      # Show context or switch
  $ chat model  # Switch model
  $ chat alias [context]        # Create alias
  $ chat delete [context]       # Delete context
  $ chat reset  # Reset context

Options:
  --edit:       Edit file
  --debug:      Debug mode
  --help:       Show help
  --prompt:     Prompt file path
  --file:       from file path
  --template:   Template file path

Usage

Set the API_KEY environment variables depending on the model you want to use.

$ export OPENAI_API_KEY ...
$ export ANTHROPIC_API_KEY ...
$ export CF_API_TOKEN ...
$ export CF_ACCOUNT_ID...

When you first launch it, you'll select a model and a system prompt.

$ chat "Hello"
[chat] iqdh1tsqyg(2)[model: claude-3-opus-20240229][system: default]
Hello! Is there something specific I can help you with today? Feel free to ask me anything.

$ chat --edit
# VS Code will open the file; it will be sent after you edit and save it.

# Reset the conversation context
$ chat reset

Adding System Prompts

You can add system prompts by creating files like ~/.promptsn/templates/my-template.md.

$ chat reset                           
[chat] greeting(0)[model: claude-3-opus-20240229][system: default]
Select model
  openai : gpt-4
  openai : gpt-4-turbo
> anthropic : claude-3-opus-20240229
  anthropic : claude-3-sonnet-20240229
  anthropic : claude-3-haiku-20240307
  cloudflare : @cf/meta/llama-2-7b-chat-int8
  cloudflare : @cf/meta/llama-2-7b-chat-fp16

Of course, you will need the appropriate API_KEY for your environment.

Conclusion

I'm sure there are countless tools like this being created, but in the end, I feel like I just have to build exactly what I want myself.

Discussion