From a1964bd9021c9aac9cff8ac074ecebe4926184ff Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 22 Jul 2024 17:15:29 +0200 Subject: query ai --- elisp/ai-query.el | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 elisp/ai-query.el diff --git a/elisp/ai-query.el b/elisp/ai-query.el new file mode 100644 index 0000000..80c9918 --- /dev/null +++ b/elisp/ai-query.el @@ -0,0 +1,58 @@ +;;; ai-query.el --- Query an AI over the lakera gandalf game -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2024 Óscar Nájera +;; +;; Author: Óscar Nájera +;; Maintainer: Óscar Nájera +;; 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) + "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)) + (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)))) + (with-current-buffer (get-buffer-create "*AI reply*") + (markdown-mode) + (erase-buffer) + (insert "# Question\n" prompt) + (insert "\n\n# Answer\n\n" + (with-current-buffer + (url-retrieve-synchronously ai-query-api) + (goto-char url-http-end-of-headers) + (gethash "answer" (json-parse-buffer)))) + (display-buffer (current-buffer))))) + +(provide 'ai-query) +;;; ai-query.el ends here -- cgit v1.2.3