|
3 | 3 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
4 | 4 | import org.apache.hc.client5.http.impl.classic.HttpClients; |
5 | 5 | import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 6 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
6 | 7 | import org.apache.hc.core5.http.io.entity.StringEntity; |
7 | 8 | import org.apache.hc.core5.http.ClassicHttpResponse; |
8 | 9 | import org.apache.hc.core5.http.io.HttpClientResponseHandler; |
@@ -147,27 +148,149 @@ private String callOllama(String prompt) throws IOException { |
147 | 148 | } |
148 | 149 |
|
149 | 150 | /** |
150 | | - * Test connection to Ollama |
| 151 | + * Test connection to Ollama with enhanced diagnostics |
151 | 152 | */ |
152 | 153 | public boolean isAvailable() { |
153 | 154 | try { |
| 155 | + Log.info("🔍 AI availability check starting..."); |
| 156 | + System.out.println("🔍 Testing Ollama connection to: " + baseUrl + " with model: " + model); |
| 157 | + |
| 158 | + // First, test basic connectivity |
| 159 | + if (!testBasicConnectivity()) { |
| 160 | + System.out.println("❌ Basic connectivity failed to Ollama service"); |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + // Then test model availability |
| 165 | + if (!testModelAvailability()) { |
| 166 | + System.out.println("❌ Model '" + model + "' not available"); |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + // Finally, test actual generation |
154 | 171 | Log.info("Quick AI availability check for model: " + model); |
155 | | - // Quick test with very short prompt |
156 | 172 | String response = callOllama("Say OK"); |
157 | 173 | boolean available = response != null && response.toLowerCase().contains("ok"); |
158 | | - Log.info("AI availability: " + available + " (model: " + model + ")"); |
| 174 | + |
| 175 | + if (available) { |
| 176 | + Log.info("✅ AI availability: " + available + " (model: " + model + ")"); |
| 177 | + System.out.println("✅ Ollama AI fully operational with model: " + model); |
| 178 | + } else { |
| 179 | + Log.info("❌ AI generation test failed (model: " + model + ")"); |
| 180 | + System.out.println("❌ AI generation test failed - response: " + response); |
| 181 | + } |
| 182 | + |
159 | 183 | return available; |
160 | 184 | } catch (Exception e) { |
161 | | - Log.info("AI not available: " + e.getMessage() + " (model: " + model + ")"); |
| 185 | + String error = "AI not available: " + e.getMessage() + " (model: " + model + ")"; |
| 186 | + Log.info(error); |
| 187 | + System.out.println("❌ " + error); |
| 188 | + e.printStackTrace(); |
| 189 | + return false; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Test basic connectivity to Ollama service |
| 195 | + */ |
| 196 | + private boolean testBasicConnectivity() { |
| 197 | + try { |
| 198 | + HttpGet get = new HttpGet(baseUrl + "/api/tags"); |
| 199 | + |
| 200 | + org.apache.hc.client5.http.config.RequestConfig requestConfig = |
| 201 | + org.apache.hc.client5.http.config.RequestConfig.custom() |
| 202 | + .setConnectionRequestTimeout(10, java.util.concurrent.TimeUnit.SECONDS) |
| 203 | + .setResponseTimeout(10, java.util.concurrent.TimeUnit.SECONDS) |
| 204 | + .build(); |
| 205 | + get.setConfig(requestConfig); |
| 206 | + |
| 207 | + HttpClientResponseHandler<Boolean> responseHandler = response -> { |
| 208 | + int status = response.getCode(); |
| 209 | + System.out.println("🔗 Ollama connectivity test - Status: " + status); |
| 210 | + return status >= 200 && status < 300; |
| 211 | + }; |
| 212 | + |
| 213 | + return httpClient.execute(get, responseHandler); |
| 214 | + } catch (Exception e) { |
| 215 | + System.out.println("🔗 Connectivity test failed: " + e.getMessage()); |
162 | 216 | return false; |
163 | 217 | } |
164 | 218 | } |
165 | 219 |
|
166 | 220 | /** |
167 | | - * Get client status |
| 221 | + * Test if the specified model is available |
| 222 | + */ |
| 223 | + private boolean testModelAvailability() { |
| 224 | + try { |
| 225 | + HttpGet get = new HttpGet(baseUrl + "/api/tags"); |
| 226 | + |
| 227 | + HttpClientResponseHandler<Boolean> responseHandler = response -> { |
| 228 | + try { |
| 229 | + String responseBody = EntityUtils.toString(response.getEntity()); |
| 230 | + System.out.println("📋 Available models response: " + responseBody); |
| 231 | + |
| 232 | + JsonNode jsonResponse = objectMapper.readTree(responseBody); |
| 233 | + JsonNode models = jsonResponse.get("models"); |
| 234 | + |
| 235 | + if (models != null && models.isArray()) { |
| 236 | + for (JsonNode modelNode : models) { |
| 237 | + String modelName = modelNode.get("name").asText(); |
| 238 | + System.out.println("📦 Found model: " + modelName); |
| 239 | + if (modelName.startsWith(model)) { |
| 240 | + System.out.println("✅ Target model '" + model + "' found!"); |
| 241 | + return true; |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + System.out.println("❌ Target model '" + model + "' not found in available models"); |
| 247 | + return false; |
| 248 | + |
| 249 | + } catch (Exception e) { |
| 250 | + System.out.println("❌ Failed to parse models response: " + e.getMessage()); |
| 251 | + return false; |
| 252 | + } |
| 253 | + }; |
| 254 | + |
| 255 | + return httpClient.execute(get, responseHandler); |
| 256 | + } catch (Exception e) { |
| 257 | + System.out.println("📋 Model availability test failed: " + e.getMessage()); |
| 258 | + return false; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Get client status with detailed information |
168 | 264 | */ |
169 | 265 | public String getStatus() { |
170 | | - return "OllamaClient - Model: " + model + ", Available: " + isAvailable(); |
| 266 | + try { |
| 267 | + boolean available = isAvailable(); |
| 268 | + return String.format("OllamaClient - URL: %s, Model: %s, Available: %s", |
| 269 | + baseUrl, model, available); |
| 270 | + } catch (Exception e) { |
| 271 | + return String.format("OllamaClient - URL: %s, Model: %s, Error: %s", |
| 272 | + baseUrl, model, e.getMessage()); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Get detailed diagnostic information |
| 278 | + */ |
| 279 | + public String getDiagnostics() { |
| 280 | + StringBuilder diag = new StringBuilder(); |
| 281 | + diag.append("=== OLLAMA CLIENT DIAGNOSTICS ===\n"); |
| 282 | + diag.append("Base URL: ").append(baseUrl).append("\n"); |
| 283 | + diag.append("Target Model: ").append(model).append("\n"); |
| 284 | + |
| 285 | + try { |
| 286 | + diag.append("Basic Connectivity: ").append(testBasicConnectivity() ? "✅ OK" : "❌ FAILED").append("\n"); |
| 287 | + diag.append("Model Available: ").append(testModelAvailability() ? "✅ OK" : "❌ FAILED").append("\n"); |
| 288 | + } catch (Exception e) { |
| 289 | + diag.append("Diagnostics Error: ").append(e.getMessage()).append("\n"); |
| 290 | + } |
| 291 | + |
| 292 | + diag.append("==============================="); |
| 293 | + return diag.toString(); |
171 | 294 | } |
172 | 295 |
|
173 | 296 | /** |
|
0 commit comments