|
20 | 20 |
|
21 | 21 | package org.eclipse.tracecompass.tmf.ui.viewers.events; |
22 | 22 |
|
| 23 | +import java.io.BufferedReader; |
| 24 | +import java.io.DataOutputStream; |
23 | 25 | import java.io.FileNotFoundException; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStreamReader; |
24 | 28 | import java.lang.reflect.InvocationTargetException; |
| 29 | +import java.net.HttpURLConnection; |
| 30 | +import java.net.URL; |
25 | 31 | import java.util.ArrayList; |
26 | 32 | import java.util.Arrays; |
27 | 33 | import java.util.Collection; |
28 | 34 | import java.util.HashMap; |
29 | 35 | import java.util.LinkedList; |
30 | 36 | import java.util.List; |
| 37 | +import java.util.Map; |
31 | 38 | import java.util.Map.Entry; |
32 | 39 | import java.util.Objects; |
33 | 40 | import java.util.regex.Matcher; |
|
43 | 50 | import org.eclipse.core.expressions.IEvaluationContext; |
44 | 51 | import org.eclipse.core.resources.IFile; |
45 | 52 | import org.eclipse.core.resources.IMarker; |
| 53 | +import org.eclipse.core.resources.IProject; |
46 | 54 | import org.eclipse.core.resources.IResource; |
47 | 55 | import org.eclipse.core.resources.IWorkspaceRunnable; |
48 | 56 | import org.eclipse.core.resources.ResourcesPlugin; |
|
167 | 175 | import org.eclipse.tracecompass.tmf.ui.views.colors.IColorSettingsListener; |
168 | 176 | import org.eclipse.tracecompass.tmf.ui.widgets.rawviewer.TmfRawEventViewer; |
169 | 177 | import org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.TmfVirtualTable; |
| 178 | +import org.eclipse.ui.IEditorRegistry; |
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; |
| 188 | +import org.eclipse.ui.part.FileEditorInput; |
178 | 189 | import org.eclipse.ui.themes.ColorUtil; |
179 | 190 | import org.eclipse.ui.themes.IThemeManager; |
| 191 | +import org.json.JSONObject; |
180 | 192 |
|
181 | 193 | import com.google.common.base.Joiner; |
182 | 194 | import com.google.common.collect.HashMultimap; |
@@ -1282,6 +1294,106 @@ public void run() { |
1282 | 1294 | builder -> builder.setSynchronized(isChecked())); |
1283 | 1295 | } |
1284 | 1296 | }; |
| 1297 | + String ollamaUrl = "http://localhost:11434"; |
| 1298 | + final IAction lookupInLLMAction = new Action("Lookup in local LLM", IAction.AS_PUSH_BUTTON) { |
| 1299 | + @Override |
| 1300 | + public void run() { |
| 1301 | + ITmfTrace trace = fTrace; |
| 1302 | + if (trace == null || (fSelectedRank == -1 && fSelectedBeginRank == -1)) { |
| 1303 | + return; |
| 1304 | + } |
| 1305 | + Map<String, String> values = new HashMap<>(); |
| 1306 | + for (int i : fTable.getColumnOrder()) { |
| 1307 | + TableColumn column = fTable.getColumns()[i]; |
| 1308 | + // Omit the margin column and hidden columns |
| 1309 | + if (isVisibleEventColumn(column)) { |
| 1310 | + values.put(column.getText(), fTable.getSelection()[0].getText(i)); |
| 1311 | + } |
| 1312 | + } |
| 1313 | + Job jerb = new Job("llm") { |
| 1314 | + |
| 1315 | + @Override |
| 1316 | + protected IStatus run(IProgressMonitor monitor) { |
| 1317 | + String urlString = ollamaUrl + "/api/generate"; |
| 1318 | + |
| 1319 | + try { |
| 1320 | + // Create the URL object |
| 1321 | + URL url = new URL(urlString); |
| 1322 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 1323 | + connection.setRequestMethod("POST"); |
| 1324 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 1325 | + connection.setDoOutput(true); |
| 1326 | + |
| 1327 | + // Create the JSON payload |
| 1328 | + JSONObject jsonPayload = new JSONObject(); |
| 1329 | + jsonPayload.put("model", "llama3.2"); // Specify the |
| 1330 | + // model |
| 1331 | + 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 |
| 1332 | + jsonPayload.put("stream", false); // input |
| 1333 | + // prompt |
| 1334 | + |
| 1335 | + // Send the request |
| 1336 | + try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { |
| 1337 | + wr.writeBytes(jsonPayload.toString()); |
| 1338 | + wr.flush(); |
| 1339 | + } |
| 1340 | + |
| 1341 | + // Get the response |
| 1342 | + int responseCode = connection.getResponseCode(); |
| 1343 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 1344 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));) { |
| 1345 | + String inputLine; |
| 1346 | + StringBuilder response = new StringBuilder(); |
| 1347 | + |
| 1348 | + while ((inputLine = in.readLine()) != null) { |
| 1349 | + response.append(inputLine); |
| 1350 | + } |
| 1351 | + |
| 1352 | + // Print the response |
| 1353 | + JSONObject jsonObj = new JSONObject(response.toString()); |
| 1354 | + String data = String.valueOf(jsonObj.get("response")); |
| 1355 | + IProject project = fTrace.getResource().getProject(); |
| 1356 | + IFile file = project.getFile("event.txt"); |
| 1357 | + String fullPath = file.getFullPath().makeAbsolute().toOSString(); |
| 1358 | + try { |
| 1359 | + file.create(data.getBytes(), IResource.FORCE, monitor); |
| 1360 | + } catch (CoreException e) { |
| 1361 | + // TODO Auto-generated catch block |
| 1362 | + e.printStackTrace(); |
| 1363 | + } |
| 1364 | + |
| 1365 | + if (file.exists()) { |
| 1366 | + Display.getDefault().asyncExec(() -> { |
| 1367 | + // Get the active workbench page |
| 1368 | + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 1369 | + // Open the file in the Markdown editor |
| 1370 | + try { |
| 1371 | + page.openEditor( |
| 1372 | + new FileEditorInput(file), |
| 1373 | + IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); |
| 1374 | + } catch (PartInitException e) { |
| 1375 | + e.printStackTrace(); |
| 1376 | + } // Replace |
| 1377 | + |
| 1378 | + }); |
| 1379 | + |
| 1380 | + } else { |
| 1381 | + System.out.println("File does not exist: " + fullPath); |
| 1382 | + } |
| 1383 | + } |
| 1384 | + } else { |
| 1385 | + System.out.println("POST request failed"); |
| 1386 | + } |
| 1387 | + } catch (IOException e) { |
| 1388 | + e.printStackTrace(); |
| 1389 | + } |
| 1390 | + |
| 1391 | + return Status.OK_STATUS; |
| 1392 | + } |
| 1393 | + }; |
| 1394 | + jerb.schedule(); |
| 1395 | + } |
| 1396 | + }; |
1285 | 1397 |
|
1286 | 1398 | class ToggleBookmarkAction extends Action { |
1287 | 1399 | private final Long fRank; |
@@ -1361,6 +1473,17 @@ public void run() { |
1361 | 1473 | } else if (!fRawViewer.isVisible()) { |
1362 | 1474 | fTablePopupMenuManager.add(showRawAction); |
1363 | 1475 | } |
| 1476 | + URL url; |
| 1477 | + try { |
| 1478 | + url = new URL(ollamaUrl); |
| 1479 | + HttpURLConnection huc = (HttpURLConnection) url.openConnection(); |
| 1480 | + |
| 1481 | + if (huc.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 1482 | + fTablePopupMenuManager.add(lookupInLLMAction); |
| 1483 | + } |
| 1484 | + } catch (IOException e) { |
| 1485 | + // ignore |
| 1486 | + } |
1364 | 1487 | fTablePopupMenuManager.add(exportToTextAction); |
1365 | 1488 | fTablePopupMenuManager.add(new Separator()); |
1366 | 1489 |
|
|
0 commit comments