xhcoding / emacs-aichat

AI Chat in Emacs, including OpenAI and Bing Chat
62 stars 8 forks source link

+html:

+html:

+html:

+html:

Use AI chat features in Emacs, currently including Bing Chat and OpenAI’s ChatGpt.

** Requirements

** Install

Clone the repository to your local machine, and add the path to your =load-path= :

+begin_src sh

git clone https://github.com/xhcoding/emacs-aichat.git ~/.emacs.d/site-lisp/emacs-aichat

+end_src

+begin_src elisp

(add-to-list 'load-path "~/.emacs.d/site-lisp/emacs-aichat")

+end_src

add the following code to your emacs startup file:

+begin_src elisp

(require 'aichat)

+end_src

Or load separately

+begin_src elisp

(require 'aichat-bingai) ;; or (require 'aichat-openai)

+end_src

+html:

[[https://github.com/xhcoding/emacs-aichat/blob/main/images/examples.org][View more examples]]

Below is information on setting up Bing AI. If you are interested in learning about using OpenAI, please scroll down to the OpenAI section.

** Setup

*** Check access (Required)

*** Getting authentication (Required)

emacs-aichat uses browser cookies to log in to Bing Chat, so you need to log in to http://www.bing.com first in your browser.

There are two ways to get the browser's cookie, you can choose any convenient method.

**** Option one

If you have a Python environment, run =pip3 install rookiepy= to install =rookiepy=, then open the terminal and execute the following code:

+begin_src sh

python -c "import rookiepy;list(map(lambda c: print('{} {} {} {} {} {}'.format(c['name'], c['value'], c['expires'], c['domain'], c['path'], c['secure'])), filter(lambda c: c['domain'] in ('.bing.com'), rookiepy.edge(['.bing.com']))))"

+end_src

If you can see the output and there is a =_U= field in the output, then you can log in successfully with this method.

**** Option two

*** Proxy [Optional]

Currently only tested http proxy, set =aichat-bingai-proxy= as proxy address, for example:

+begin_src elisp

(setq aichat-bingai-proxy "localhost:51837")

+end_src

If your IP address is in China, you must set up a proxy to access it.

** Usage

*** Commands

*** Customized fixed questioning approach.

Use =aichat-bingai-prompt-create= to create a fixed way of asking questions, such as:

+begin_src elisp

(aichat-bingai-prompt-create "translator" :input-prompt "Please translate: " :text-format "Please identify the language I am speaking. If it is Chinese, please translate it into English; if it is not Chinese, please translate it into Chinese. Your answer must only contain the translated content. The following is the sentence I want to translate:\n%s" :chat t :assistant t :replace-or-insert t)

+end_src

The above configuration will generate three functions: =aichat-bingai-chat-translator=, =aichat-bingai-assistant-translator=, and =aichat-bingai-replace-or-insert-translator=, which have the same effect as the above three commands, but the content of the query will be in the format of =text-format=.

*** Options

+html:

** Setup

*** AppKey

emacs-aichat obtains AppKey through =auth-source-pick-first-password=, and writes your appkey into =~/.authinfo= file, with the following format:

+begin_src conf

machine platform.openai.com login aichat-openai password your-app-key

+end_src

*** Proxy [Optional]

Currently only tested http proxy, set =aichat-openai-proxy= as proxy address, for example:

+begin_src elisp

(setq aichat-openai-proxy "localhost:51837")

+end_src

** Usage

*** Chat

  1. Create a new chat file using =aichat-openai-chat=.
  2. Enter the chat content.
  3. Use =aichat-openai-chat-send-buffer (C-c C-c)= to send the entire contents of the buffer, or use =aichat-openai-chat-last-heading (C-c C-l)= to only send the last heading message.

*** Assistant

*** Customized fixed questioning approach.

Use =aichat-openai-prompt-create= to create a fixed way of asking questions, such as:

+begin_src elisp

(aichat-openai-prompt-create "translator" :input-prompt "Please translate: " :text-format "Please identify the language I am speaking. If it is Chinese, please translate it into English; if it is not Chinese, please translate it into Chinese. Your answer must only contain the translated content. The following is the sentence I want to translate:\n%s" :assistant t :replace-or-insert t)

+end_src

The above configuration will generate two functions: =aichat-openai-assistant-translator=, and =aichat-openai-replace-or-insert-translator=, which have the same effect as the above three commands, but the content of the query will be in the format of =text-format=.

*** Options

To feedback your issue, please follow these steps:

  1. Open the debug mode by typing the command =aichat-toggle-debug=.
  2. [[https://github.com/xhcoding/emacs-aichat/issues/new][Submit an issue]] and attach the content of the =AICHAT-DEBUG= buffer when an error occurs.