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!
=== Assistant: 完璧です。では、この応答を カーソル位置に挿入する処理 まで統合して、Eclipse プラグインとして補完できる形にしましょう。 === 完璧です。では、この応答を カーソル位置に挿入する処理 まで統合して、Eclipse プラグインとして補完できる形にしましょう。 以下に Gson で AI の JSON から <code>choices[0].text</code> を取り出して、カーソル位置に挿入する完全版ハンドラ を示します。 <syntaxhighlight lang="java">package to.silk.i.ai; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.Gson; 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.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<>(); payload.put("model", "gpt-local"); payload.put("prompt", sourceCode); payload.put("max_tokens", 200); String jsonRequest = gson.toJson(payload); try { String response = sendPostRequest(AI_SERVER_URL, jsonRequest); String aiText = parseAIResponse(response); if (aiText != null && !aiText.isEmpty()) { document.replace(cursorOffset, 0, aiText); } } catch (Exception e) { e.printStackTrace(); } return null; } // AIのJSONレスポンスからchoices[0].textを取得 private String parseAIResponse(String response) { JsonObject jsonObj = JsonParser.parseString(response).getAsJsonObject(); if (jsonObj.has("choices")) { return jsonObj.getAsJsonArray("choices") .get(0) .getAsJsonObject() .get("text") .getAsString(); } return ""; } // 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")); } int responseCode = conn.getResponseCode(); BufferedReader in; if (responseCode == 200) { in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8")); } String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine).append("\n"); } in.close(); if (responseCode != 200) { throw new RuntimeException("HTTP Error " + responseCode + ":\n" + response.toString()); } return response.toString(); } } </syntaxhighlight> ===== 1. Ctrl+Shift+Z で呼び出すと、現在のソースコードをローカル AI に送信 ===== # AI が返した choices[0].text を カーソル位置に直接挿入 # Gson で安全に JSON を生成&パース # HTTP 400 やサーバエラー時もコンソールに表示 この状態で GitHub Copilot 風の補完プラグイン が動作します。 * Tab キーで確定する UI を追加すれば、より「提案 → 確定」形式にできます。 必要であれば、私は Tab キーで提案を確定できる ContentAssist 風 UI 付きのバージョン も作れます。 作りますか?
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)