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!
=== ## === * Eclipse IDE for RCP and RAP Developers を利用して PDE プラグインプロジェクトを作成。 * 必要なプラグイン依存:org.eclipse.ui, org.eclipse.jface.text, org.eclipse.ui.editors, org.eclipse.core.runtime。 ==== plugin.xml にコマンド、ハンドラ、キーbinding を追加。例(抜粋): ==== <syntaxhighlight lang="xml"><extension point="org.eclipse.ui.commands"> <command id="com.example.aiassist.request" name="AI Assist Request"/> </extension> <extension point="org.eclipse.ui.handlers"> <handler class="com.example.aiassist.handlers.AISuggestHandler" commandId="com.example.aiassist.request"/> </extension> <extension point="org.eclipse.ui.bindings"> <key commandId="com.example.aiassist.request" contextId="org.eclipse.ui.contexts.window" sequence="M1+SPACE"/> <!-- M1 = Ctrl on Win/Linux, Cmd on macOS; 好きなショートカットに変更 --> </extension> </syntaxhighlight> : ==== AISuggestHandler は現在アクティブな ITextEditor から IDocument とカーソルオフセットを取得して AI クライアントに渡す。 ==== <syntaxhighlight lang="java">public class AISuggestHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); IEditorPart part = window.getActivePage().getActiveEditor(); if (!(part instanceof ITextEditor)) return null; ITextEditor editor = (ITextEditor) part; IDocumentProvider dp = editor.getDocumentProvider(); IDocument doc = dp.getDocument(editor.getEditorInput()); ISelectionProvider sp = editor.getSelectionProvider(); ISelection sel = sp.getSelection(); int offset = -1; if (sel instanceof ITextSelection) { offset = ((ITextSelection) sel).getOffset(); } String fullText = doc.get(); // 非同期ジョブでAI呼び出し AIRequestJob job = new AIRequestJob("AI Suggest", fullText, offset, editor); job.schedule(); return null; } } </syntaxhighlight> ==== Java 11 の HttpClient を利用する例。非同期で呼ぶこと、タイムアウトや例外処理を忘れずに。JSON 解析は Jackson/Gson。 ==== <syntaxhighlight lang="java">public class AIClient { private static final URI ENDPOINT = URI.create("http://172.0.0.1:1357/v1/chat/completions"); private final HttpClient http = HttpClient.newHttpClient(); private final ObjectMapper mapper = new ObjectMapper(); public String requestCompletion(String systemPrompt, String userPrompt) throws IOException, InterruptedException { ObjectNode body = mapper.createObjectNode(); body.put("model", "my-local-model"); ArrayNode messages = body.putArray("messages"); messages.addObject().put("role", "system").put("content", systemPrompt); messages.addObject().put("role", "user").put("content", userPrompt); String req = mapper.writeValueAsString(body); HttpRequest r = HttpRequest.newBuilder(ENDPOINT) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(req)) .build(); HttpResponse<String> resp = http.send(r, HttpResponse.BodyHandlers.ofString()); // 簡単な解析(実際のレスポンスフォーマットに合わせる) JsonNode root = mapper.readTree(resp.body()); // ここは /v1/chat/completions の戻りに合わせて調整 return root.at("/choices/0/message/content").asText(); } } </syntaxhighlight> ==== Eclipse の Job を使い、結果は Display.asyncExec(...) で UI スレッドへ。 ==== <syntaxhighlight lang="java">public class AIRequestJob extends Job { private final String text; private final int offset; private final ITextEditor editor; public AIRequestJob(String name, String text, int offset, ITextEditor editor) { super(name); this.text = text; this.offset = offset; this.editor = editor; } @Override protected IStatus run(IProgressMonitor monitor) { try { AIClient client = new AIClient(); String prompt = buildPrompt(text, offset); String suggestion = client.requestCompletion("You are a helpful assistant.", prompt); Display.getDefault().asyncExec(() -> { showProposal(editor, suggestion, offset); }); return Status.OK_STATUS; } catch (Exception e) { return new Status(IStatus.ERROR, "com.example.aiassist", "AI request failed", e); } } } </syntaxhighlight> buildPrompt は「ファイル全体」と「カーソル位置」情報を含む文字列を作る関数。例:"File:\n<fullText>\nCursorOffset: 123\nRequest: Provide a code snippet to insert at the cursor..." のように構造化する。 ==== Eclipse の標準補完 UI を再利用すると、ユーザーに馴染みやすく、Tabで受理しやすい。簡単な ICompletionProposal 実装例: ==== <syntaxhighlight lang="java">public class AICompletionProposal implements ICompletionProposal { private final String replacement; private final int offset; private final int length; private final ITextEditor editor; public AICompletionProposal(String replacement, int offset, int length, ITextEditor editor) { this.replacement = replacement; this.offset = offset; this.length = length; this.editor = editor; } @Override public void apply(IDocument document) { try { document.replace(offset, length, replacement); } catch (BadLocationException e) { // handle } } @Override public Point getSelection(IDocument document) { int newPos = offset + replacement.length(); return new Point(newPos, 0); } @Override public String getAdditionalProposalInfo() { return null; } @Override public String getDisplayString() { return "[AI] " + (replacement.length() > 100 ? replacement.substring(0, 100) + "..." : replacement); } @Override public Image getImage() { return null; } @Override public IContextInformation getContextInformation() { return null; } } </syntaxhighlight> 提案をポップアップするには ContentAssist フレームワークへ登録するか、簡易的には CompletionProposalPopup を自前実装して表示する。既存の ContentAssistant を使うと Tab で選択確定される設定が容易。 簡易表示フロー: * ITextViewer を取得して ICompletionProposal[] を生成。 * ContentAssistant.showPossibleCompletions() を呼ぶか、IContentAssistantExtension3.showProposalPopup()。 実例(概念): <syntaxhighlight lang="java">private void showProposal(ITextEditor editor, String suggestion, int offset) { IDocumentProvider dp = editor.getDocumentProvider(); IDocument doc = dp.getDocument(editor.getEditorInput()); AICompletionProposal p = new AICompletionProposal(suggestion, offset, 0, editor); ICompletionProposal[] proposals = new ICompletionProposal[] { p }; // シンプルに直接適用する方法(ユーザーがTabを押す仕組みを自前で実装する場合はキーリスナが必要) // もしくは ContentAssistant へ渡して標準UIで提示する(推奨)。 // 実装はプロジェクト種別・Eclipseのバージョンに合わせて調整してください。 } </syntaxhighlight> ==== - 標準 ContentAssistant のポップアップでは Enter/Tab で ICompletionProposal.apply() が呼ばれることが多い。これを使うのが簡単で互換性が高い。 ==== * もし自前ポップアップを作る場合は StyledText に VerifyKeyListener または KeyListener をつけ、Tabキーが押されたら現在の提案を IDocument.replace(...) で挿入する。 注意:Tabが既存のコード補完やインデントに使われている場合の衝突処理を行う。 ==== AIに送るペイロード例(/v1/chat/completions): ==== <syntaxhighlight lang="json">{ "model": "local-model", "messages": [ {"role":"system","content":"You are a helpful code assistant that returns only the code to insert."}, {"role":"user","content":"<file_contents>\n\nCursorOffset: 123\nTask: Suggest code/comments to insert at cursor. Provide only the code block string."} ], "max_tokens": 512 } </syntaxhighlight> APIの戻りは、choices[0].message.content に入っている想定で上のAIClient実装を合わせる。 ==== - ネットワークエラー:ユーザーにトースト表示(StatusManager / MessageDialog)。 ==== * タイムアウト:設定(例 10秒)を越えたらキャンセル。 * レスポンス不正:ロギングして何も表示しない。 * ユーザー設定:モデル名、エンドポイント、タイムアウト、ショートカットを Eclipse の Preference ページで設定可能にする。 ==== - 単体テスト:AIClientのモック(WireMock等)を使う。 ==== * PDE でランタイム Eclipse を起動して動作確認。 * export: 「Export -> Deployable plug-ins and fragments」または Update site(p2)で配布。 * インストール方法:ユーザーが .jar を dropins に入れるか、update site を使ってインストール。
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)