import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import com.theokanning.openai.completion.chat.ChatCompletionChoice;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatCompletionResult;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageRole;
import com.theokanning.openai.service.OpenAiService;
public class App {
public static void main(String[] args) {
String token="sk-w*******************************************";// APIキー
OpenAiService service=new OpenAiService(token,Duration.ofSeconds(60));
String message="Q: 日本の首都は、何ですか? A:";
String prompt="The following is a conversation with an AI assistant. The assistant is helpful, creative, clever";
// メッセージ格納List
List<ChatMessage> messages=new ArrayList<ChatMessage>();
// promptメッセージ
ChatMessage promptMessage=new ChatMessage();
promptMessage.setRole(ChatMessageRole.SYSTEM.value());
promptMessage.setContent(prompt);
messages.add(promptMessage);
// Userメッセージ
ChatMessage userMessage=new ChatMessage();
userMessage.setRole(ChatMessageRole.USER.value());
userMessage.setContent(message);
messages.add(userMessage);
ChatCompletionRequest completionRequest=ChatCompletionRequest.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.maxTokens(2048)
.build();
ChatCompletionResult completionResult=service.createChatCompletion(completionRequest);
ChatCompletionChoice choice=completionResult.getChoices().get(0);
System.out.println(choice.getMessage());
}
}