Mise en place v0.6.8
Amélioration de l'installation automatique Ajout de la possibilité de télécharger le llm que l'on veut à l'interieur de l'application en communicant avec ollama
This commit is contained in:
@@ -7,7 +7,9 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -15,7 +17,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -66,11 +78,81 @@ public class SettingsController {
|
||||
return forward(HttpMethod.POST, "/models/ollama/info", body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Telecharge un modele Ollama et streame la progression au client.
|
||||
* <p>
|
||||
* On bypass RestTemplate (qui bufferise toute la reponse) au profit du
|
||||
* client HTTP standard de Java en mode streaming. Le Brain renvoie du
|
||||
* NDJSON ligne par ligne ; on relaie chaque chunk tel quel pour que le
|
||||
* frontend voie la progression en temps reel.
|
||||
*/
|
||||
@PostMapping(value = "/models/ollama/pull", produces = "application/x-ndjson")
|
||||
public ResponseEntity<StreamingResponseBody> pullOllamaModel(@RequestBody Map<String, Object> body) {
|
||||
guardDemoMode();
|
||||
StreamingResponseBody stream = output -> {
|
||||
HttpClient http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(brainBaseUrl + "/models/ollama/pull"))
|
||||
.timeout(Duration.ofMinutes(60))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(toJson(body)))
|
||||
.build();
|
||||
try {
|
||||
HttpResponse<InputStream> resp = http.send(req, HttpResponse.BodyHandlers.ofInputStream());
|
||||
try (InputStream in = resp.body()) {
|
||||
byte[] buf = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buf)) != -1) {
|
||||
output.write(buf, 0, n);
|
||||
output.flush();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("Pull interrompu", ie);
|
||||
}
|
||||
};
|
||||
return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/x-ndjson")).body(stream);
|
||||
}
|
||||
|
||||
@DeleteMapping("/models/ollama/{name}")
|
||||
public ResponseEntity<Map<String, Object>> deleteOllamaModel(@PathVariable("name") String name) {
|
||||
guardDemoMode();
|
||||
return forward(HttpMethod.DELETE, "/models/ollama/" + name, null);
|
||||
}
|
||||
|
||||
@GetMapping("/models/onemin")
|
||||
public ResponseEntity<Map<String, Object>> listOneMinModels() {
|
||||
return forward(HttpMethod.GET, "/models/onemin", null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialiseur JSON minimal pour eviter d'instancier ObjectMapper a chaque
|
||||
* appel. Suffisant pour notre cas d'usage : Map<String,Object> avec des
|
||||
* String/Number/Boolean en valeur.
|
||||
*/
|
||||
private static String toJson(Map<String, Object> m) {
|
||||
StringBuilder sb = new StringBuilder("{");
|
||||
boolean first = true;
|
||||
for (Map.Entry<String, Object> e : m.entrySet()) {
|
||||
if (!first) sb.append(",");
|
||||
sb.append("\"").append(escape(e.getKey())).append("\":");
|
||||
Object v = e.getValue();
|
||||
if (v == null) sb.append("null");
|
||||
else if (v instanceof Number || v instanceof Boolean) sb.append(v);
|
||||
else sb.append("\"").append(escape(v.toString())).append("\"");
|
||||
first = false;
|
||||
}
|
||||
return sb.append("}").toString();
|
||||
}
|
||||
|
||||
private static String escape(String s) {
|
||||
return s.replace("\\", "\\\\").replace("\"", "\\\"")
|
||||
.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
|
||||
}
|
||||
|
||||
private void guardDemoMode() {
|
||||
if (demoMode) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Settings disabled in demo mode");
|
||||
|
||||
Reference in New Issue
Block a user