Skip to content

ChatGPT Example with history conversation

Ahmad Albarqawi edited this page Mar 5, 2023 · 1 revision

You can use IntelliJava to start a conversation with ChatGPT giving the context of the previous chat and continue adding to the dialogue.

The primary roles for ChatGPT conversation:

  • System: defines the chat mode or theme.
  • User: defines the end user message or query.
  • Assistant: defines the chatbot response.

Example with a historical context and multiple outputs:

// Initiate the model with openai backend
Chatbot bot = new Chatbot(openaiKey, SupportedChatModels.openai);

// prepare the conversation context
String mode = "You are a helpful assistant.";
ChatModelInput input = new ChatGPTInput.Builder(mode)
.addMessage(new ChatGPTMessage("Who won the world series in 2020?", Role.user))
.addMessage(new ChatGPTMessage("The Los Angeles Dodgers won the World Series in 2020.", Role.assistant))
.addMessage(new ChatGPTMessage("Where was it played?", Role.user))
.setNumberOfOutputs(2).build();

// get the answer, the model will response with assistant role
List<String> resValues =  bot.chat(input);

Example for simple communication:

// Initiate the model with openai backend
Chatbot bot = new Chatbot(openaiKey, "openai");

// set the mode and user question
String mode = "You are a helpful astronomy assistant.";
ChatModelInput input = new ChatGPTInput.Builder(mode)
.addUserMessage("what is the space between moon and earth").build();

// get the answer
List<String> resValues =  bot.chat(input);
Clone this wiki locally