emacs-aichat
emacs-aichat copied to clipboard
AI Chat in Emacs, including OpenAI and Bing Chat
#+html:
- AI Chat in Emacs
Use AI chat features in Emacs, currently including Bing Chat and OpenAI’s ChatGpt.
- Installation
** Requirements
- [[https://github.com/ahyatt/emacs-websocket][emacs-websocket]] To communicate with Bing Chat, if you have already installed this package, please update to the latest version.
- [[https://github.com/chuntaro/emacs-async-await][emacs-async-await]]
- [[https://github.com/jrblevin/markdown-mode][markdown-mode]]
** 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
- Bing Chat
#+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)
- Install the latest version of Microsoft Edge
- Open http://bing.com/chat
- If you see a chat feature, you are good to go
*** 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
- Install the cookie editor extension for [[https://microsoftedge.microsoft.com/addons/detail/cookieeditor/neaplmfkghagebokkhpjpoebhdledlfi][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
*** 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
- =aichat-bingai-chat= : Send a conversation message, adding =C-u= prefix will start a new conversation
- =aichat-bingai-assistant= : Send the selected region or the input content, and the returned result is displayed in the Buffer defined by =aichat-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
*** 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
- =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
#+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
- 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 use =aichat-openai-chat-last-heading (C-c C-l)= to only send the last heading message.
*** Assistant
- =aichat-openai-assistant= : Send the selected region or the input content, and the returned result is displayed in the Buffer defined by =aichat-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
*** 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
- =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:
- Open the debug mode by typing the command =aichat-toggle-debug=.
- [[https://github.com/xhcoding/emacs-aichat/issues/new][Submit an issue]] and attach the content of the =AICHAT-DEBUG= buffer when an error occurs.
- Acknowledgements
- [[https://github.com/acheong08/EdgeGPT][EdgeGPT]]
- [[https://github.com/manateelazycat/mind-wave][mind-wave]]
- [[https://github.com/f/awesome-chatgpt-prompts][awesome-chatgpt-prompts]]