Advertisement
Guest User

rag-demo

a guest
Nov 1st, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. (setq pdf "x.pdf")
  2.  
  3. (defun upload-pdf (path)
  4. "DOCSTRING"
  5. (interactive)
  6. (let* ((cmd (append (list "curl" "https://api.openai.com/v1/files"
  7. "--fail-with-body"
  8. "--no-progress-meter"
  9. "-H" (funcall chatgpt-shell-auth-header)
  10. "-F" "purpose=assistants"
  11. "-F" (concat "file=@" path)
  12. )
  13. ))
  14. (result (shell-maker--execute-command-sync
  15. :command cmd
  16. ))
  17. )
  18. (if (equal 0 (map-elt result :exit-status))
  19. (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
  20. (error "error: %s" (map-elt result :output)))
  21. ))
  22. (setq file-id (or file-id (upload-pdf pdf)))
  23.  
  24. (defun create-pdf-assistant ()
  25. "DOCSTRING"
  26. (interactive)
  27. (let ((result (shell-maker-make-http-request
  28. :url "https://api.openai.com/v1/assistants"
  29. :data `((name . "PDF Document Assistant")
  30. (instructions . "You are an expert assistant. Use the uploaded PDF to answer questions about its contents.")
  31. (tools . [,(list (cons 'type "file_search"))])
  32. (model . "gpt-4o"))
  33. :headers (list "Content-Type: application/json"
  34. "OpenAI-Beta: assistants=v2"
  35. (funcall chatgpt-shell-auth-header))
  36. ))
  37. )
  38. (if (equal 0 (map-elt result :exit-status))
  39. (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
  40. (error "error: %s" (map-elt result :output)))
  41. )
  42. )
  43. (setq assistant-id (or assistant-id (create-pdf-assistant)))
  44.  
  45. (defun create-thread ()
  46. "DOCSTRING"
  47. (interactive)
  48. (let* ((cmd (append
  49. (shell-maker-make--curl-command
  50. :url "https://api.openai.com/v1/threads"
  51. :headers (list "Content-Type: application/json"
  52. "OpenAI-Beta: assistants=v2"
  53. (funcall chatgpt-shell-auth-header))
  54. )
  55. (list "-d" "")
  56. ))
  57. (result (shell-maker--execute-command-sync
  58. :command cmd
  59. ))
  60. )
  61. (if (equal 0 (map-elt result :exit-status))
  62. (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
  63. (error "error: %s" (map-elt result :output)))
  64. ))
  65.  
  66. (defun create-message (file-id thread-id prompt)
  67. "DOCSTRING"
  68. (interactive)
  69. (let* ((cmd (shell-maker-make--curl-command
  70. :url (format "https://api.openai.com/v1/threads/%s/messages" thread-id)
  71. :headers (list "Content-Type: application/json"
  72. "OpenAI-Beta: assistants=v2"
  73. (funcall chatgpt-shell-auth-header))
  74. :data `((role . "user")
  75. (content . ,prompt)
  76. (attachments . [(
  77. ,(cons 'file_id file-id)
  78. (tools . [,(list (cons 'type "file_search"))])
  79. )])
  80. )
  81. ))
  82. (result (shell-maker--execute-command-sync
  83. :command cmd
  84. ))
  85. )
  86. (if (equal 0 (map-elt result :exit-status))
  87. (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
  88. (error "error: %s" (map-elt result :output)))
  89. ))
  90.  
  91. (cl-defun run-thread (&key thread-id assistant-id version temperature streaming other-params)
  92. "DOCSTRING"
  93. (interactive)
  94. (let* ((data (append
  95. `((assistant_id . ,assistant-id)
  96. (model . ,(or version (chatgpt-shell-model-version))))
  97. (when temperature
  98. `((temperature . ,temperature)))
  99. (when streaming
  100. `((stream . t)))
  101. other-params))
  102. (cmd (shell-maker-make--curl-command
  103. :url (format "https://api.openai.com/v1/threads/%s/runs" thread-id)
  104. :headers (list "Content-Type: application/json"
  105. "OpenAI-Beta: assistants=v2"
  106. (funcall chatgpt-shell-auth-header))
  107. :data data
  108. ))
  109. (result (shell-maker--execute-command-sync :command cmd))
  110. )
  111. (if (equal 0 (map-elt result :exit-status))
  112. (let-alist (shell-maker--json-parse-string (map-elt result :output)) .id)
  113. (error "error: %s" (map-elt result :output)))
  114. ))
  115.  
  116. (cl-defun get-run (&key thread-id run-id)
  117. "DOCSTRING"
  118. (interactive)
  119. (let* ((cmd (shell-maker-make--curl-command
  120. :url (format "https://api.openai.com/v1/threads/%s/runs/%s" thread-id run-id)
  121. :headers (list "Content-Type: application/json"
  122. "OpenAI-Beta: assistants=v2"
  123. (funcall chatgpt-shell-auth-header))
  124. ))
  125. (result (shell-maker--execute-command-sync :command cmd))
  126. )
  127. (if (equal 0 (map-elt result :exit-status))
  128. (shell-maker--json-parse-string (map-elt result :output))
  129. (error "error: %s" (map-elt result :output)))
  130. ))
  131.  
  132. (cl-defun get-run-status (&key thread-id run-id)
  133. "DOCSTRING"
  134. (interactive)
  135. (let-alist
  136. (get-run :thread-id thread-id :run-id run-id)
  137. .status)
  138. )
  139.  
  140. (cl-defun wait-run-complete (&key thread-id run-id)
  141. "DOCSTRING"
  142. (interactive)
  143. (let ((status "queued"))
  144. (while (or (string= status "queued")
  145. (string= status "in_progress")
  146. (string= status "cancelling"))
  147. (setq status
  148. (get-run-status
  149. :thread-id thread-id
  150. :run-id run-id
  151. ))
  152. )
  153. status
  154. )
  155. )
  156.  
  157. (defun retrieve-thread (thread-id)
  158. "DOCSTRING"
  159. (interactive)
  160. (let* ((cmd (shell-maker-make--curl-command
  161. :url (format "https://api.openai.com/v1/threads/%s/messages" thread-id)
  162. :headers (list "Content-Type: application/json"
  163. "OpenAI-Beta: assistants=v2"
  164. (funcall chatgpt-shell-auth-header))
  165. ))
  166. )
  167. (shell-maker--execute-command-sync :command cmd )
  168. ))
  169.  
  170. (defun messages-of-thread (thread-id)
  171. "DOCSTRING"
  172. (interactive)
  173. (let ((result (retrieve-thread thread-id)))
  174. (if (equal 0 (map-elt result :exit-status))
  175. (let-alist (shell-maker--json-parse-string (map-elt result :output)) .data)
  176. (error "error: %s" (map-elt result :output)))
  177. )
  178. )
  179.  
  180. (defun get-message-content (msg)
  181. "DOCSTRING"
  182. (interactive)
  183. (->
  184. msg
  185. (let-alist .content)
  186. (seq-first)
  187. (let-alist .text)
  188. (let-alist .value)
  189. )
  190. )
  191.  
  192. (defun latest-message-of-thread (thread-id)
  193. "DOCSTRING"
  194. (interactive)
  195. (let ((messages (messages-of-thread thread-id)))
  196. (message "messages: %s" messages)
  197. (get-message-content (seq-first messages))
  198. )
  199. )
  200.  
  201. (cl-defun chat (&key file-id assistant-id thread-id prompt version temperature streaming other-params)
  202. "DOCSTRING"
  203. (interactive)
  204. (let ((thread-id (or thread-id (create-thread)))
  205. (assistant-id (or assistant-id (create-pdf-assistant)))
  206. run-id
  207. )
  208. (message "thread-id: %s" thread-id)
  209. (create-message file-id thread-id prompt)
  210. (setq run-id
  211. (run-thread
  212. :thread-id thread-id
  213. :assistant-id assistant-id
  214. :version version
  215. :temperature temperature
  216. :streaming streaming
  217. :other-params other-params
  218. ))
  219. (message "run-id: %s" run-id)
  220. (if (string= (wait-run-complete :thread-id thread-id :run-id run-id)
  221. "completed")
  222. (message "%s" (latest-message-of-thread thread-id))
  223. (error "error: %s" (get-run :thread-id thread-id :run-id run-id))
  224. )
  225. )
  226. )
  227.  
  228. (chat
  229. :file-id file-id
  230. :assistant-id assistant-id
  231. :prompt "Please summarize the given PDF"
  232. :version "gpt-4o"
  233. )
  234.  
  235.  
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement