Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/691151cc-3da4-800f-9b7c-81b0e0d10369
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== User: package to.silk.i.ai; === package to.silk.i.ai; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.handlers.HandlerUtil; import org.eclipse.ui.texteditor.ITextEditor; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; public class AISuggestHandler extends AbstractHandler { private static final String AI_SERVER_URL = "http://127.0.0.1:1357/v1/completions"; @Override public Object execute(ExecutionEvent event) throws ExecutionException { IEditorPart editorPart = HandlerUtil.getActiveEditor(event); if (!(editorPart instanceof ITextEditor)) { System.out.println("現在のエディタはテキストエディタではありません"); return null; } ITextEditor textEditor = (ITextEditor) editorPart; IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); ITextSelection selection = (ITextSelection) textEditor.getSelectionProvider().getSelection(); String sourceCode = document.get(); int cursorOffset = selection.getOffset(); // AI サーバーに送信する JSON を作成 Gson gson = new Gson(); Map<String, Object> payload = new HashMap<>(); String instruction = "以下のコードのカーソル位置に追加するコードかコメントだけを生成してください。" + " 説明文やコメント、提案、Markdown記法(など)、提案文は一切不要。" + " 既存のコードを壊さず、追加すべきコードかコメントのみを返してください。"; payload.put("model", "gpt-local"); payload.put("prompt", instruction + "\n\nコード:\n" + sourceCode); payload.put("cursor_offset", cursorOffset); payload.put("max_tokens", 200); String jsonRequest = gson.toJson(payload); System.out.println("jsonRequest" + jsonRequest); try { String response = sendPostRequest(AI_SERVER_URL, jsonRequest); String aiText = ""; JsonObject jsonObj = JsonParser.parseString(response).getAsJsonObject(); if (jsonObj.has("choices")) { aiText = jsonObj .getAsJsonArray("choices") .get(0) .getAsJsonObject() .get("text") .getAsString(); aiText = extractCodeFromAIResponse(aiText); System.out.println("aiText:" + aiText); } document.replace(cursorOffset, 0, aiText); } catch (Exception e) { e.printStackTrace(); } return null; } private String extractCodeFromAIResponse(String response) { // "final" というキーワードの最後の出現位置を取得 int index = response.lastIndexOf("final"); if (index == -1) { // final が見つからなければそのまま返す return response.trim(); } // final の後に続く部分だけ取り出す String codePart = response.substring(index + "final".length()); // 1. 不要な制御トークン(<|…|>)を削除 String cleaned = codePart.replaceAll("<\\|.*?\\|>", ""); // 2. Markdownコードブロック(java や )を削除 cleaned = cleaned.replaceAll("(?m)^.*$", ""); cleaned = cleaned.replaceAll("", ""); // 3. 前後空白を削除 return cleaned.trim(); } // HTTP POST 送信 private String sendPostRequest(String urlStr, String json) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); try (OutputStream os = conn.getOutputStream()) { os.write(json.getBytes("UTF-8")); } BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine).append("\n"); } in.close(); return response.toString(); } }
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)