(defungptel-curl--get-args(promptstoken)"Produce list of arguments for calling Curl.
PROMPTS is the data to send, TOKEN is a unique identifier."(let*((args(list"--location""--silent""--compressed""--disable"))(url"https://api.openai.com/v1/chat/completions")(data(encode-coding-string(json-encode(gptel--request-dataprompts))'utf-8))(headers`(("Content-Type"."application/json")("Authorization".,(concat"Bearer "(gptel--api-key))))))(push(format"-X%s""POST")args)(push(format"-w(%s . %%{size_header})"token)args);; (push (format "--keepalive-time %s" 240) args)(push(format"-m%s"60)args)(push"-D-"args)(pcase-dolist(`(,key.,val)headers)(push(format"-H%s: %s"keyval)args))(push(format"-d%s"data)args)(nreverse(consurlargs))))
(defunslack-curl--get-args(promptstoken)"Produce list of arguments for calling Curl.
PROMPTS is the data to send, TOKEN is a unique identifier."(let*((args(list"--location""--silent""--compressed""--disable"))(url"https://slack.com/api/chat.postMessage")(data(encode-coding-string(json-encode(gptel--request-dataprompts))'utf-8))(headers`(("Content-Type"."application/json")("Authorization".,(concat"Bearer "(gptel--api-key))))));;向 crul参数:args 中添加字段(push(format"-X%s""POST")args)(push(format"-w(%s . %%{size_header})"token)args);; (push (format "--keepalive-time %s" 240) args)(push(format"-m%s"60)args)(push"-D-"args);; pcase-dolist essentially循环一个alist或plist,为每个元素执行一些操作(pcase-dolist(`(,key.,val)headers)(push(format"-H%s: %s"keyval)args));;添加 prompt data(push(format"-d%s"data)args)(nreverse(consurlargs))))
;;TODO: The :transformer argument here is an alternate implementation of;;`gptel-response-filter-functions'. The two need to be unified.;;;###autoload(defungptel-curl-get-response(info&optionalcallback);; 1"Retrieve response to prompt in INFO.
INFO is a plist with the following keys:
- :prompt (the prompt being sent)
- :buffer (the gptel buffer)
- :position (marker at which to insert the response).
Call CALLBACK with the response and INFO afterwards. If omitted
the response is inserted into the current buffer after point."(let*((token(md5(format"%s%s%s%s"(random)(emacs-pid)(user-full-name)(recent-keys))));; 3(args(gptel-curl--get-args(plist-getinfo:prompt)token));;(process(apply#'start-process"gptel-curl"(generate-new-buffer"*gptel-curl*")"curl"args)));;4(with-current-buffer(process-bufferprocess)(set-process-query-on-exit-flagprocessnil);;6(setf(alist-getprocessgptel-curl--process-alist)(nconc(list:tokentoken:callback(orcallback(ifgptel-stream#'gptel-curl--stream-insert-response#'gptel--insert-response)):transformer(when(or(eqgptel-default-mode'org-mode)(eq(buffer-local-value'major-mode(plist-getinfo:buffer))'org-mode))(gptel--stream-convert-markdown->org)))info))(ifgptel-stream(progn(set-process-sentinelprocess#'gptel-curl--stream-cleanup)(set-process-filterprocess#'gptel-curl--stream-filter));;8(set-process-sentinelprocess#'gptel-curl--sentinel)))));;9
它定义了一个名为 gptel-curl-get-response 的函数,该函数接受 info plist 和可选的回调 callback 作为参数。
info plist 包含诸如提示、缓冲区、插入响应位置的标记等信息。 callback 将在获得响应后以 response 和 info 为参数进行调用。如果省略,响应将插入当前缓冲区的 point 之后。
;;;###autoload(defunslack-curl-get-response(info&optionalcallback);; 1"Retrieve response to prompt in INFO.
INFO is a plist with the following keys:
- :prompt (the prompt being sent)
- :buffer (the gptel buffer)
- :position (marker at which to insert the response).
Call CALLBACK with the response and INFO afterwards. If omitted
the response is inserted into the current buffer after point."(let*((token(md5(format"%s%s%s%s"(random)(emacs-pid)(user-full-name)(recent-keys))));; 3(args(gptel-curl--get-args(plist-getinfo:prompt)token));;(process(apply#'start-process"gptel-curl"(generate-new-buffer"*gptel-curl*")"curl"args)));;4(with-current-buffer(process-bufferprocess)(set-process-query-on-exit-flagprocessnil);;6(setf(alist-getprocessgptel-curl--process-alist)(nconc(list:tokentoken:callback(orcallback(ifgptel-stream#'gptel-curl--stream-insert-response#'gptel--insert-response)):transformer(when(or(eqgptel-default-mode'org-mode)(eq(buffer-local-value'major-mode(plist-getinfo:buffer))'org-mode))(gptel--stream-convert-markdown->org)))info))(ifgptel-stream(progn(set-process-sentinelprocess#'gptel-curl--stream-cleanup)(set-process-filterprocess#'gptel-curl--stream-filter));;8(set-process-sentinelprocess#'gptel-curl--sentinel)))))
;;;###autoload(defungptel-curl--stream-insert-response(responseinfo)"Insert streaming RESPONSE from ChatGPT into the gptel buffer.
INFO is a mutable plist containing information relevant to this buffer.
See `gptel--url-get-response' for details."(let((status-str(plist-getresponse:status))(start-marker(plist-getinfo:position))(tracking-marker(plist-getinfo:tracking-marker))(transformer(plist-getinfo:transformer)))(whenresponse(with-current-buffer(marker-bufferstart-marker)(save-excursion(message"插入结果222:-----%s \n 状态:%s"responsestatus-str)(unlesstracking-marker(gptel--update-header-line" Typing..."'success)(goto-charstart-marker)(unless(or(bobp)(plist-getinfo:in-place))(insert"\n\n"))(setqtracking-marker(set-marker(make-marker)(point)))(set-marker-insertion-typetracking-markert)(plist-putinfo:tracking-markertracking-marker))(whentransformer(setqresponse(funcalltransformerresponse)))(put-text-property0(lengthresponse)'gptel'responseresponse)(goto-chartracking-marker)(insertresponse))))))