branch: externals/llm commit 40151757ded8fc8a8c1312da8f80d56968e21c22 Author: Andrew Hyatt <ahy...@gmail.com> Commit: Andrew Hyatt <ahy...@gmail.com>
Switch to a method of nonfree warnings easier for provider modules Also, change how warnings are logged, since list types do not currently work. --- llm-openai.el | 7 +++---- llm-vertex.el | 7 +++---- llm.el | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/llm-openai.el b/llm-openai.el index 76d6ab45cd..70d0836e89 100644 --- a/llm-openai.el +++ b/llm-openai.el @@ -50,15 +50,15 @@ EMBEDDING-MODEL is the model to use for embeddings. If unset, it will use a reasonable default." key chat-model embedding-model) -(defun llm-openai--maybe-warn () - (llm--warn-on-nonfree "Open AI" "https://openai.com/policies/terms-of-use")) +(cl-defmethod llm-nonfree-message-info ((provider llm-openai)) + (ignore provider) + (cons "Open AI" "https://openai.com/policies/terms-of-use")) (defun llm-openai--embedding-make-request (provider string vector-callback error-callback sync) "Make a request to Open AI to get an embedding for STRING. PROVIDER, VECTOR-CALLBACK and ERROR-CALLBACK are as in the `llm-embedding-async' call. SYNC is non-nil when the request should wait until the response is received." - (llm-openai--maybe-warn) (unless (llm-openai-key provider) (error "To call Open AI API, add a key to the `llm-openai' provider.")) (request "https://api.openai.com/v1/embeddings" @@ -102,7 +102,6 @@ ERROR-CALLBACK is called if there is an error, with the error signal and message. SYNC is non-nil when the request should wait until the response is received." - (llm-openai--maybe-warn) (unless (llm-openai-key provider) (error "To call Open AI API, the key must have been set")) (let (request-alist system-prompt) diff --git a/llm-vertex.el b/llm-vertex.el index 3fdd50c245..11543a4ca5 100644 --- a/llm-vertex.el +++ b/llm-vertex.el @@ -69,8 +69,9 @@ KEY-GENTIME keeps track of when the key was generated, because the key must be r (setf (llm-vertex-key provider) result)) (setf (llm-vertex-key-gentime provider) (current-time)))) -(defun llm-vertex-maybe-warn () - (llm--warn-on-nonfree "Google Cloud Vertex" "https://policies.google.com/terms/generative-ai")) +(cl-defmethod llm-nonfree-message-info ((provider llm-vertex)) + (ignore provider) + (cons "Google Cloud Vertex" "https://policies.google.com/terms/generative-ai")) (defun llm-vertex--embedding (provider string vector-callback error-callback sync) "Get the embedding for STRING. @@ -78,7 +79,6 @@ PROVIDER, VECTOR-CALLBACK, ERROR-CALLBACK are all the same as `llm-embedding-async'. SYNC, when non-nil, will wait until the response is available to return." (llm-vertex-refresh-key provider) - (llm-vertex-maybe-warn) (request (format "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:predict" llm-vertex-gcloud-region (llm-vertex-project provider) @@ -116,7 +116,6 @@ PROVIDER, RESPONSE-CALLBACK, ERROR-CALLBACK are all the same as `llm-chat-async'. SYNC, when non-nil, will wait until the response is available to return." (llm-vertex-refresh-key provider) - (llm-vertex-maybe-warn) (let ((request-alist)) (when (llm-chat-prompt-context prompt) (push `("context" . ,(llm-chat-prompt-context prompt)) request-alist)) diff --git a/llm.el b/llm.el index 5d6202b18b..4e78678383 100644 --- a/llm.el +++ b/llm.el @@ -57,7 +57,7 @@ TOS is the URL of the terms of service for the LLM. All non-free LLMs should call this function on each llm function invocation." (when llm-warn-on-nonfree - (lwarn '(llm nonfree) :warning "%s API is not free software, and your freedom to use it is restricted. + (lwarn 'llm :warning "%s API is not free software, and your freedom to use it is restricted. See %s for the details on the restrictions on use." name tos))) (cl-defstruct llm-chat-prompt @@ -96,6 +96,17 @@ an LLM, and don't need the more advanced features that the `llm-chat-prompt' struct makes available." (make-llm-chat-prompt :interactions (list (make-llm-chat-prompt-interaction :role 'user :content text)))) +(cl-defgeneric llm-nonfree-message-info (provider) + "If PROVIDER is non-free, return info for a warning. +This should be a cons of the name of the LLM, and the URL of the +terms of service. + +If the LLM is free and has no restrictions on use, this should +return nil. Since this function already returns nil, there is no +need to override it." + (ignore provider) + nil) + (cl-defgeneric llm-chat (provider prompt) "Return a response to PROMPT from PROVIDER. PROMPT is a `llm-chat-prompt'. The response is a string." @@ -105,6 +116,11 @@ PROMPT is a `llm-chat-prompt'. The response is a string." (cl-defmethod llm-chat ((_ (eql nil)) _) (error "LLM provider was nil. Please set the provider in the application you are using.")) +(cl-defmethod llm-chat :before (provider _ _ _) + "Issue a warning if the LLM is non-free." + (when-let (info (llm-nonfree-message-info provider)) + (llm--warn-on-nonfree (car info) (cdr info)))) + (cl-defgeneric llm-chat-async (provider prompt response-callback error-callback) "Return a response to PROMPT from PROVIDER. PROMPT is a `llm-chat-prompt'. @@ -116,6 +132,11 @@ ERROR-CALLBACK receives the error response." (cl-defmethod llm-chat-async ((_ (eql nil)) _ _ _) (error "LLM provider was nil. Please set the provider in the application you are using.")) +(cl-defmethod llm-chat-async :before (provider _ _ _) + "Issue a warning if the LLM is non-free." + (when-let (info (llm-nonfree-message-info provider)) + (llm--warn-on-nonfree (car info) (cdr info)))) + (cl-defgeneric llm-embedding (provider string) "Return a vector embedding of STRING from PROVIDER." (ignore provider string) @@ -124,6 +145,11 @@ ERROR-CALLBACK receives the error response." (cl-defmethod llm-embedding ((_ (eql nil)) _) (error "LLM provider was nil. Please set the provider in the application you are using.")) +(cl-defmethod llm-embedding :before (provider _) + "Issue a warning if the LLM is non-free." + (when-let (info (llm-nonfree-message-info provider)) + (llm--warn-on-nonfree (car info) (cdr info)))) + (cl-defgeneric llm-embedding-async (provider string vector-callback error-callback) "Calculate a vector embedding of STRING from PROVIDER. VECTOR-CALLBACK will be called with the vector embedding. @@ -135,6 +161,11 @@ error signal and a string message." (cl-defmethod llm-embedding-async ((_ (eql nil)) _ _ _) (error "LLM provider was nil. Please set the provider in the application you are using.")) +(cl-defmethod llm-embedding-async :before (provider _ _ _) + "Issue a warning if the LLM is non-free." + (when-let (info (llm-nonfree-message-info provider)) + (llm--warn-on-nonfree (car info) (cdr info)))) + (cl-defgeneric llm-count-tokens (provider string) "Return the number of tokens in STRING from PROVIDER. This may be an estimate if the LLM does not provide an exact