Skip to content

Commit eeb5b46

Browse files
introduce tracegpt
[added] TraceGPT Change-Id: I5dc5e397c0034eebb21860d64ffca90906e95a51 Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent e7f5c1f commit eeb5b46

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

tmf/org.eclipse.tracecompass.tmf.ui/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Require-Bundle: org.eclipse.core.expressions,
3030
org.eclipse.tracecompass.tmf.filter.parser,
3131
org.eclipse.e4.ui.css.swt.theme,
3232
org.eclipse.ui.themes,
33-
org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional
33+
org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional,
34+
json
3435
Export-Package: org.eclipse.tracecompass.internal.provisional.tmf.ui.model;x-internal:=true,
3536
org.eclipse.tracecompass.internal.provisional.tmf.ui.viewers.xychart;x-internal:=true,
3637
org.eclipse.tracecompass.internal.provisional.tmf.ui.widgets;x-friends:="org.eclipse.tracecompass.analysis.timing.ui",

tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/events/TmfEventsTable.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@
2020

2121
package org.eclipse.tracecompass.tmf.ui.viewers.events;
2222

23+
import java.io.BufferedReader;
24+
import java.io.DataOutputStream;
25+
import java.io.File;
2326
import java.io.FileNotFoundException;
27+
import java.io.FileWriter;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
2430
import java.lang.reflect.InvocationTargetException;
31+
import java.net.HttpURLConnection;
32+
import java.net.URL;
2533
import java.util.ArrayList;
2634
import java.util.Arrays;
2735
import java.util.Collection;
2836
import java.util.HashMap;
2937
import java.util.LinkedList;
3038
import java.util.List;
39+
import java.util.Map;
3140
import java.util.Map.Entry;
3241
import java.util.Objects;
3342
import java.util.regex.Matcher;
@@ -170,13 +179,15 @@
170179
import org.eclipse.ui.IEditorSite;
171180
import org.eclipse.ui.IWorkbenchPage;
172181
import org.eclipse.ui.IWorkbenchPartSite;
182+
import org.eclipse.ui.PartInitException;
173183
import org.eclipse.ui.PlatformUI;
174184
import org.eclipse.ui.commands.ICommandService;
175185
import org.eclipse.ui.handlers.IHandlerService;
176186
import org.eclipse.ui.ide.IDE;
177187
import org.eclipse.ui.ide.IGotoMarker;
178188
import org.eclipse.ui.themes.ColorUtil;
179189
import org.eclipse.ui.themes.IThemeManager;
190+
import org.json.JSONObject;
180191

181192
import com.google.common.base.Joiner;
182193
import com.google.common.collect.HashMultimap;
@@ -1282,6 +1293,105 @@ public void run() {
12821293
builder -> builder.setSynchronized(isChecked()));
12831294
}
12841295
};
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+
};
12851395

12861396
class ToggleBookmarkAction extends Action {
12871397
private final Long fRank;
@@ -1361,6 +1471,17 @@ public void run() {
13611471
} else if (!fRawViewer.isVisible()) {
13621472
fTablePopupMenuManager.add(showRawAction);
13631473
}
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+
}
13641485
fTablePopupMenuManager.add(exportToTextAction);
13651486
fTablePopupMenuManager.add(new Separator());
13661487

0 commit comments

Comments
 (0)