|
9 | 9 |
|
10 | 10 | (set! *warn-on-reflection* true) |
11 | 11 |
|
| 12 | +(def ^:private logger-tag "[LLM-API]") |
| 13 | + |
12 | 14 | (defn extra-models [config] |
13 | 15 | (llm-providers.ollama/list-models {:host (:host (:ollama config)) |
14 | 16 | :port (:port (:ollama config))})) |
|
17 | 19 | ;; TODO ask LLM for the most relevant parts of the path |
18 | 20 | (slurp path)) |
19 | 21 |
|
| 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 | + |
20 | 46 | (defn ^:private tool->llm-tool [tool] |
21 | 47 | (assoc (select-keys tool [:name :description :parameters]) |
22 | 48 | :type "function")) |
|
43 | 69 | "o3" |
44 | 70 | "gpt-4.1"} model) |
45 | 71 | (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}) |
58 | 84 |
|
59 | 85 | (contains? #{"claude-sonnet-4-0" |
60 | 86 | "claude-opus-4-0" |
61 | 87 | "claude-3-5-haiku-latest"} model) |
62 | 88 | (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}) |
74 | 100 |
|
75 | 101 | (string/starts-with? model config/ollama-model-prefix) |
76 | 102 | (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}) |
88 | 114 |
|
89 | 115 | :else |
90 | 116 | (on-error-wrapper {:msg (str "ECA Unsupported model: " model)})))) |
0 commit comments