Skip to content

Commit b3906fd

Browse files
committed
fixed some build and logic errors
1 parent 5f0fef5 commit b3906fd

File tree

5 files changed

+70
-35
lines changed

5 files changed

+70
-35
lines changed

src/cl-llm-agent/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ maintain context, and interface with different LLM back-ends.
1111
- Testing suite with FiveAM
1212

1313
## Installation
14+
15+
Define Gemini model to use:
16+
17+
export GEMINI_MODEL=gemini-2.5-pro
18+
1419
1. Clone the repository.
1520
2. In your Lisp REPL:
1621
```lisp

src/cl-llm-agent/agent-gemini.lisp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
;;; agent-gemini.lisp -- Gemini-based agent implementation
22
(in-package :cl-llm-agent)
33

4+
;; Default model used when none is provided explicitly.
5+
(defparameter *default-gemini-model-id* "gemini-pro"
6+
"Fallback Gemini model identifier used by the agent.")
7+
8+
(defun normalize-gemini-model-id (model-id)
9+
"Ensure MODEL-ID uses the format expected by gemini:generate."
10+
(let ((id (or model-id *default-gemini-model-id*)))
11+
(if (uiop:string-prefix-p "models/" id)
12+
(subseq id (length "models/"))
13+
id)))
14+
15+
(defun default-gemini-model-id ()
16+
"Return the Gemini model identifier to use for new agents."
17+
(normalize-gemini-model-id (uiop:getenv "GEMINI_MODEL")))
18+
419
;; Define a specialized agent using the Gemini LLM back-end
5-
(defclass gemini-agent (base-agent) ()
20+
(defclass gemini-agent (base-agent)
21+
((model-id :initarg :model-id
22+
:accessor gemini-agent-model-id
23+
:initform (default-gemini-model-id)
24+
:documentation "Gemini model identifier to use for LLM calls."))
625
(:documentation "Agent using Gemini LLM back-end."))
726

827
(defmethod agent-llm-call ((agent gemini-agent) prompt)
928
"Perform an LLM call for GEMINI-AGENT using gemini:generate."
10-
(declare (ignore agent))
11-
(gemini:generate prompt))
29+
(gemini:generate (normalize-gemini-model-id (gemini-agent-model-id agent))
30+
prompt))

src/cl-llm-agent/agent-generic.lisp

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
(error "Failed to parse JSON: ~A" json-string)))
1515

1616
(defun strip-markdown-json (text)
17-
"Remove markdown fences around JSON in TEXT."
18-
(let ((trimmed (string-trim '(#\Space #\Newline #\Tab #\Return) text)))
19-
(if (and (search "```json" trimmed)
20-
(search "```" trimmed :start2 (1+ (search "```json" trimmed))))
21-
(subseq trimmed
22-
(search "```json" trimmed)
23-
(search "```" trimmed :start2 (1+ (search "```json" trimmed))))
17+
"Remove markdown fences around JSON in TEXT."
18+
(let* ((trimmed (string-trim '(#\Space #\Newline #\Tab #\Return) text))
19+
(start (search "```json" trimmed)))
20+
(if (and start (search "```" trimmed :start2 (1+ start)))
21+
(let ((json-start (+ start 7))
22+
(json-end (search "```" trimmed :start2 (1+ start))))
23+
(subseq trimmed json-start json-end))
2424
trimmed)))
2525

2626
;; Macro to define new agent classes
@@ -49,7 +49,7 @@
4949

5050
;; LLM back-end generic
5151
(defgeneric agent-llm-call (agent prompt)
52-
"Perform an LLM call with AGENT given PROMPT.")
52+
(:documentation "Perform an LLM call with AGENT given PROMPT."))
5353

5454
(defun make-agent (agent-type &rest initargs &key context)
5555
"Construct an agent of AGENT-TYPE with INITARGS and optional CONTEXT."
@@ -67,25 +67,36 @@
6767
(display-context (agent-context agent) "Context start:"))
6868
(let* ((tools-info (with-output-to-string (out)
6969
(format out "tools:~%")
70-
(dolist (t (list-tools))
70+
(dolist (tool (list-tools))
7171
(format out " ~A: ~A~%"
72-
(getf t :name)
73-
(getf t :description)))))
72+
(getf tool :name)
73+
(getf tool :description)))))
7474
(prompt (format nil "~A~%User Input: ~A~%~%If using tools, respond in JSON." tools-info user-input))
7575
(raw (agent-llm-call agent prompt))
7676
(clean (strip-markdown-json raw))
77-
(parsed (safe-parse-json clean))
78-
(actions (if (getf parsed :actions)
79-
(getf parsed :actions)
80-
(list parsed))))
81-
(when *agent-verbose*
82-
(format t "[agent-converse] raw: ~A~%clean: ~A~%actions: ~A~%" raw clean actions))
83-
(loop with prev = nil
84-
for action across actions
85-
for name = (getf action :action)
86-
for params = (getf action :parameters)
87-
do (setf prev (apply #'execute-tool name
88-
(map 'list (lambda (p)
89-
(if (string-equal p "PREV_RESULT") prev p))
90-
params)))
91-
finally (return (or prev (format nil "~A" raw)))))
77+
(parsed (handler-case
78+
(safe-parse-json clean)
79+
(error (err)
80+
(when *agent-verbose*
81+
(format t "[agent-converse] JSON parse error: ~A~%" err))
82+
nil))))
83+
(unless (and parsed
84+
(or (getf parsed :actions)
85+
(getf parsed :action)))
86+
(when *agent-verbose*
87+
(format t "[agent-converse] returning raw response: ~A~%" clean))
88+
(return-from agent-converse clean))
89+
(let ((actions (if (getf parsed :actions)
90+
(getf parsed :actions)
91+
(list parsed))))
92+
(when *agent-verbose*
93+
(format t "[agent-converse] raw: ~A~%clean: ~A~%actions: ~A~%" raw clean actions))
94+
(loop with prev = nil
95+
for action across actions
96+
for name = (getf action :action)
97+
for params = (getf action :parameters)
98+
do (setf prev (apply #'execute-tool name
99+
(map 'list (lambda (p)
100+
(if (string-equal p "PREV_RESULT") prev p))
101+
params)))
102+
finally (return (or prev (format nil "~A" raw)))))))

src/cl-llm-agent/context.lisp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
(make-instance 'context))
1212

1313
(defgeneric context-set (ctx key value)
14-
"Set KEY to VALUE in CTX.")
14+
(:documentation "Set KEY to VALUE in CTX."))
1515
(defmethod context-set ((ctx context) key value)
1616
(setf (gethash key (context-data ctx)) value))
1717

1818
(defgeneric context-get (ctx key)
19-
"Retrieve the value for KEY from CTX.")
19+
(:documentation "Retrieve the value for KEY from CTX."))
2020
(defmethod context-get ((ctx context) key)
2121
(gethash key (context-data ctx)))
2222

2323
(defgeneric context-remove (ctx key)
24-
"Remove KEY from CTX.")
24+
(:documentation "Remove KEY from CTX."))
2525
(defmethod context-remove ((ctx context) key)
2626
(remhash key (context-data ctx)))
2727

src/cl-llm-agent/tools.lisp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
(or (char= (char n 0) #\#)
5050
(char= (char (aref n (1- (length n))) #\~)))
5151
(uiop:directory-files path))
52-
(error "Directory not found: ~A" path))))
52+
(error "Directory not found: ~A" path)))))
5353

5454
(register-tool "tool-read-directory"
5555
:description "Reads the contents of a directory."
@@ -81,10 +81,10 @@
8181
(defun helper-summarize (text)
8282
"Summarize TEXT using Gemini LLM backend."
8383
(let ((prompt (format nil "Summarize the following text:~%~A~%" text)))
84-
(gemini:generate prompt)))
84+
(gemini:generate (default-gemini-model-id) prompt)))
8585

8686
(register-tool "tool-summarize"
8787
:description "Summarize text using Gemini."
8888
:parameters '(text)
8989
:parameter-example "text: string"
90-
:function #'helper-summarize)
90+
:function #'helper-summarize)

0 commit comments

Comments
 (0)