Are you interested in using the ChatGPT library to build a chatbot or natural language processing application?
Follow along as we walk through creating an AWS Lambda function using Java. By the end of this tutorial, you will have a fully-functioning Java-based AWS Lambda that integrates with the ChatGPT library.
ChatGPT Token
To use the ChatGPT API, you need to create a token:
Login to https://openai.com/api/
And create a token
AWS Lambda function code
This article will explore how to integrate ChatGPT into your application using the openai-java library. This library, developed by TheoKanning and available on GitHub at github.com/TheoKanning/openai-java, provides a convenient way to access the ChatGPT API and take advantage of its natural language processing capabilities in your own projects.
Create a new maven project
In pom.xml add
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>client</artifactId>
<version>1.18.24</version>
</dependency>
- Create a class
TalkToChatGPT.java
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Slf4j
public class TalkToChatGPT {
private static final String MODEL = "text-davinci-003";
private static final String TOKEN = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static final OpenAiService OPEN_AI_SERVICE = new OpenAiService(TOKEN);
public String handleRequest(Event event) {
final String question = event.getBody();
return getChatGPTResponse(question);
}
private String getChatGPTResponse(String question) {
CompletionRequest completionRequest = getCompletionRequest(question);
return Optional.ofNullable(OPEN_AI_SERVICE.createCompletion(completionRequest))
.map(CompletionResult::getChoices)
.stream()
.flatMap(List::stream)
.map(CompletionChoice::getText)
.map(this::removeSpaces)
.collect(Collectors.joining());
}
private static CompletionRequest getCompletionRequest(String question) {
return CompletionRequest.builder()
.model(MODEL)
.prompt(question)
.maxTokens(1000)
.build();
}
private String removeSpaces(String text) {
return text.replace("\n", "").trim();
}
}
You can find the complete project at this link https://github.com/laidani/AWS-Lambda-ChatGPT
Before we can upload and run our code on AWS Lambda, we need to create a .jar file containing all of the necessary code and dependencies. To do this, we can use the
mvn clean package
command.
AWS Lambda function
Create a new Lambda function
To proceed, log in to your AWS account and create a new Lambda function as follows:
To test the function, we can enable the function URL:
This will allow us to send requests to the function and receive responses from it, allowing us to verify that it is working as intended.
To ensure that ChatGPT has enough time to generate a response, it is advisable to set the timeout for the function to a value greater than 15 seconds(default timeout of lambda function).
Upload the .jar file to the Lambda function
Ensure that the Handler field in your Lambda function points to the correct Java function by specifying the fully qualified class name followed by
::
and the function name, for example:packaged.name
.ClassName::functionName
.
Test Lambda function
To test the functionality of our AWS Lambda function, we can use the following curl command:
$ curl --location --request POST 'https://luqp5lgsrvcdxu5dxf3aa67iee0spbbm.lambda-url.eu-west-1.on.aws/' \
--header 'Content-Type: text/plain' \
--data-raw 'How many planets are in the Solar System?'
$ There are 8 planets in the Solar System: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.
Or by using postman:
Enjoy ;)