aboutsummaryrefslogtreecommitdiffstats
path: root/elisp/ai-query.el
diff options
context:
space:
mode:
Diffstat (limited to 'elisp/ai-query.el')
-rw-r--r--elisp/ai-query.el64
1 files changed, 64 insertions, 0 deletions
diff --git a/elisp/ai-query.el b/elisp/ai-query.el
new file mode 100644
index 0000000..3bc621c
--- /dev/null
+++ b/elisp/ai-query.el
@@ -0,0 +1,64 @@
+;;; ai-query.el --- Query an AI over the lakera gandalf game -*- lexical-binding: t; -*-
+;;
+;; Copyright (C) 2024 Óscar Nájera
+;;
+;; Author: Óscar Nájera <hi@oscarnajera.com>
+;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
+;; Created: July 22, 2024
+;; Modified: July 22, 2024
+;; Version: 0.0.1
+;; Package-Requires: ((emacs "27.1"))
+;;
+;; This file is not part of GNU Emacs.
+;;
+;;; Commentary:
+;;
+;; Query an AI over the lakera gandalf game
+;;
+;;; Code:
+
+(require 'subr-x)
+(require 'markdown-mode)
+
+;; Silence byte-compiler.
+(defvar url-http-end-of-headers)
+
+(defconst ai-query-api
+ "https://gandalf.lakera.ai/api/send-message"
+ "AI security endpoint, use it for queries.")
+
+(defun ai-query--request (prompt callback)
+ "Async request to API using PROMPT and process with CALLBACK."
+ (let ((url-request-method "POST")
+ (url-request-extra-headers
+ '(("Content-Type" . "application/x-www-form-urlencoded")))
+ (url-request-data
+ (thread-first
+ (list 'prompt prompt)
+ (cons '((defender "baseline")))
+ (url-build-query-string))))
+ (url-retrieve ai-query-api callback)))
+
+(defun ai-query--process-response (_status)
+ "Callback function to process response."
+ (goto-char url-http-end-of-headers)
+ (let ((response (json-parse-buffer)))
+ (with-current-buffer (get-buffer-create "*AI reply*")
+ (markdown-mode)
+ (erase-buffer)
+ (insert "# Question\n" (gethash "prompt" response))
+ (insert "\n\n# Answer\n\n" (gethash "answer" response))
+ (display-buffer (current-buffer)))))
+
+(defun ai-query-request (prompt)
+ "Query the API with this PROMPT."
+ (thread-first
+ (if (use-region-p)
+ (buffer-substring-no-properties (region-beginning) (region-end))
+ (read-string "What is your question? "))
+ (list)
+ (interactive))
+ (ai-query--request prompt #'ai-query--process-response))
+
+(provide 'ai-query)
+;;; ai-query.el ends here