59 long startTime = System.currentTimeMillis();
60 HttpClient httpClient = HttpClient.newHttpClient();
62 HttpRequest.Builder requestBuilder =
63 getRequestBuilderDefault(uri)
65 body.getBodyPublisher());
66 HttpRequest request = requestBuilder.build();
67 if (this.verbose) LOG.info(
"Asking model: " + body.toString());
68 HttpResponse<InputStream> response =
69 httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
71 int statusCode = response.statusCode();
72 InputStream responseBodyStream = response.body();
73 StringBuilder responseBuffer =
new StringBuilder();
74 try (BufferedReader reader =
75 new BufferedReader(
new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
77 while ((line = reader.readLine()) !=
null) {
78 if (statusCode == 404) {
79 LOG.warn(
"Status code: 404 (Not Found)");
82 responseBuffer.append(ollamaResponseModel.getError());
83 }
else if (statusCode == 401) {
84 LOG.warn(
"Status code: 401 (Unauthorized)");
88 responseBuffer.append(ollamaResponseModel.getError());
89 }
else if (statusCode == 400) {
90 LOG.warn(
"Status code: 400 (Bad Request)");
93 responseBuffer.append(ollamaResponseModel.getError());
103 if (statusCode != 200) {
104 LOG.error(
"Status code " + statusCode);
107 long endTime = System.currentTimeMillis();
109 new OllamaResult(responseBuffer.toString().trim(), endTime - startTime, statusCode);
110 if (verbose) LOG.info(
"Model response: " + ollamaResult);