[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/ellama e81296b75d 3/4: Merge pull request #71 from s-ko
From: |
ELPA Syncer |
Subject: |
[elpa] externals/ellama e81296b75d 3/4: Merge pull request #71 from s-kostyaev/add-naming-scheme |
Date: |
Sun, 11 Feb 2024 15:57:59 -0500 (EST) |
branch: externals/ellama
commit e81296b75d5ca5e02f45afc664e4a9298802e45d
Merge: 9c145514f1 61c43dee8c
Author: Sergey Kostyaev <s-kostyaev@users.noreply.github.com>
Commit: GitHub <noreply@github.com>
Merge pull request #71 from s-kostyaev/add-naming-scheme
Implement user option for different naming schemes
---
README.org | 3 +++
ellama.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 67 insertions(+), 2 deletions(-)
diff --git a/README.org b/README.org
index 708a377c98..1502008197 100644
--- a/README.org
+++ b/README.org
@@ -267,6 +267,9 @@ argument generated text string.
comment lines. Default value 100.
- ~ellama-session-auto-save~: Automatically save ellama sessions if
set. Enabled by default.
+- ~ellama-naming-scheme~: How to name new sessions.
+- ~ellama-naming-provider~: LLM provider for generating session names
+ by LLM. If not set ~ellama-provider~ will be used.
** Acknowledgments
diff --git a/ellama.el b/ellama.el
index 311c22f58f..4af67646de 100644
--- a/ellama.el
+++ b/ellama.el
@@ -167,6 +167,23 @@
:group 'ellama
:type 'integer)
+(defcustom ellama-naming-scheme 'ellama-generate-name-by-words
+ "How to name sessions.
+If you choose custom function, that function should accept PROVIDER, ACTION
+and PROMPT arguments.
+
+PROVIDER is an llm provider.
+
+ACTION is a symbol, current command.
+
+PROMPT is a prompt string."
+ :group 'ellama
+ :type `(choice
+ (const :tag "By first N words of prompt"
ellama-generate-name-by-words)
+ (const :tag "By current time" ellama-generate-name-by-time)
+ (const :tag "By generating name with LLM based on prompt."
ellama-generate-name-by-llm)
+ (function :tag "By custom function")))
+
(defcustom ellama-translate-word-prompt-template "Translate %s to %s"
"Promp template for `ellama-translate' with single word."
:group 'ellama
@@ -247,6 +264,17 @@
:group 'ellama
:type 'string)
+(defcustom ellama-get-name-template "I will get you user query, you should
return short topic only, what this conversation about. NEVER respond to query
itself. Topic must be short and concise.
+For example:
+Query: Why is sky blue?
+Topic: Blue sky
+
+Query: %s
+Topic:"
+ "Prompt template for `ellama-get-name'."
+ :group 'ellama
+ :type 'string)
+
(defcustom ellama-chat-done-callback nil
"Callback that will be called on ellama chat response generation done.
It should be a function with single argument generated text string."
@@ -387,6 +415,11 @@ This filter contains only subset of markdown syntax to be
good enough."
:type 'string
:group 'ellama)
+(defcustom ellama-naming-provider nil
+ "LLM provider for generating names."
+ :group 'ellama
+ :type '(sexp :validate 'cl-struct-p))
+
(defvar-local ellama--current-session nil)
(defvar ellama--current-session-id nil)
@@ -411,8 +444,8 @@ CONTEXT contains context for next request."
"Return ellama session buffer by provided ID."
(gethash id ellama--active-sessions))
-(defun ellama-generate-name (provider action prompt)
- "Generate name for ellama ACTION by PROVIDER according to PROMPT."
+(defun ellama-generate-name-by-words (provider action prompt)
+ "Generate name for ACTION by PROVIDER by getting first N words from PROMPT."
(let* ((cleaned-prompt (replace-regexp-in-string "/" "_" prompt))
(prompt-words (split-string cleaned-prompt)))
(string-join
@@ -425,6 +458,35 @@ CONTEXT contains context for next request."
(format "(%s)" (llm-name provider))))
" ")))
+(defun ellama-get-name (prompt)
+ "Generate session name by LLM based on PROMPT."
+ (let ((provider (or ellama-naming-provider ellama-provider)))
+ (string-trim-right
+ (string-trim
+ (llm-chat provider (llm-make-simple-chat-prompt
+ (format ellama-get-name-template prompt))))
+ "\\.")))
+
+(defun ellama-generate-name-by-llm (provider _action prompt)
+ "Generate name for ellama ACTION by PROVIDER and PROMPT by LLM."
+ (format "%s (%s)"
+ (ellama-get-name prompt)
+ (llm-name provider)))
+
+(defun ellama-get-current-time ()
+ "Return string representation of current time."
+ (replace-regexp-in-string
+ "\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)\\'" "\\1:\\2"
+ (format-time-string "%FT%T%z" (current-time))))
+
+(defun ellama-generate-name-by-time (_provider _action _prompt)
+ "Generate name for ellama session by current time."
+ (ellama-get-current-time))
+
+(defun ellama-generate-name (provider action prompt)
+ "Generate name for ellama ACTION by PROVIDER according to PROMPT."
+ (funcall ellama-naming-scheme provider action prompt))
+
(defvar ellama--new-session-context nil)
(defun ellama-new-session (provider prompt &optional ephemeral)