|
27 | 27 | functions definition for functions referenced in the initial prompt
|
28 | 28 | opts for running the model
|
29 | 29 | 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}] |
31 | 31 | (let [[c h] (openai/chunk-handler)
|
32 | 32 | request (merge
|
33 |
| - m |
| 33 | + (dissoc metadata :agent) |
34 | 34 | {:messages messages
|
35 | 35 | :level level}
|
36 | 36 | (when (seq functions) {:tools functions})
|
37 | 37 | (when url {:url url})
|
38 | 38 | (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}))] |
40 | 41 | (try
|
41 | 42 | (if (seq messages)
|
42 | 43 | (openai/openai request h)
|
|
69 | 70 | "generate the next AI message
|
70 | 71 | passes the whole converation to the AI model"
|
71 | 72 | [state]
|
72 |
| - (run-llm (:messages state) (dissoc (:metadata state) :agent) (:functions state) (:opts state))) |
| 73 | + (run-llm state)) |
73 | 74 |
|
74 | 75 | (defn tool
|
75 | 76 | "execute the tool_calls from the last AI message in the conversation"
|
|
97 | 98 |
|
98 | 99 | (declare stream chat-with-tools)
|
99 | 100 |
|
100 |
| -(defn apply-functions [coll] |
| 101 | +(defn apply-functions [coll] |
101 | 102 | (fn [state]
|
102 | 103 | (reduce (fn [m f] (f state m)) state coll)))
|
103 | 104 |
|
|
113 | 114 | (stream
|
114 | 115 | ((or construct-graph chat-with-tools) state)
|
115 | 116 | (->
|
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) |
123 | 124 | (update-in [:opts :level] (fnil inc 0)))))]
|
124 | 125 | ((or next-state state/add-last-message-as-tool-call) state sub-graph-state)))))
|
125 | 126 |
|
|
0 commit comments