Use AI chat features in Emacs, currently including Bing Chat and OpenAI’s ChatGpt.
- emacs-websocket To communicate with Bing Chat, if you have already installed this package, please update to the latest version.
- emacs-async-await
- markdown-mode
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)
Below is information on setting up Bing AI. If you are interested in learning about using OpenAI, please scroll down to the OpenAI section.
- Install the latest version of Microsoft Edge
- Open http://bing.com/chat
- If you see a chat feature, you are good to go
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.
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.
- Install the cookie editor extension for Edge
- Go to bing.com
- Open the extension
- Click “Export” on the bottom right (This saves your cookies to clipboard)
- Paste your cookies into a file cookies.json
- Set
aichat-bingai-cookies-file
to your cookies.json path
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.
aichat-bingai-chat
: Send a conversation message, addingC-u
prefix will start a new conversationaichat-bingai-assistant
: Send the selected region or the input content, and the returned result is displayed in the Buffer defined byaichat-bingai-assistant-buffer
aichat-bingai-replace-or-insert
: Send the selected region or the input content, and replace the selected region or insert at the current position with the returned result
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
.
aichat-bingai-cookies-file
: The path of cookies file.aichat-bingai-conversation-style
: Conversation style, optional values are:creative
,balanced
orprecise
, default isbalanced
aichat-bingai-chat-file
: a file that saves chat historyaichat-bingai-chat-display-function
: a function that displays chat filesaichat-bingai-assistant-buffer
: Display the buffer name of the content returned byaichat-bingai-assistant
aichat-bingai-assistant-display-function
: The function to displayaichat-bingai-assistant-buffer
, default todisplay-buffer
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
Currently only tested http proxy, set aichat-openai-proxy
as proxy address, for example:
(setq aichat-openai-proxy "localhost:51837")
- Create a new chat file using
aichat-openai-chat
. - Enter the chat content.
- Use
aichat-openai-chat-send-buffer (C-c C-c)
to send the entire contents of the buffer, or useaichat-openai-chat-last-heading (C-c C-l)
to only send the last heading message.
aichat-openai-assistant
: Send the selected region or the input content, and the returned result is displayed in the Buffer defined byaichat-openai-assistant-buffer
aichat-openai-replace-or-insert
: Send the selected region or the input content, and replace the selected region or insert at the current position with the returned result
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
.
aichat-openai-proxy
: set up an HTTP proxy that requests OpenAIaichat-openai-assistant-buffer
: the buffer name of the OpenAI Assistant resultaichat-openai-assistant-display-function
: the function of display assistant bufferaichat-openai-chat-directory
: Directory for storing chat filesaichat-openai-chat-display-function
: Function to display chat buffer
To feedback your issue, please follow these steps:
- Open the debug mode by typing the command
aichat-toggle-debug
. - Submit an issue and attach the content of the
*AICHAT-DEBUG*
buffer when an error occurs.