|
172 | 172 | (defn get-prompts
|
173 | 173 | "run extractors and then render prompt templates
|
174 | 174 | returns ordered collection of chat messages"
|
175 |
| - [{:keys [prompts user platform] :as opts}] |
| 175 | + [{:keys [parameters prompts user platform] :as opts}] |
176 | 176 | (let [;; TODO the docker default no longer makes sense here
|
177 |
| - m (run-extractors opts) |
| 177 | + m (merge (run-extractors opts) parameters) |
178 | 178 | renderer (partial selma-render prompts (facts m user platform))
|
179 | 179 | prompts (if (fs/directory? prompts)
|
180 | 180 | (->> (fs/list-dir prompts)
|
|
207 | 207 | (->> (filter #(= function-name (-> % :function :name)) functions)
|
208 | 208 | first)
|
209 | 209 | :function)]
|
210 |
| - (try |
211 |
| - (cond |
212 |
| - (:container definition) ;; synchronous call to container function |
213 |
| - (let [arg-context (merge |
214 |
| - ;; TODO raw is a bad name when merging |
215 |
| - {:raw (if json-arg-string |
216 |
| - json-arg-string |
217 |
| - "{}")} |
218 |
| - (when json-arg-string (json/parse-string json-arg-string true))) |
219 |
| - function-call (merge |
220 |
| - (:container definition) |
221 |
| - (dissoc opts :functions) |
222 |
| - {:command (into [] |
223 |
| - (concat |
224 |
| - [] |
225 |
| - (->> |
226 |
| - (-> definition :container :command) |
227 |
| - (map (partial interpolate arg-context)) |
228 |
| - (into []))))} |
229 |
| - (when user {:user user}) |
230 |
| - (when pat {:pat pat}) |
231 |
| - (when timeout {:timeout timeout})) |
232 |
| - {:keys [pty-output exit-code done] :as result} (docker/run-function function-call)] |
233 |
| - (cond |
234 |
| - (and (= :exited done) (= 0 exit-code)) |
235 |
| - (resolve pty-output) |
236 |
| - (and (= :exited done) (not= 0 exit-code)) |
237 |
| - (fail (format "call exited with non-zero code (%d): %s" exit-code pty-output)) |
238 |
| - (= :timeout done) |
239 |
| - (fail (format "call timed out: %s" (:timeout result))) |
240 |
| - :else |
241 |
| - (fail (format "call failed")))) |
242 |
| - (= "prompt" (:type definition)) ;; asynchronous call to another agent - new conversation-loop |
243 |
| - ;; TODO set a custom map for prompts in the next conversation loop |
244 |
| - (do |
245 |
| - (jsonrpc/notify :message {:content (format "## (%s) sub-prompt" (:ref definition))}) |
246 |
| - (let [{:keys [messages _finish-reason]} |
247 |
| - (async/<!! (conversation-loop |
248 |
| - (assoc opts :prompts (git/prompt-file (:ref definition)))))] |
249 |
| - (jsonrpc/notify :message {:content (format "## (%s) end sub-prompt" (:ref definition))}) |
250 |
| - (resolve (->> messages |
251 |
| - (filter #(= "assistant" (:role %))) |
252 |
| - (last) |
253 |
| - :content)))) |
254 |
| - :else |
255 |
| - (fail (format "bad container definition %s" definition))) |
256 |
| - (catch Throwable t |
257 |
| - (fail (format "system failure %s" t)))) |
| 210 | + (let [arg-context (merge |
| 211 | + ;; TODO raw is a bad name when merging |
| 212 | + {:raw (if json-arg-string |
| 213 | + json-arg-string |
| 214 | + "{}")} |
| 215 | + (when json-arg-string (json/parse-string json-arg-string true)))] |
| 216 | + (try |
| 217 | + (cond |
| 218 | + (:container definition) ;; synchronous call to container function |
| 219 | + (let [function-call (merge |
| 220 | + (:container definition) |
| 221 | + (dissoc opts :functions) |
| 222 | + {:command (into [] |
| 223 | + (concat |
| 224 | + [] |
| 225 | + (->> |
| 226 | + (-> definition :container :command) |
| 227 | + (map (partial interpolate arg-context)) |
| 228 | + (into []))))} |
| 229 | + (when user {:user user}) |
| 230 | + (when pat {:pat pat}) |
| 231 | + (when timeout {:timeout timeout})) |
| 232 | + {:keys [pty-output exit-code done] :as result} (docker/run-function function-call)] |
| 233 | + (cond |
| 234 | + (and (= :exited done) (= 0 exit-code)) |
| 235 | + (resolve pty-output) |
| 236 | + (and (= :exited done) (not= 0 exit-code)) |
| 237 | + (fail (format "call exited with non-zero code (%d): %s" exit-code pty-output)) |
| 238 | + (= :timeout done) |
| 239 | + (fail (format "call timed out: %s" (:timeout result))) |
| 240 | + :else |
| 241 | + (fail (format "call failed")))) |
| 242 | + |
| 243 | + (= "prompt" (:type definition)) ;; asynchronous call to another agent - new conversation-loop |
| 244 | + (do |
| 245 | + (jsonrpc/notify :message {:content (format "## (%s) sub-prompt" (:ref definition))}) |
| 246 | + (let [{:keys [messages _finish-reason]} |
| 247 | + (async/<!! (conversation-loop |
| 248 | + (assoc opts |
| 249 | + :prompts (git/prompt-file (:ref definition)) |
| 250 | + :parameters arg-context)))] |
| 251 | + (jsonrpc/notify :message {:content (format "## (%s) end sub-prompt" (:ref definition))}) |
| 252 | + (resolve (->> messages |
| 253 | + (filter #(= "assistant" (:role %))) |
| 254 | + (last) |
| 255 | + :content)))) |
| 256 | + :else |
| 257 | + (fail (format "bad container definition %s" definition))) |
| 258 | + (catch Throwable t |
| 259 | + (fail (format "system failure %s" t))))) |
258 | 260 | (fail "no function found")))
|
259 | 261 |
|
260 | 262 | (defn- run-prompts
|
|
0 commit comments