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= :
git clone https://github.com/xhcoding/emacs-aichat.git ~/.emacs.d/site-lisp/emacs-aichat
(add-to-list 'load-path "~/.emacs.d/site-lisp/emacs-aichat")
add the following code to your emacs startup file:
(require 'aichat)
Or load separately
(require 'aichat-bingai) ;; or (require 'aichat-openai)
[[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:
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']))))"
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:
(setq aichat-bingai-proxy "localhost:51837")
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:
(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)
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
=aichat-bingai-cookies-file= : The path of cookies file.
=aichat-bingai-conversation-style= : Conversation style, optional values are: =creative= , =balanced= or =precise= , default is =balanced=
=aichat-bingai-chat-file= : a file that saves chat history
=aichat-bingai-chat-display-function= : a function that displays chat files
=aichat-bingai-assistant-buffer= : Display the buffer name of the content returned by =aichat-bingai-assistant=
=aichat-bingai-assistant-display-function= : The function to display =aichat-bingai-assistant-buffer=, default to =display-buffer=
OpenAI
** Setup
*** AppKey
emacs-aichat obtains AppKey through =auth-source-pick-first-password=, and writes your appkey into =~/.authinfo= file, with the following format:
machine platform.openai.com login aichat-openai password your-app-key
*** Proxy [Optional]
Currently only tested http proxy, set =aichat-openai-proxy= as proxy address, for example:
(setq aichat-openai-proxy "localhost:51837")
** Usage
*** Chat
*** Assistant
*** Customized fixed questioning approach.
Use =aichat-openai-prompt-create= to create a fixed way of asking questions, such as:
(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)
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
=aichat-openai-proxy= : set up an HTTP proxy that requests OpenAI
=aichat-openai-assistant-buffer= : the buffer name of the OpenAI Assistant result
=aichat-openai-assistant-display-function= : the function of display assistant buffer
=aichat-openai-chat-directory= : Directory for storing chat files
=aichat-openai-chat-display-function= : Function to display chat buffer
Feedback Issues
To feedback your issue, please follow these steps: