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!
==== 1. Content-Type に charset がない - 一部のサーバでは "application/json; charset=UTF-8" が必須 ``<code>java conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); <syntaxhighlight>2. HTTP 400 の原因:サーバ仕様と JSON の不一致 - cursor_offset をサポートしていない場合やフィールド名が異なる場合があります。 - ローカル AI サーバの仕様を確認してください。 ==== # HTTPS 用 HttpsURLConnection の import - 今回は http://127.0.0.1:1357 なので HttpsURLConnection は不要です。 - import javax.net.ssl.HttpsURLConnection; は削除して OK # escapeJson メソッドはもう不要 - Gson を使えば自動で JSON エスケープされます - 現在のコードでも呼んでいないので問題なし ==== </syntaxhighlight>java ==== 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(); if (responseCode != 200) { // エラー時は InputStream ではなく ErrorStream から読む try (BufferedReader err = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"))) { String line; StringBuilder errorResponse = new StringBuilder(); while ((line = err.readLine()) != null) { errorResponse.append(line).append("\n"); } throw new RuntimeException("HTTP Error " + responseCode + ":\n" + errorResponse.toString()); } } try (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"); } return response.toString(); } } <syntaxhighlight> ==== 1. サーバ仕様 - /v1/completions が model・prompt 以外に必須フィールドを求めていないか - cursor_offset が必須でなければ外してテスト ==== </syntaxhighlight>java payload.put("model", "gpt-local"); payload.put("prompt", sourceCode); // payload.put("cursor_offset", cursorOffset); // サーバ仕様による payload.put("max_tokens", 200); <syntaxhighlight> # JSON 出力の確認 - System.out.println("jsonRequest = " + jsonRequest); で内容を確認 - 改行や特殊文字が正しくエスケープされているか # サーバに curl で送って確認 </syntaxhighlight>bash curl -X POST http://127.0.0.1:1357/v1/completions \ -H "Content-Type: application/json" \ -d '{"model":"gpt-local","prompt":"print(\"Hello\")","max_tokens":10}' </code>`` * これでサーバが返答するか確認できます 💡 まとめ * Gson で JSON を生成 → ほぼ安全 * Content-Type を application/json; charset=UTF-8 に変更 * サーバ仕様に合わせて JSON フィールドを調整 * HTTP 400 の場合は、ErrorStream から詳細を読み取ると原因がわかる 必要であれば、私はこのコードを 取得したソースコードとカーソル位置を JSON にして AI に送信 → 返答をカーソル位置に挿入 まで完全に統合した Eclipse プラグイン版に書き直せます。 作りますか?
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)