Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (setq pdf "x.pdf")
- (defun upload-pdf (path)
- "DOCSTRING"
- (interactive)
- (let* ((cmd (append (list "curl" "https://api.openai.com/v1/files"
- "--fail-with-body"
- "--no-progress-meter"
- "-H" (funcall chatgpt-shell-auth-header)
- "-F" "purpose=assistants"
- "-F" (concat "file=@" path)
- )
- ))
- (result (shell-maker--execute-command-sync
- :command cmd
- ))
- )
- (if (equal 0 (map-elt result :exit-status))
- (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
- (error "error: %s" (map-elt result :output)))
- ))
- (setq file-id (or file-id (upload-pdf pdf)))
- (defun create-pdf-assistant ()
- "DOCSTRING"
- (interactive)
- (let ((result (shell-maker-make-http-request
- :url "https://api.openai.com/v1/assistants"
- :data `((name . "PDF Document Assistant")
- (instructions . "You are an expert assistant. Use the uploaded PDF to answer questions about its contents.")
- (tools . [,(list (cons 'type "file_search"))])
- (model . "gpt-4o"))
- :headers (list "Content-Type: application/json"
- "OpenAI-Beta: assistants=v2"
- (funcall chatgpt-shell-auth-header))
- ))
- )
- (if (equal 0 (map-elt result :exit-status))
- (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
- (error "error: %s" (map-elt result :output)))
- )
- )
- (setq assistant-id (or assistant-id (create-pdf-assistant)))
- (defun create-thread ()
- "DOCSTRING"
- (interactive)
- (let* ((cmd (append
- (shell-maker-make--curl-command
- :url "https://api.openai.com/v1/threads"
- :headers (list "Content-Type: application/json"
- "OpenAI-Beta: assistants=v2"
- (funcall chatgpt-shell-auth-header))
- )
- (list "-d" "")
- ))
- (result (shell-maker--execute-command-sync
- :command cmd
- ))
- )
- (if (equal 0 (map-elt result :exit-status))
- (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
- (error "error: %s" (map-elt result :output)))
- ))
- (defun create-message (file-id thread-id prompt)
- "DOCSTRING"
- (interactive)
- (let* ((cmd (shell-maker-make--curl-command
- :url (format "https://api.openai.com/v1/threads/%s/messages" thread-id)
- :headers (list "Content-Type: application/json"
- "OpenAI-Beta: assistants=v2"
- (funcall chatgpt-shell-auth-header))
- :data `((role . "user")
- (content . ,prompt)
- (attachments . [(
- ,(cons 'file_id file-id)
- (tools . [,(list (cons 'type "file_search"))])
- )])
- )
- ))
- (result (shell-maker--execute-command-sync
- :command cmd
- ))
- )
- (if (equal 0 (map-elt result :exit-status))
- (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
- (error "error: %s" (map-elt result :output)))
- ))
- (cl-defun run-thread (&key thread-id assistant-id version temperature streaming other-params)
- "DOCSTRING"
- (interactive)
- (let* ((data (append
- `((assistant_id . ,assistant-id)
- (model . ,(or version (chatgpt-shell-model-version))))
- (when temperature
- `((temperature . ,temperature)))
- (when streaming
- `((stream . t)))
- other-params))
- (cmd (shell-maker-make--curl-command
- :url (format "https://api.openai.com/v1/threads/%s/runs" thread-id)
- :headers (list "Content-Type: application/json"
- "OpenAI-Beta: assistants=v2"
- (funcall chatgpt-shell-auth-header))
- :data data
- ))
- (result (shell-maker--execute-command-sync :command cmd))
- )
- (if (equal 0 (map-elt result :exit-status))
- (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
- (error "error: %s" (map-elt result :output)))
- ))
- (cl-defun get-run (&key thread-id run-id)
- "DOCSTRING"
- (interactive)
- (let* ((cmd (shell-maker-make--curl-command
- :url (format "https://api.openai.com/v1/threads/%s/runs/%s" thread-id run-id)
- :headers (list "Content-Type: application/json"
- "OpenAI-Beta: assistants=v2"
- (funcall chatgpt-shell-auth-header))
- ))
- (result (shell-maker--execute-command-sync :command cmd))
- )
- (if (equal 0 (map-elt result :exit-status))
- (shell-maker--json-parse-string (map-elt result :output))
- (error "error: %s" (map-elt result :output)))
- ))
- (cl-defun get-run-status (&key thread-id run-id)
- "DOCSTRING"
- (interactive)
- (let-alist
- (get-run :thread-id thread-id :run-id run-id)
- .status)
- )
- (cl-defun wait-run-complete (&key thread-id run-id)
- "DOCSTRING"
- (interactive)
- (let ((status "queued"))
- (while (or (string= status "queued")
- (string= status "in_progress")
- (string= status "cancelling"))
- (setq status
- (get-run-status
- :thread-id thread-id
- :run-id run-id
- ))
- )
- status
- )
- )
- (defun retrieve-thread (thread-id)
- "DOCSTRING"
- (interactive)
- (let* ((cmd (shell-maker-make--curl-command
- :url (format "https://api.openai.com/v1/threads/%s/messages" thread-id)
- :headers (list "Content-Type: application/json"
- "OpenAI-Beta: assistants=v2"
- (funcall chatgpt-shell-auth-header))
- ))
- )
- (shell-maker--execute-command-sync :command cmd )
- ))
- (defun messages-of-thread (thread-id)
- "DOCSTRING"
- (interactive)
- (let ((result (retrieve-thread thread-id)))
- (if (equal 0 (map-elt result :exit-status))
- (let-alist (shell-maker--json-parse-string (map-elt result :output)) .data)
- (error "error: %s" (map-elt result :output)))
- )
- )
- (defun get-message-content (msg)
- "DOCSTRING"
- (interactive)
- (->
- msg
- (let-alist .content)
- (seq-first)
- (let-alist .text)
- (let-alist .value)
- )
- )
- (defun latest-message-of-thread (thread-id)
- "DOCSTRING"
- (interactive)
- (let ((messages (messages-of-thread thread-id)))
- (message "messages: %s" messages)
- (get-message-content (seq-first messages))
- )
- )
- (cl-defun chat (&key file-id assistant-id thread-id prompt version temperature streaming other-params)
- "DOCSTRING"
- (interactive)
- (let ((thread-id (or thread-id (create-thread)))
- (assistant-id (or assistant-id (create-pdf-assistant)))
- run-id
- )
- (message "thread-id: %s" thread-id)
- (create-message file-id thread-id prompt)
- (setq run-id
- (run-thread
- :thread-id thread-id
- :assistant-id assistant-id
- :version version
- :temperature temperature
- :streaming streaming
- :other-params other-params
- ))
- (message "run-id: %s" run-id)
- (if (string= (wait-run-complete :thread-id thread-id :run-id run-id)
- "completed")
- (message "%s" (latest-message-of-thread thread-id))
- (error "error: %s" (get-run :thread-id thread-id :run-id run-id))
- )
- )
- )
- (chat
- :file-id file-id
- :assistant-id assistant-id
- :prompt "Please summarize the given PDF"
- :version "gpt-4o"
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement