Skip to content

Commit f46e327

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

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@
2020

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

23+
import java.io.BufferedReader;
24+
import java.io.DataOutputStream;
2325
import java.io.FileNotFoundException;
26+
import java.io.IOException;
27+
import java.io.InputStreamReader;
2428
import java.lang.reflect.InvocationTargetException;
29+
import java.net.HttpURLConnection;
30+
import java.net.URL;
2531
import java.util.ArrayList;
2632
import java.util.Arrays;
2733
import java.util.Collection;
2834
import java.util.HashMap;
2935
import java.util.LinkedList;
3036
import java.util.List;
37+
import java.util.Map;
3138
import java.util.Map.Entry;
3239
import java.util.Objects;
3340
import java.util.regex.Matcher;
@@ -43,6 +50,7 @@
4350
import org.eclipse.core.expressions.IEvaluationContext;
4451
import org.eclipse.core.resources.IFile;
4552
import org.eclipse.core.resources.IMarker;
53+
import org.eclipse.core.resources.IProject;
4654
import org.eclipse.core.resources.IResource;
4755
import org.eclipse.core.resources.IWorkspaceRunnable;
4856
import org.eclipse.core.resources.ResourcesPlugin;
@@ -167,16 +175,20 @@
167175
import org.eclipse.tracecompass.tmf.ui.views.colors.IColorSettingsListener;
168176
import org.eclipse.tracecompass.tmf.ui.widgets.rawviewer.TmfRawEventViewer;
169177
import org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.TmfVirtualTable;
178+
import org.eclipse.ui.IEditorRegistry;
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;
188+
import org.eclipse.ui.part.FileEditorInput;
178189
import org.eclipse.ui.themes.ColorUtil;
179190
import org.eclipse.ui.themes.IThemeManager;
191+
import org.json.JSONObject;
180192

181193
import com.google.common.base.Joiner;
182194
import com.google.common.collect.HashMultimap;
@@ -1282,6 +1294,106 @@ public void run() {
12821294
builder -> builder.setSynchronized(isChecked()));
12831295
}
12841296
};
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+
};
12851397

12861398
class ToggleBookmarkAction extends Action {
12871399
private final Long fRank;
@@ -1361,6 +1473,17 @@ public void run() {
13611473
} else if (!fRawViewer.isVisible()) {
13621474
fTablePopupMenuManager.add(showRawAction);
13631475
}
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+
}
13641487
fTablePopupMenuManager.add(exportToTextAction);
13651488
fTablePopupMenuManager.add(new Separator());
13661489

0 commit comments

Comments
 (0)