(in-package #:cl-openai) (defun chat/completion-messages-to-prompts (model messages) (cl-json:encode-json-to-string (remove nil `(("model" . ,model) ("messages" . ,messages))))) (defun prompt (model messages) (let ((json-data (chat/completion-messages-to-prompts model messages))) (json-decode-http-request-to-string (server-path "v1/chat/completions") :content json-data :method :post))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #| (defclass chat-completion () ((model :initarg :model :accessor model) (messages :initarg :messages :accessor messages) (response :initarg :response :accessor response))) (defun create-chat-completion (model messages) (make-instance 'chat-completion :model model :messages messages :response nil)) (defun add-user-message (chat-completion user-message) (push `(:role "user" :content ,user-message) (messages chat-completion))) (defun add-assistant-response (chat-completion assistant-response) (setf (response chat-completion) assistant-response) (push `(:role "assistant" :content ,assistant-response) (messages chat-completion))) (defun chat/completion-messages-to-prompts (chat-completion) (let ((model (model chat-completion)) (messages (messages chat-completion))) (cl-json:encode-json-to-string (remove nil `(("model" . ,model) ("messages" . ,messages)))))) (defun prompt (chat-completion) (let ((json-data (chat/completion-messages-to-prompts chat-completion))) (json-decode-http-request-to-string (server-path "v1/chat/completions") :content json-data :method :post))) ;; Example usage: (let ((chat (create-chat-completion "gpt-4o-mini" '((:role "system" :content "You are a helpful assistant."))))) (add-user-message chat "What is a LLM?") (let ((response (prompt chat))) (add-assistant-response chat response)) (add-user-message chat "Who won the world series in 2020?") (let ((response (prompt chat))) (add-assistant-response chat response)) (add-user-message chat "Where was it played?") (let ((response (prompt chat))) (add-assistant-response chat response)) ;; Now you can access the chat history and responses (format t "Chat history: ~a~%" (messages chat)) (format t "Last assistant response: ~a~%" (response chat))) |#