[go: up one dir, main page]

Skip to content

xhcoding/emacs-aichat

Repository files navigation

AI Chat in Emacs

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

Installation

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)

Bing Chat

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:

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

  • 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

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

  • 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:

(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

  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

  • 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:

(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:

  1. Open the debug mode by typing the command aichat-toggle-debug.
  2. Submit an issue and attach the content of the *AICHAT-DEBUG* buffer when an error occurs.

Acknowledgements