# Chat with ChatGPT using AWS Lambda and Java

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/](https://openai.com/api/)
    
* And create a token
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673179388534/8ec9aa55-9526-4951-a229-b8b628e9cda0.png align="center")

### 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 [**https://github.com/TheoKanning/openai-java**](https://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
    

```xml
<dependency>
    <groupId>com.theokanning.openai-gpt3-java</groupId>
    <artifactId>client</artifactId>
    <version>1.18.24</version>
</dependency>
```

* Create a class `TalkToChatGPT.java`
    

```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](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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673171909957/0238af94-6b08-4e8d-aa57-d1e8f1c9a3e4.png align="center")

To test the function, we can enable the function URL:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673246823759/14fd5071-5389-4902-b6af-c203706a0976.png align="center")

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).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673172936569/bc31034d-367d-48f6-9da7-42efb6397ce1.png align="center")

#### Upload the .jar file to the Lambda function

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673173150558/1033f9ad-20e3-4016-b233-3d51f59f7588.png align="center")

> #### **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`](http://packaged.name)`.ClassName::functionName`**.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673173216419/131286ec-3dd6-482a-884c-201a427743aa.png align="center")

### Test Lambda function

To test the functionality of our AWS Lambda function, we can use the following curl command:

```bash
$ 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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673176796036/3cb49a34-f932-4b02-b659-5638a80059f5.png align="center")

Enjoy ;)
