Skip to content

Commit 4245fe6

Browse files
Simplify llm function to just take entire conversation state
1 parent 088882a commit 4245fe6

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/graph.clj

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
functions definition for functions referenced in the initial prompt
2828
opts for running the model
2929
returns channel that will contain an coll of messages"
30-
[messages m functions {:keys [url model stream level] :as _opts :or {level 0}}]
30+
[{:keys [messages functions metadata] {:keys [url model stream level]} :opts}]
3131
(let [[c h] (openai/chunk-handler)
3232
request (merge
33-
m
33+
(dissoc metadata :agent)
3434
{:messages messages
3535
:level level}
3636
(when (seq functions) {:tools functions})
3737
(when url {:url url})
3838
(when model {:model model})
39-
(when (and stream (nil? (:stream m))) {:stream stream}))]
39+
;; stream is a special case where we don't want to overwrite the metadata
40+
(when (and stream (nil? (:stream metadata))) {:stream stream}))]
4041
(try
4142
(if (seq messages)
4243
(openai/openai request h)
@@ -69,7 +70,7 @@
6970
"generate the next AI message
7071
passes the whole converation to the AI model"
7172
[state]
72-
(run-llm (:messages state) (dissoc (:metadata state) :agent) (:functions state) (:opts state)))
73+
(run-llm state))
7374

7475
(defn tool
7576
"execute the tool_calls from the last AI message in the conversation"
@@ -97,7 +98,7 @@
9798

9899
(declare stream chat-with-tools)
99100

100-
(defn apply-functions [coll]
101+
(defn apply-functions [coll]
101102
(fn [state]
102103
(reduce (fn [m f] (f state m)) state coll)))
103104

@@ -113,13 +114,13 @@
113114
(stream
114115
((or construct-graph chat-with-tools) state)
115116
(->
116-
((or
117-
(and
118-
init-state
119-
(if (coll? init-state)
120-
(apply-functions init-state)
121-
init-state))
122-
(comp (partial state/construct-initial-state-from-prompts state) state/add-prompt-ref)) state)
117+
((or
118+
(and
119+
init-state
120+
(if (coll? init-state)
121+
(apply-functions init-state)
122+
init-state))
123+
(comp (partial state/construct-initial-state-from-prompts state) state/add-prompt-ref)) state)
123124
(update-in [:opts :level] (fnil inc 0)))))]
124125
((or next-state state/add-last-message-as-tool-call) state sub-graph-state)))))
125126

src/graphs/sql.clj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@
5151
(state/tools-reset)
5252
(state/messages-from-prompt "prompts/sql/query-gen.md")
5353
(state/messages-append-all)]) state)
54-
{:keys [messages _finish-reason]} (async/<! (graph/run-llm
55-
(:messages x)
56-
(dissoc (:metadata x) :agent)
57-
(:functions x)
58-
(:opts x)))]
54+
{:keys [messages _finish-reason]} (async/<! (graph/run-llm x))]
5955

6056
; check for bad tool_calls and create failed Tool messages for them
6157
{:messages

0 commit comments

Comments
 (0)