chatblade

chatblade

ChatGPT命令行工具 灵活高效的交互体验

Chatblade是一款功能丰富的命令行工具,专门用于与OpenAI的ChatGPT进行交互。该工具支持管道输入和参数传递,可保存常用提示以提高使用效率。Chatblade能从ChatGPT响应中提取JSON或Markdown,并提供会话管理、模型选择和流式输出等功能。此外,它还支持自定义提示和Azure OpenAI配置,为ChatGPT用户提供了灵活高效的命令行操作体验。

ChatbladeCLI工具ChatGPTAPI交互会话管理Github开源项目

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

Latest and greatest

To stay up to date with the current main branch you can:

  • check out the project, and run pip install .
  • or pip install 'chatblade @ git+https://github.com/npiv/chatblade'

Via pypi

The last released version can be installed with pip install chatblade --upgrade

Via Brew

brew install chatblade

Documentation

Making queries

A new conversation

You can begin any query by just typing. f.e.:

chatblade how can I extract a still frame from a video at 22:01 with ffmpeg
<img width="650" alt="image" src="https://user-images.githubusercontent.com/452020/226869260-1dcd4faf-521c-466b-998a-fd5cfdc5b3c7.png">

recall the last conversation

if you would like to recall the last conversation just call it back with -l

chatblade -l

Continue the last conversation

To continue the conversation and ask for a change within the context, you can again use -l but with a query.

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

-l is shorthand for -S last or the last session. We can keep track of and continue various distinct conversations using the session options

Picking between gpt-3.5 and 4

By default, gpt-3.5 is used, you can switch at any point to 4 by using -c 4 or the latest 4o ("omni") by using -c 4o.

Additionally, you can pass any arbitrary full model name, f.e. -c gpt-3.5-turbo-16k.

Chatting interactively

If you preferred to chat interactively instead just use chatblade -i.

Show streaming text (experimental)

You can also stream the responses, just like in the webui. At the end of the stream it will format the result. This can be combined in an interactive session

chatblade -s -i

https://user-images.githubusercontent.com/452020/226891636-54d12df2-528f-4365-a4f3-e51cb025773c.mov

Formatting the results

Responses are parsed and if chatblade thinks its markdown it will be presented as such, to get syntax highlighting. But sometimes this may not be what you want, as it removes new lines, or because you are only interested in extracting a part of the result to pipe to another command.

In that case you have 2 options:

  • -r for raw, which just prints the text exactly as ChatGPT returned it, and doesn't pass it through Markdown.
  • -e for extract, which will try to detect what was returned (either a code block or json) and extract only that part. If neither of those are found it does the same as -r

Both options can be used either with a new query, e.g.

chatblade -e write me a python boilerplate script that starts a server and prints hello world > main.py

or with the last result (json in this example)

chatblade -l -e | jq

Piping content into chatblade

If we have long prompts we don't want to type everytime, or just want to provide context for our query we can pipe into chatblade.

e.g.

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.

<img src="assets/example3.png">

or

chatblade what does this script do < script.sh

What gets sent to ChatGPT over the wire is:

piped input
-------
query

Session Options

Sessions are named conversations.

If you start chatblade with a session name SESS of your choice:

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

chatblade will create a session called SESS if it does not exist, and it will store the current exchange (query-response pair) for SESS.

If such a session already exists, the saved conversation will be loaded and the new exchange will be appended.

Without a session argument, the exchange also gets stored in a session named last; however, subsequent sessionless invocation will overwrite the content of last. (You can continue a conversation that was started as a sessionless exchange by passing -S last, but last won't be a safe space for keeping a conversation, as the next sessionless invocation will clear it again.) The -l option is provided as a shorthand for -S last.

If you specify a session without a query:

chatblade -S SESS

chatblade will recall the conversation without modifying the session.

chatblade supports various operations on sessions. It provides the --session-OP options, where OP can be list, path, dump, delete, rename.

Checking token count and estimated costs

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

We could do this when passing in a lot of context like in the example above for instance.

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
<img width="650" alt="image" src="https://user-images.githubusercontent.com/452020/226874588-28c53f53-1d19-4ce3-b7ec-b01c2f7cf75a.png">

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

Use custom prompts (the system msg)

The system message is used to instruct the model how to behave, see OpenAI - Instructing Chat Models.

These can be loaded with -p. For convenience any file we place under ~/.config/chatblade/ will be picked up by this command.

So for example, given the following file ~/.config/chatblade/etymology, which contains:

I want you to act as a professional Etymologist 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 what a perfect answer would look 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

You can also point -p to a file path directly to load a system message from any arbitrary location

<img src="assets/example5.png">

And since we asked for JSON, we can pipe our result to something else, e.g.:

chatblade -l -e > toanki

Configuring for Azure OpenAI

chatblade can be used with an Azure OpenAI endpoint, in which case in addition to the OPENAI_API_KEY you'll need to set the following environment variables:

  • OPENAI_API_TYPE :: Set to azure. As required by openai-python
  • AZURE_OPENAI_ENDPOINT :: URL to your cognitive services' endpoint, e.g. https://eastus.api.cognitive.microsoft.com/. Please note this is a breaking change introduced by openai-python and the previous environment variable name is OPENAI_API_BASE
  • OPENAI_API_AZURE_ENGINE :: name of your deployment in Azure, f.e. my-gpt-35-turbo (maps to a specific model)

Note: that this will override any option for -c 3.5 or -c 4 which don't make sense in this case.

Help

usage: Chatblade [-h] [--openai-api-key key] [--temperature t] [-c CHAT_GPT] [-i] [-s] [-t] [-p name] [-e] [-r] [-n] [-o] [--theme theme] [-l] [-S sess] [--session-list] [--session-path] [--session-dump] [--session-delete]
                 [--session-rename newsess]
                 [query ...]

a CLI Swiss Army Knife for ChatGPT

positional arguments:
  query                            Query to send to chat GPT

options:
  -h, --help                       show this help message and exit
  --openai-api-key key             the OpenAI API key can also be set as env variable OPENAI_API_KEY
  --openai-base-url key            A custom url to use the openAI against a local or custom model, eg ollama
  --temperature t                  temperature (openai setting)
  -c CHAT_GPT, --chat-gpt CHAT_GPT
                                   ChatGPT model - use either the fully qualified model name, or one of 3.5 (gpt-3.5-turbo), 4 (gpt-4),
                                   4t (gpt-4-turbo), 4o (gpt-4o), mini (gpt-4o-mini). Default is gpt-4o-mini. Can also be set via env variable OPENAI_API_MODEL, see
                                   https://platform.openai.com/docs/models/continuous-model-upgrades for available models.
  -i, --interactive                start an interactive chat session. This will implicitly continue the conversation
  -s, --stream                     Stream the incoming text to the terminal
  -t, --tokens                     display what *would* be sent, how many tokens, and estimated costs
  -p name, --prompt-file name      prompt name - will load the prompt with that name at ~/.config/chatblade/name or a path to a file

result formatting options:
  -e, --extract                    extract content from response if possible (either json or code block)
  -r, --raw                        print session as pure text, don't pretty print or format
  -n, --no-format                  do not add pretty print formatting to output
  -o, --only                       Only display the response, omit query
  --theme theme                    Set the theme for syntax highlighting see https://pygments.org/styles/, can also be set with CHATBLADE_THEME

session options:
  -l, --last                       alias for '-S last', the default session if none is specified
  -S sess, --session sess          initiate or continue named session
  --session-list                   list sessions
  --session-path                   show path to session file
  --session-dump                   dump session to stdout
  --session-delete                 delete session
  --session-rename newsess         rename session

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

下拉加载更多