@Takomeshi_

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ProcessingとOpenAI APIの併用によるエラー

解決したいこと

"Processing"で、ChatGPTを用いた料理推薦アプリを作成しています。その日の好みなどを三択形式で選択し、その結果をOpenAI APIを通してChatGPTに送信される仕組みとなっています。
しかし、APIからChatGPTに送信する際、下記の画像のようなエラーが出てしまいます。どうすればよろしいでしょうか?解決方法を教えていただけると幸いです。

発生している問題・エラー

エラー文は以下の通りです。非常に小さく読みにくいため、ご注意ください。申し訳ございません。
エラー文.png

ソースコード

import http.requests.*;
import java.util.HashMap;

HashMap questions;
HashMap options;
String currentQuestion;
String[] currentOptions;
int step = 0;

void setup() {
size(480, 640);
textSize(16);
textAlign(CENTER, CENTER);
PFont font = createFont("Meiryo", 20);
textFont(font);

// 質問と選択肢の初期設定
questions = new HashMap();
options = new HashMap();
initializeQuestions();

currentQuestion = questions.get("step0")[0];
currentOptions = options.get("step0");
}

void draw() {
background(255);
displayQuestionAndOptions();
}

void mousePressed() {
for (int i = 0; i < currentOptions.length; i++) {
if (mouseX > 40 && mouseX < 440 && mouseY > 200 + i * 100 && mouseY < 280 + i * 100) {
processResponse(currentOptions[i]);
break;
}
}
}

void initializeQuestions() {
questions.put("step0", new String[]{"どの時間帯に食事をしますか?"});
options.put("step0", new String[]{"朝", "昼", "夜"});

questions.put("step1_朝", new String[]{"食べる量はどのくらいですか?"});
options.put("step1_朝", new String[]{"少し", "ほどほど", "たくさん"});

questions.put("step1_昼", new String[]{"食べる量はどのくらいですか?"});
options.put("step1_昼", new String[]{"少し", "ほどほど", "たくさん"});

questions.put("step1_夜", new String[]{"食べる量はどのくらいですか?"});
options.put("step1_夜", new String[]{"少し", "ほどほど", "たくさん"});

// Add more questions and options as needed
}

void displayQuestionAndOptions() {
fill(0);
text(currentQuestion, width / 2, 80);
for (int i = 0; i < currentOptions.length; i++) {
fill(200, 200, 250);
rect(40, 200 + i * 100, 400, 80);
fill(0);
text(currentOptions[i], width / 2, 240 + i * 100);
}
}

void processResponse(String response) {
step++;
String nextStepKey = "step" + step + "_" + response;
if (questions.containsKey(nextStepKey)) {
currentQuestion = questions.get(nextStepKey)[0];
currentOptions = options.get(nextStepKey);
} else {
recommendDish(response);
}
}

void recommendDish(String lastResponse) {
// 最後の質問への回答を基に、ChatGPT APIにリクエストを送る
String prompt = "Based on the user's preferences, recommend a dish.";

// HTTPリクエストの設定
PostRequest post = new PostRequest("https://api.openai.com/v1/engines/davinci-codex/completions");
post.addHeader("Authorization", "Bearer API Key");
post.addHeader("Content-Type", "application/json");
post.addData("prompt", prompt);
post.addData("max_tokens", "50");

post.send();
if (post.getContent() != null) {
String response = post.getContent();
currentQuestion = "Based on your preferences, we recommend: " + response;
currentOptions = new String[]{};
}
}

0 likes

2Answer

リクエストボディに JSON をセットするには

post.addData("prompt", prompt);
post.addData("max_tokens", "50");

ではなく

JSONObject json = new JSONObject();
json.setString("prompt", prompt);
json.setInt("max_tokens", 50);
post.addData(json.toString());

だと思います。(動くかどうか試してませんが)

参考:

1Like

エラー文は以下の通りです。非常に小さく読みにくいため、ご注意ください。

そのエラー文の内容をどこまで調べて、どうだったのでしょうか?

0Like

Your answer might help someone💌