Skip to content

shafreeck/guru

Repository files navigation

Guru

Go Report Card

Enjoy ChatGPT in your terminal. 中文

A guru is a teacher or mentor who is highly respected and knowledgeable in
their field. The term is often associated with spiritual or religious
leaders, but can also refer to experts in other areas such as business,
technology, or health.                      -- generated by ChatGPT

chat

Features

  • Works in conversation or oneshot mode, and it is default in conversation mode. Use --oneshot to enter the oneshot mode which drops the context messages for each request. Start guru with --last to continue the last conversation.
  • Flexible to manage context messages. The history messages would be auto-shrinked when the token limitation is exceeded, and you can view, delete, or shrink messages manually.
  • Redirect stdin/stdout with pipe. It is easy to wrap into your scripts.
  • Command auto-completion, cool animations, and Markdown rendering of ChatGPT responses.
  • Integrated with awesome prompt repos, default using awesome-chatgpt-prompts and awesome-chatgpt-prompts-zh (synchronization required for first use). It has two builtin prompts: Committer, Cheatsheet.
  • Basic session management such as session resumption, session creation, and session switching
  • Unique and powerful session stack, which can push or pop sub-sessions.
  • View or set internal parameters dynamicly, for example, modifying the API parameters.
  • Support to execute system commands, the output would be submitted for next request.
  • Unique and powerful Executor, which executes the output of ChatGPT, and optional to feedback the executed result.

Quick Start

Installation

go install github.com/shafreeck/guru@latest

Get Your OpenAI API Key

Guru uses OpenAI's open API to communicate with ChatGPT. Get the corresponding API key by logging in your account as a developer.

https://platform.openai.com/account/api-keys

Configure Guru

Configure guru for the first time using, run guru config and enter your OpenAI API Key gained from above step. A socks5 proxy is optional to use. You can bypass this step and using parameters directly for the command, but it is recommended to save your secret key into the configuration file.

guru-config-cropped

User Guide

Conversation Mode

> guru [text]

Enter conversation mode by directly executing guru.guru is actually an alias to the guru chat command. Run guru with --oneshot flag to enter oneshot mode, in this mode, guru drops the context messages for each asking. The session is always automatically recorded.(default in ~/.guru/session/ directory)

chat

Built-in Prompts

Act as a Cheatsheet

> guru cheat

guru cheat is an alias to the guru chat -p Cheatsheet command, which is used to simplify user input.

guru cheat

Act as a Committer

> git diff | guru commit

guru commit is an alias to guru chat -p Committer.

231173798-4d0d4f37-9343-407e-8cf5-c43f3ead52db

Oneshot Mode

Use the --oneshot parameter to enter one-shot conversation mode. In this mode, the context messages will be automatically discarded. However, if --prompt, -p specifies a prompt, the content of the prompt will be pinned and submitted with each request.

Use the : message pin command to pin any message

guru --oneshot

Redirect stdin and stdout

echo list files in this dir in detail | guru cheat | sh

redirect

Message management

ChatGPT does not store the context of the conversation on the server side. Its context-awareness capability is achieved by submitting all the context content from the client. As defined in the OpenAI API, both a submitted question or a replied answer is called a message. The content of a message is tokenized into tokens, and there is a limitation of the total tokens for both submitted and replied, which is 4096 at most. A long conversation would run out the tokens.

Guru supports automatic cleaning of old messages to achieve continuous conversation with a rolling window. However, sometimes we expect more precise control over the messages submitted to ChatGPT. At this time, internal commands from message management can be used to manually shrink, delete, or append messages.

For messages that we do not want to delete or not want to be cleaned by the rolling window, we can pin messages with the : message pin command. The oneshot mode uses this method to pin the prompt message, which keeps the prompt would be submitted for each question.

  • : message list Lists all current messages, aliasing :ls
  • : message delete [id...] Delete messages, where the parameters are message IDs that can delete multiple messages at the same time
  • : message shrink [expr] Shrinks messages, where expr is a range expression, it is the same as the expression in Golang Slice: begin:end. Begin or End can be omitted, for example 5:, which means to retain all messages with an ID greater than or equal to 5.
  • message show [id] Displays a certain message and renders it with Markdown. The default is to display the last message.
  • message append Appends a message, also available with the shortcut :append
  • message pin [id] Pins a message, and the pinned message will not be automatically deleted by the message auto-shrink mechanism and cannot be deleted by the : message delete command.
  • message unpin [id] Unpins a message

Session management

Each time guru is executed, a session is automatically created. The session history is saved in the ~/.guru/session/ directory by default. When starting, you can specify a session ID with --session-id, -s or restore the last session with --last. If the session ID specified does not exist, it will be created automatically.

Session management offers a wealth of features.You can create and switch sessions within the same Guru REPL. The most useful feature of session management is the session stack which enables the nesting of child sessions without interrupting the current session. Continuity of the session is very useful, for example, in a long conversation when talking about a paper, I expect the conversation messages to be recorded and then I would see the clear chatting history when reviewing in the future. Guru will also support exporting session histories later.

guru > :session
Available commands:

:session new                  create a new session
:session remove               delete a session
:session shrink               shrink sessions
:session list                 list sessions
:session switch               switch a session
:session history              print history of current session
:session stack                show the session stack
:session stack push           create a new session, and stash the current
:session stack pop            pop out current session
  • :session new creates a new session, can also be triggered via the shorthand alias :new.
  • :session remove [sid] removes a session.
  • :session shrink [expr] shrinks a session, where expr is a range expression, similar to the :message shrink command.
  • :session list lists all sessions, with the current session indicated by *.
  • :session switch [sid] switches to a different session.
  • :session history displays the session history.
  • :session stack displays the session stack status, can also be triggered via the shorthand alias :stack.
  • :session stack push creates a new session and pushes it onto the stack, can also be triggered via the shorthand alias >.
  • :session stack pop pops the current session from the stack, can also be triggered via the shorthand alias <.

Session stack usage

> is a special command that serves as an alias for :session stack push. When executed, it creates a new session and pushes it onto the stack. The command prompt will append a ">" symbol, such as guru >>. < is an alias for :session stack pop. When executed, it pops the topmost session from the stack, and the command prompt will remove the ">" symbol.

Note: currently, only > and < commands will affect the command prompt. Using :session stack push/pop directly will not have this effect, but we plan to improve the mechanism in the future.

Prompt repository management

The prompt repo commands enable you to add or sync prompt repositories. Currently, Guru supports two high-quality prompt repositories: awesome-chatgpt-prompts and awesome-chatgpt-prompts-zh. Users can also add their own favorite repositories.

Note that, upon initial usage, remote prompts other than Cheatsheet and Committer will require a :prompt repo sync command to sync their files for use. The synced prompt files will be stored in the ~/.guru/prompt/ directory by default.

guru > :prompt
Available commands:
:prompt act as                act as a role
:prompt list                  list all prompts
:prompt repo sync             sync prompts with remote repos
:prompt repo add              add a remote repo
:prompt repo list             list remote repos
Alias commands:
:prompts                      alias :prompts = :prompt list

:prompt commands enable you to use the prompts defined in your awesome-chatgpt-prompts repository, as well as add and sync your own prompt repositories.

  • :prompt act as acts as the role of the prompt, can also be triggered via the shorthand alias :act as.
  • :prompt list lists all the loaded prompt information, can also be triggered via the shorthand alias :prompts.
  • :prompt repo add/sync/list adds, syncs, and lists prompt repositories.

Acts as Linux Terminal

guru > :act as Linux Terminal

actas

Executing system commands

You can use the $ sign to execute system commands, and the output of the command will be submitted to ChatGPT in the next round of conversation. This is particularly useful when you need to load a file.

640

If no command follows the $ sign, Guru will switch to shell mode, and the command prompt will change to guru $. In this mode, any command you enter will be executed like in a shell, and all output will be submitted to ChatGPT in the next round of conversation.

In shell mode, entering > will return to conversation mode.

Viewing or setting live parameters

  • :info viewing live parameters
  • :set setting live parameters
dir                           /Users/shafreeck/.guru
filename
openai-api-key                sk-************************************************
pin                           false
prompt
session-id                    chat-1680879639912-1ec4e509-af5b-4abb-9f4b-bebde2276d96
socks5                        localhost:8804
stdin                         false
timeout                       3m0s
------------------------------
chatgpt.frequency_penalty     0
chatgpt.max_tokens            0
chatgpt.model                 gpt-3.5-turbo
chatgpt.n                     1
chatgpt.presence_penalty      0
chatgpt.stop
chatgpt.stream                true
chatgpt.temperature           1
chatgpt.top_p                 1
chatgpt.user
disable-auto-shrink           false
executor
feedback                      false
non-interactive               false
oneshot                       false
system
verbose                       false
:set chatgpt.temperature 0.5

Executor

The Executor is the most powerful and unique feature of Guru. When starting Guru, you can specify the executor using the --executor, -e argument. After each chat round, Guru will pass the ChatGPT output to the executor through stdin. If --feedback is specified, the executor's output will also be fed back to ChatGPT.

The executor is different from the system commands mentioned above. The system commands only enrich the means of data input through the shell. While the executor is used to handle ChatGPT output, implementing a complete closed loop of input -> output -> input. This means that we can use the executor during the conversation to process messages.

For security reasons, user confirmation is required for each executor call.

Shell as Executor

The simplest usage scenario is to execute the command returned by ChatGPT through the shell.

> guru cheat -e sh

executor

Advanced Feature: ChatGPT's Self-chatting

You can use the following command to achieve ChatGPT self-conversation:

> guru -e "guru --dir ./conversation --last" --feedback Hi

The principle of self-chatting is to use another guru as the executor, set the --dir of the second guru to its own independent directory to avoid confusion with the first guru, and set --last to restore the session every time it starts to maintain the context of the conversation.

640