10

GitHub - npiv/chatblade: A CLI Swiss Army Knife for ChatGPT

 1 year ago
source link: https://github.com/npiv/chatblade
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Chatblade

A CLI Swiss Army Knife for ChatGPT

Chatblade is a versatile command-line interface (CLI) tool designed to interact with OpenAI's ChatGPT. It accepts piped input, arguments, or both, and allows you to save common prompt preambles for quick usage. Additionally, Chatblade provides utility methods to extract JSON or Markdown from ChatGPT responses.

Note: You'll need to set up your OpenAI API key to use Chatblade.

You can do that by either passing --openai-api-key KEY or by setting an env variable OPENAI_API_KEY (recommended). The examples below all assume an env variable is set.

Install

on linux like systems you should be able to just checkout the project and pip install .

Some Examples

Basic

In its simplest form, Chatblade can perform a straightforward query:

chatblade how can I extract a still frame from a video at 22:01 with ffmpeg

example1.png

Continue a conversation and extract

To continue the conversation and ask for a change, you can use the -l flag for "last."

chatblade -l can we make a gif instead from 00:22:01 to 00:22:04

example2.png

You can also use -l without a query to redisplay the last thread at any point.

If you want to extract the last suggested command, you can use the -e flag. For example:

chatblade -e | pbcopy

This command places the ffmpeg suggestion in your clipboard on macOS.

Piping into Chatblade

You can pipe input to Chatblade:

curl https://news.ycombinator.com/rss | chatblade given the above rss can you show me the top 3 articles about AI and their links -c 4

The piped input is placed above the query and sent to ChatGPT. In this example, we also use the -c 4 flag to select ChatGPT-4 (the default is ChatGPT-3.5).

example3.png

Check token count and estimaed costs

If you want to check the approximate cost and token usage of a previous query, you can use the -t flag for "tokens."

curl https://news.ycombinator.com/rss | chatblade given the above rss can you show me the top 3 articles about AI and their links -t

example4.png

this won't perform any action over the wire, and just calculates the tokens locally

Make custom prompts

We can also save common prompt configs for easy reuse. Any yaml file we place under ~/.config/chatblade/ will be picked up by the command.

So for example given the following yaml called etymology.yaml, and which contains:

system: |-
  I want you to act as a professional Etomologist and Quiz Generator. You have a deep knowledge of etymology and will be provided with a word. 
  The goal is to create cards that quiz on both the etymology and finding the word by its definition.

  The following is a what a perfect answer would like for the word "disparage"

  [{
    "question": "A verb used to indicate the act of speaking about someone or something in a negative or belittling way.<br/> <i>E.g He would often _______ his coworkers behind their backs.</i>",
    "answer": "disparage"
  },
  {
    "question": "What is the etymological root of the word disparage?",
    "answer": "From the Old French word <i>'desparagier'</i>, meaning 'marry someone of unequal rank', which comes from <i>'des-'</i> (dis-) and <i>'parage'</i> (equal rank)"
  }]

  You will return answers in JSON only. Answer truthfully and if you don't know then say so. Keep questions as close as possible to the
  provided examples. Make sure to include an example in the definition question. Use HTML within the strings to nicely format your answers.

  If multiple words are provided, create questions and answers for each of them in one list. 
  
  Only answer in JSON, don't provide any more text. Valid JSON uses "" quotes to wrap its items.

we can now run a command and refer to this prompt with -p etymology

chatblade -p etymology gregarious

example5.png

And since we asked for json we can pipe our result to something else e.g.

chatblade -l -e > toanki
usage: chatblade [-h] [--last] [--prompt-config PROMPT_CONFIG] [--openai-api-key OPENAI_API_KEY] [--temperature TEMPERATURE] [--chat-gpt {3.5,4}] [--extract] [--raw] [--tokens]
                 [query ...]

Chatblade

positional arguments:
  query                 Query to send to chat GPT

options:
  -h, --help            show this help message and exit
  --last, -l            Display the last result. If a query is given the conversation is continued
  --prompt-config PROMPT_CONFIG, -p PROMPT_CONFIG
                        Prompt config name, or file containing a prompt config
  --openai-api-key OPENAI_API_KEY
                        OpenAI API key can also be set as env variable OPENAI_API_KEY
  --temperature TEMPERATURE
                        Temperature (openai setting)
  --chat-gpt {3.5,4}, -c {3.5,4}
                        Chat GPT model (default 3.5)
  --extract, -e         Extract content from response if possible (either json or code block)
  --raw, -r             print the last response as pure text, don't pretty print or format
  --tokens, -t          Display what *would* be sent, how many tokens, and estimated costs

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK