Last active
May 28, 2025 05:06
-
-
Save jwiegley/d087f04f0a221e3543dd54b7e3677cf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun synchronous (func) | |
"Make any asynchronous function into a synchronous one. | |
FUNC is called with a callback to be called when the asynchronous | |
function is finished. For example, in the case where make-thread | |
is used to run a function asynchronously, which when complete, | |
finishes the synchronous call. | |
(synchronous | |
#'(lambda (k) | |
(make-thread #'(lambda () | |
(sleep-for 3) | |
(funcall k 123)))))" | |
(let* ((mut (make-mutex)) | |
(var (make-condition-variable mut)) | |
(result (cons nil nil))) | |
(funcall func | |
#'(lambda (x) | |
(with-mutex mut | |
(setf (car result) t) | |
(setf (cdr result) x) | |
(condition-notify var)))) | |
(with-mutex mut | |
(while (null (car result)) | |
(sleep-for 0 100) | |
(condition-wait var)))' | |
(cdr result))) | |
(cl-defun gptel-request-synchronous | |
(&optional prompt &key callback | |
(buffer (current-buffer)) | |
position context dry-run | |
(stream nil) (in-place nil) | |
(system gptel--system-message) | |
transforms (fsm (gptel-make-fsm))) | |
(synchronous | |
#'(lambda (komplete) | |
(gptel-request | |
prompt | |
:callback | |
#'(lambda (response info) | |
(funcall callback response info) | |
(unless (stringp response) | |
(funcall komplete response))) | |
:buffer buffer | |
:position position | |
:context context | |
:dry-run dry-run | |
:stream stream | |
:in-place in-place | |
:system system | |
:transforms transforms | |
:fsm fsm)))) | |
(defun llm (prompt) | |
(with-temp-buffer | |
(gptel-request-synchronous | |
prompt | |
:callback `(lambda (response _info) | |
(when (stringp response) | |
(with-current-buffer ,(current-buffer) | |
(insert response))))) | |
(buffer-string))) | |
(when nil | |
(llm "What is your name? /no_think")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment