Skip to content
Snippets Groups Projects
Commit 53372985 authored by ale's avatar ale
Browse files

Make copilot.el talk to the indexer service over HTTP

This greatly reduces latency due to not having to start the Python
interpreter every time (the dependencies take a while to load).
parent a0134976
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@
:group 'copilot)
(defcustom ollama-model
"qwen2.5-coder:32b"
"qwen2.5-coder:14b"
"Model name."
:type 'string
:group 'copilot)
......@@ -24,36 +24,65 @@
:type 'string
:group 'copilot)
;;;###autoload
(defun copilot-complete ()
(interactive)
(defcustom src-context-uri
"http://localhost:7894"
"URI for the ecopilot-src-context HTTP endpoint."
:type 'string
:group 'copilot)
(defun copilot-json-request-source-context (rootdir codefile)
(let ((url-request-method "POST")
(url-request-data (json-encode-plist (list "git_root" rootdir "chat_fnames" (list codefile))))
(url-request-extra-headers '(("Content-Type" . "application/json"))))
(with-current-buffer
(url-retrieve-synchronously src-context-uri)
(prog1
(buffer-substring url-http-end-of-headers (point-max))
(kill-buffer)))))
(defun copilot-source-context ()
(let* ((spot (point))
(inhibit-quit t)
(curfile (buffer-file-name))
(lang (file-name-extension curfile))
(rootdir (projectile-project-root))
;; (rootdir (projectile-project-root))
(rootdir default-directory)
(codefile (concat curfile ".ecopilot_snippet." lang))
(promptfile (concat curfile ".prompt"))
;; extract the entire buffer up to the cursor as the
;; completion prompt.
(code (save-excursion
(beginning-of-buffer)
(buffer-substring-no-properties (point) spot))))
(prog2
(write-region code nil codefile 'silent)
(copilot-json-request-source-context rootdir codefile)
(delete-file codefile))))
;;;###autoload
(defun copilot-complete ()
(interactive)
(let* ((spot (point))
(inhibit-quit t)
(curfile (buffer-file-name))
(lang (file-name-extension curfile))
(promptfile (concat curfile ".prompt"))
(code (save-excursion
(beginning-of-buffer)
(buffer-substring-no-properties (point) spot)))
(let* (
;; create new prompt for this interaction
(system "\
You are an Emacs code generator. \
Writing comments is forbidden. \
Writing test code is forbidden. \
Writing English explanations is forbidden. ")
Writing English explanations and notes is forbidden. \
You can use the provided code snippets as additional examples and reference. \
")
;; expand context relevant to the current source file
(context (shell-command-to-string (concat src-context-bin " --repo=" rootdir " " codefile)))
(context (copilot-source-context))
(prompt (format
"%s\nConsider the following references and context:\n%s\n\nGenerate %s code to complete:\n```%s\n%s"
"%s\n\
Reference snippets:\n%s\n\n\n\
Only output the missing parts of the code. \
Generate %s code to complete the following snippet:\n```%s\n%s"
system context lang lang code)))
;; append prompt for current interaction to the big old prompt
......@@ -75,11 +104,10 @@ Writing English explanations is forbidden. ")
(goto-char spot)
(while (re-search-forward "```[a-z]*" end t)
(setq end (- end (length (match-string 0))))
(replace-match "")))))
(replace-match ""))))
;; remove temporary files
(delete-file promptfile)
(delete-file codefile)))
(delete-file promptfile)))
;; define `ctrl-c ctrl-k` keybinding for llm code completion
(defun copilot-c-hook ()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment