|
20 | 20 |
|
21 | 21 | package org.eclipse.tracecompass.tmf.ui.viewers.events; |
22 | 22 |
|
| 23 | +import java.io.BufferedReader; |
| 24 | +import java.io.DataOutputStream; |
| 25 | +import java.io.File; |
23 | 26 | import java.io.FileNotFoundException; |
| 27 | +import java.io.FileWriter; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStreamReader; |
24 | 30 | import java.lang.reflect.InvocationTargetException; |
| 31 | +import java.net.HttpURLConnection; |
| 32 | +import java.net.URL; |
25 | 33 | import java.util.ArrayList; |
26 | 34 | import java.util.Arrays; |
27 | 35 | import java.util.Collection; |
28 | 36 | import java.util.HashMap; |
29 | 37 | import java.util.LinkedList; |
30 | 38 | import java.util.List; |
| 39 | +import java.util.Map; |
31 | 40 | import java.util.Map.Entry; |
32 | 41 | import java.util.Objects; |
33 | 42 | import java.util.regex.Matcher; |
|
170 | 179 | import org.eclipse.ui.IEditorSite; |
171 | 180 | import org.eclipse.ui.IWorkbenchPage; |
172 | 181 | import org.eclipse.ui.IWorkbenchPartSite; |
| 182 | +import org.eclipse.ui.PartInitException; |
173 | 183 | import org.eclipse.ui.PlatformUI; |
174 | 184 | import org.eclipse.ui.commands.ICommandService; |
175 | 185 | import org.eclipse.ui.handlers.IHandlerService; |
176 | 186 | import org.eclipse.ui.ide.IDE; |
177 | 187 | import org.eclipse.ui.ide.IGotoMarker; |
178 | 188 | import org.eclipse.ui.themes.ColorUtil; |
179 | 189 | import org.eclipse.ui.themes.IThemeManager; |
| 190 | +import org.json.JSONObject; |
180 | 191 |
|
181 | 192 | import com.google.common.base.Joiner; |
182 | 193 | import com.google.common.collect.HashMultimap; |
@@ -1282,6 +1293,105 @@ public void run() { |
1282 | 1293 | builder -> builder.setSynchronized(isChecked())); |
1283 | 1294 | } |
1284 | 1295 | }; |
| 1296 | + String ollamaUrl = "http://localhost:11434"; |
| 1297 | + final IAction lookupInLLMAction = new Action("Lookup in local LLM", IAction.AS_PUSH_BUTTON) { |
| 1298 | + @Override |
| 1299 | + public void run() { |
| 1300 | + ITmfTrace trace = fTrace; |
| 1301 | + if (trace == null || (fSelectedRank == -1 && fSelectedBeginRank == -1)) { |
| 1302 | + return; |
| 1303 | + } |
| 1304 | + Map<String, String> values = new HashMap<>(); |
| 1305 | + for (int i : fTable.getColumnOrder()) { |
| 1306 | + TableColumn column = fTable.getColumns()[i]; |
| 1307 | + // Omit the margin column and hidden columns |
| 1308 | + if (isVisibleEventColumn(column)) { |
| 1309 | + values.put(column.getText(), fTable.getSelection()[0].getText(i)); |
| 1310 | + } |
| 1311 | + } |
| 1312 | + Job jerb = new Job("llm") { |
| 1313 | + |
| 1314 | + @Override |
| 1315 | + protected IStatus run(IProgressMonitor monitor) { |
| 1316 | + String urlString = ollamaUrl + "/api/generate"; |
| 1317 | + |
| 1318 | + try { |
| 1319 | + // Create the URL object |
| 1320 | + URL url = new URL(urlString); |
| 1321 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 1322 | + connection.setRequestMethod("POST"); |
| 1323 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 1324 | + connection.setDoOutput(true); |
| 1325 | + |
| 1326 | + // Create the JSON payload |
| 1327 | + JSONObject jsonPayload = new JSONObject(); |
| 1328 | + jsonPayload.put("model", "llama3.2"); // Specify the |
| 1329 | + // model |
| 1330 | + jsonPayload.put("prompt", "I have an event from a trace of type " + trace.getTraceTypeId() + " in that there is an event content " + values.toString() + " explain it."); // Your |
| 1331 | + jsonPayload.put("stream", false); // input |
| 1332 | + // prompt |
| 1333 | + |
| 1334 | + // Send the request |
| 1335 | + try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { |
| 1336 | + wr.writeBytes(jsonPayload.toString()); |
| 1337 | + wr.flush(); |
| 1338 | + } |
| 1339 | + |
| 1340 | + // Get the response |
| 1341 | + int responseCode = connection.getResponseCode(); |
| 1342 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 1343 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));) { |
| 1344 | + String inputLine; |
| 1345 | + StringBuilder response = new StringBuilder(); |
| 1346 | + |
| 1347 | + while ((inputLine = in.readLine()) != null) { |
| 1348 | + response.append(inputLine); |
| 1349 | + } |
| 1350 | + |
| 1351 | + // Print the response |
| 1352 | + JSONObject jsonObj = new JSONObject(response.toString()); |
| 1353 | + String data = String.valueOf(jsonObj.get("response")); |
| 1354 | + File tmpFile = File.createTempFile("event_info", ".md"); |
| 1355 | + try (FileWriter fw = new FileWriter(tmpFile)) { |
| 1356 | + fw.append(data); |
| 1357 | + } |
| 1358 | + |
| 1359 | + IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new org.eclipse.core.runtime.Path(tmpFile.getAbsolutePath())); |
| 1360 | + |
| 1361 | + if (file != null && file.exists()) { |
| 1362 | + Display.getCurrent().asyncExec(new Runnable() { |
| 1363 | + @Override |
| 1364 | + public void run() { |
| 1365 | + // Get the active workbench page |
| 1366 | + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 1367 | + // Open the file in the Markdown |
| 1368 | + // editor |
| 1369 | + try { |
| 1370 | + IDE.openEditor(page, file, "org.eclipse.mylyn.wikit.editor.WikiEditor", true); |
| 1371 | + } catch (PartInitException e) { |
| 1372 | + e.printStackTrace(); |
| 1373 | + } // Replace |
| 1374 | + |
| 1375 | + } |
| 1376 | + }); |
| 1377 | + |
| 1378 | + } else { |
| 1379 | + System.out.println("File does not exist: " + tmpFile.getAbsolutePath()); |
| 1380 | + } |
| 1381 | + } |
| 1382 | + } else { |
| 1383 | + System.out.println("POST request failed"); |
| 1384 | + } |
| 1385 | + } catch (IOException e) { |
| 1386 | + e.printStackTrace(); |
| 1387 | + } |
| 1388 | + |
| 1389 | + return Status.OK_STATUS; |
| 1390 | + } |
| 1391 | + }; |
| 1392 | + jerb.schedule(); |
| 1393 | + } |
| 1394 | + }; |
1285 | 1395 |
|
1286 | 1396 | class ToggleBookmarkAction extends Action { |
1287 | 1397 | private final Long fRank; |
@@ -1361,6 +1471,17 @@ public void run() { |
1361 | 1471 | } else if (!fRawViewer.isVisible()) { |
1362 | 1472 | fTablePopupMenuManager.add(showRawAction); |
1363 | 1473 | } |
| 1474 | + URL url; |
| 1475 | + try { |
| 1476 | + url = new URL(ollamaUrl); |
| 1477 | + HttpURLConnection huc = (HttpURLConnection) url.openConnection(); |
| 1478 | + |
| 1479 | + if (huc.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 1480 | + fTablePopupMenuManager.add(lookupInLLMAction); |
| 1481 | + } |
| 1482 | + } catch (IOException e) { |
| 1483 | + // ignore |
| 1484 | + } |
1364 | 1485 | fTablePopupMenuManager.add(exportToTextAction); |
1365 | 1486 | fTablePopupMenuManager.add(new Separator()); |
1366 | 1487 |
|
|
0 commit comments