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: 素晴らしいです! === 素晴らしいです! 🎉 では、'''取得したソースコードとカーソル位置をローカルAIサーバ(http://127.0.0.1:1357)に送信して、補完を受け取る'''部分を作りましょう。 今回は Java の標準 HTTP クライアント(<code>HttpURLConnection</code>) を使ったシンプルな例で説明します。 ==== <syntaxhighlight lang="java">package to.silk.i.ai; ==== 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 javax.net.ssl.HttpsURLConnection; // HTTPS なら import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; 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 を作成 String jsonRequest = String.format( "{ \"model\": \"gpt-local\", \"prompt\": \"%s\", \"cursor_offset\": %d, \"max_tokens\": 200 }", escapeJson(sourceCode), cursorOffset ); try { String response = sendPostRequest(AI_SERVER_URL, jsonRequest); // AIの返答をコンソールに表示(ここで後でUIに反映可能) System.out.println("AI Response:\n" + response); } catch (Exception e) { e.printStackTrace(); } return null; } // JSONエスケープ private String escapeJson(String text) { return text.replace("\\", "\\\\") .replace("\"", "\\\"") .replace("\n", "\\n") .replace("\r", ""); } // 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"); 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(); } } </syntaxhighlight> ==== | | | ==== | --- | --- | | | | | | | | | | | | | | | | ==== - コンソールに表示しているだけなので、最終的には カーソル位置に補完を挿入 する処理を実装可能: ``<code>java document.replace(cursorOffset, 0, aiSuggestion); </code>`` ==== * JSONのパースには、org.json や Gson などを追加すると便利です。 * 選択範囲に対しても補完を挿入するよう拡張可能です。 これで、 # Ctrl+Shift+Z を押す # 現在のソースコードとカーソル位置を取得 # ローカル AI に送信 # 返答を取得(コンソール出力) までが完成しました 🎉 希望であれば次に、AIの返答をエディタに直接挿入して「Tab」で採用」する仕組みまで実装例を作ることもできます。 作りますか?
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)