|
5 | 5 | */ |
6 | 6 | package io.jooby.internal.langchain4j; |
7 | 7 |
|
8 | | -import java.nio.file.Paths; |
9 | 8 | import java.time.Duration; |
10 | 9 |
|
11 | 10 | import com.typesafe.config.Config; |
@@ -106,21 +105,15 @@ public ChatModel createChatModel(@NonNull Config config) { |
106 | 105 | check("dev.langchain4j.model.jlama.JlamaChatModel", "langchain4j-jlama"); |
107 | 106 | return JlamaChatModel.builder() |
108 | 107 | .modelName(config.getString("model-name")) |
109 | | - .workingDirectory( |
110 | | - config.hasPath("working-dir") |
111 | | - ? Paths.get(config.getString("working-dir")) |
112 | | - : Paths.get(System.getProperty("user.dir"), "./models")) |
| 108 | + .workingDirectory(getOrCreateWorkingDir(config)) |
113 | 109 | .build(); |
114 | 110 | } |
115 | 111 |
|
116 | 112 | @Override |
117 | 113 | public StreamingChatModel createStreamingModel(@NonNull Config config) { |
118 | 114 | return JlamaStreamingChatModel.builder() |
119 | 115 | .modelName(config.getString("model-name")) |
120 | | - .workingDirectory( |
121 | | - config.hasPath("working-dir") |
122 | | - ? Paths.get(config.getString("working-dir")) |
123 | | - : Paths.get(System.getProperty("user.dir"), "./models")) |
| 116 | + .workingDirectory(getOrCreateWorkingDir(config)) |
124 | 117 | .build(); |
125 | 118 | } |
126 | 119 | }; |
@@ -164,4 +157,21 @@ protected Duration getStreamTimeout(Config config) { |
164 | 157 | protected double getTemp(Config config) { |
165 | 158 | return config.hasPath("temperature") ? config.getDouble("temperature") : 0.7; |
166 | 159 | } |
| 160 | + |
| 161 | + protected java.nio.file.Path getOrCreateWorkingDir(Config config) { |
| 162 | + java.nio.file.Path path = |
| 163 | + config.hasPath("working-dir") |
| 164 | + ? java.nio.file.Paths.get(config.getString("working-dir")) |
| 165 | + : java.nio.file.Paths.get(System.getProperty("user.dir"), "models"); |
| 166 | + |
| 167 | + try { |
| 168 | + // Jlama explicitly requires the directory to exist before booting |
| 169 | + if (!java.nio.file.Files.exists(path)) { |
| 170 | + java.nio.file.Files.createDirectories(path); |
| 171 | + } |
| 172 | + return path; |
| 173 | + } catch (java.io.IOException e) { |
| 174 | + throw new IllegalStateException("Failed to create a working directory at: " + path, e); |
| 175 | + } |
| 176 | + } |
167 | 177 | } |
0 commit comments