Skip to content

Commit ea19915

Browse files
committed
Improve default model logic
1 parent dd8e968 commit ea19915

File tree

7 files changed

+70
-50
lines changed

7 files changed

+70
-50
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Improve default model logic to check for configs and env vars of known models.
6+
57
## 0.3.0
68

79
- Support stop chat prompts via `chat/promptStop` notification.

src/eca/db.clj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
:web-search true}
2222
"claude-3-5-haiku-latest" {:tools true
2323
:web-search true}} ;; + ollama local models
24-
:default-model "claude-sonnet-4-0" ;; unless a ollama model is running.
2524
:mcp-clients {}})
2625

2726
(defonce db* (atom initial-db))

src/eca/features/chat.clj

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
(ns eca.features.chat
22
(:require
33
[babashka.fs :as fs]
4+
[cheshire.core :as json]
45
[clojure.set :as set]
56
[clojure.string :as string]
6-
[eca.config :as config]
77
[eca.features.index :as f.index]
88
[eca.features.rules :as f.rules]
99
[eca.features.tools :as f.tools]
1010
[eca.llm-api :as llm-api]
1111
[eca.logger :as logger]
1212
[eca.messenger :as messenger]
13-
[eca.shared :as shared]
14-
[cheshire.core :as json]))
13+
[eca.shared :as shared]))
1514

1615
(set! *warn-on-reflection* true)
1716

@@ -57,10 +56,8 @@
5756
"chat" "Help with code changes only if user requested/agreed, ask first before do changes, answer questions, and provide explanations."
5857
"agent" "Help with code changes when applicable, suggesting you do the changes itself, answer questions, and provide explanations."))
5958

60-
(defn default-model [db]
61-
(if-let [ollama-model (first (filter #(string/starts-with? % config/ollama-model-prefix) (vals (:models db))))]
62-
ollama-model
63-
(:default-model db)))
59+
(defn default-model [db config]
60+
(llm-api/default-model db config))
6461

6562
(defn finish-chat-prompt! [chat-id status messenger db*]
6663
(swap! db* assoc-in [:chats chat-id :status] status)
@@ -112,7 +109,7 @@
112109
{:behavior (behavior->behavior-str (or behavior (:chat-default-behavior db)))})
113110
refined-contexts (raw-contexts->refined contexts)
114111
context-str (build-context-str refined-contexts rules)
115-
chosen-model (or model (default-model db))
112+
chosen-model (or model (default-model db config))
116113
past-messages (get-in db [:chats chat-id :messages] [])
117114
user-prompt message
118115
all-tools (f.tools/all-tools @db* config)

src/eca/handlers.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
db*
3838
config))
3939
{:models (keys (:models @db*))
40-
:chat-default-model (f.chat/default-model @db*)
40+
:chat-default-model (f.chat/default-model @db* config)
4141
:chat-behaviors (:chat-behaviors @db*)
4242
:chat-default-behavior (:chat-default-behavior @db*)
4343
:chat-welcome-message (:welcomeMessage (:chat config))}))

src/eca/llm_api.clj

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
(set! *warn-on-reflection* true)
1111

12+
(def ^:private logger-tag "[LLM-API]")
13+
1214
(defn extra-models [config]
1315
(llm-providers.ollama/list-models {:host (:host (:ollama config))
1416
:port (:port (:ollama config))}))
@@ -17,6 +19,30 @@
1719
;; TODO ask LLM for the most relevant parts of the path
1820
(slurp path))
1921

22+
(defn ^:private anthropic-api-key [config]
23+
(or (:anthropicApiKey config)
24+
(System/getenv "ANTHROPIC_API_KEY")))
25+
26+
(defn ^:private openai-api-key [config]
27+
(or (:openaiApiKey config)
28+
(System/getenv "OPENAI_API_KEY")))
29+
30+
(defn default-model
31+
"Returns the default LLM model checking this waterfall:
32+
- Anthropic api key set
33+
- Openai api key set
34+
- Ollama first model if running
35+
- Anthropic default model."
36+
[db config]
37+
(let [[decision model] (or (first (filter #(string/starts-with? % config/ollama-model-prefix) (vals (:models db))))
38+
(when (anthropic-api-key config)
39+
[:api-key-found "claude-sonnet-4-0"])
40+
(when (openai-api-key config)
41+
[:api-key-found "o4-mini"])
42+
[:default "claude-sonnet-4-0"])]
43+
(logger/info logger-tag (format "Default LLM model '%s' decision '%s'" model decision))
44+
model))
45+
2046
(defn ^:private tool->llm-tool [tool]
2147
(assoc (select-keys tool [:name :description :parameters])
2248
:type "function"))
@@ -43,48 +69,48 @@
4369
"o3"
4470
"gpt-4.1"} model)
4571
(llm-providers.openai/completion!
46-
{:model model
47-
:context context
48-
:user-prompt user-prompt
49-
:past-messages past-messages
50-
:tools tools
51-
:web-search web-search
52-
:api-key (:openaiApiKey config)}
53-
{:on-message-received on-message-received-wrapper
54-
:on-error on-error-wrapper
55-
:on-prepare-tool-call on-prepare-tool-call
56-
:on-tool-called on-tool-called
57-
:on-reason on-reason})
72+
{:model model
73+
:context context
74+
:user-prompt user-prompt
75+
:past-messages past-messages
76+
:tools tools
77+
:web-search web-search
78+
:api-key (openai-api-key config)}
79+
{:on-message-received on-message-received-wrapper
80+
:on-error on-error-wrapper
81+
:on-prepare-tool-call on-prepare-tool-call
82+
:on-tool-called on-tool-called
83+
:on-reason on-reason})
5884

5985
(contains? #{"claude-sonnet-4-0"
6086
"claude-opus-4-0"
6187
"claude-3-5-haiku-latest"} model)
6288
(llm-providers.anthropic/completion!
63-
{:model model
64-
:context context
65-
:user-prompt user-prompt
66-
:past-messages past-messages
67-
:tools tools
68-
:web-search web-search
69-
:api-key (:anthropicApiKey config)}
70-
{:on-message-received on-message-received-wrapper
71-
:on-error on-error-wrapper
72-
:on-prepare-tool-call on-prepare-tool-call
73-
:on-tool-called on-tool-called})
89+
{:model model
90+
:context context
91+
:user-prompt user-prompt
92+
:past-messages past-messages
93+
:tools tools
94+
:web-search web-search
95+
:api-key (anthropic-api-key config)}
96+
{:on-message-received on-message-received-wrapper
97+
:on-error on-error-wrapper
98+
:on-prepare-tool-call on-prepare-tool-call
99+
:on-tool-called on-tool-called})
74100

75101
(string/starts-with? model config/ollama-model-prefix)
76102
(llm-providers.ollama/completion!
77-
{:host (-> config :ollama :host)
78-
:port (-> config :ollama :port)
79-
:model (string/replace-first model config/ollama-model-prefix "")
80-
:past-messages past-messages
81-
:context context
82-
:tools tools
83-
:user-prompt user-prompt}
84-
{:on-message-received on-message-received-wrapper
85-
:on-error on-error-wrapper
86-
:on-prepare-tool-call on-prepare-tool-call
87-
:on-tool-called on-tool-called})
103+
{:host (-> config :ollama :host)
104+
:port (-> config :ollama :port)
105+
:model (string/replace-first model config/ollama-model-prefix "")
106+
:past-messages past-messages
107+
:context context
108+
:tools tools
109+
:user-prompt user-prompt}
110+
{:on-message-received on-message-received-wrapper
111+
:on-error on-error-wrapper
112+
:on-prepare-tool-call on-prepare-tool-call
113+
:on-tool-called on-tool-called})
88114

89115
:else
90116
(on-error-wrapper {:msg (str "ECA Unsupported model: " model)}))))

src/eca/llm_providers/anthropic.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
:cache_control {:type "ephemeral"}})))
3232

3333
(defn ^:private base-request! [{:keys [rid body api-key content-block* on-error on-response]}]
34-
(let [api-key (or api-key
35-
(System/getenv "ANTHROPIC_API_KEY"))
36-
url (url messages-path)]
34+
(let [url (url messages-path)]
3735
(llm-util/log-request logger-tag rid url body)
3836
(http/post
3937
url

src/eca/llm_providers/openai.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
path))
2121

2222
(defn ^:private base-completion-request! [{:keys [rid body api-key on-error on-response]}]
23-
(let [api-key (or api-key
24-
(System/getenv "OPENAI_API_KEY"))
25-
url (url responses-path)]
23+
(let [url (url responses-path)]
2624
(llm-util/log-request logger-tag rid url body)
2725
(http/post
2826
url

0 commit comments

Comments
 (0)