Why does langchain4j openAI api breaks while using in a Jakarta EE Rest web app?

I am new to Jakarta EE and I am trying to build a rest based chatbot using openAI apis with langchain4j.

@Path("blockChat")
public class BlockChat {

    private OpenAiChatModel chatModel;
    public BlockChat(){
        String apiKey = "XXXXXXXXX";
        chatModel = OpenAiChatModel.withApiKey(apiKey);
    }
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/{message}")
    public String chat(@PathParam("message") String message) {
        return chatModel.generate(message);
    }
}

when I made a get request to this endpoint froma client with a message, the application breaks.

20:09:56,735 WARN  [dev.langchain4j.internal.RetryUtils] (default task-1) Exception was thrown on attempt 1 of 3: java.lang.RuntimeException: Unable to create instance of class dev.ai4j.openai4j.chat.ChatCompletionResponse. Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.

Caused by: java.lang.UnsupportedOperationException: Cannot allocate class dev.ai4j.openai4j.chat.ChatCompletionResponse. Usage of JDK sun.misc.Unsafe is enabled, but it could not be used. Make sure your runtime is configured correctly.

I understand that this arises when GSON is trying to use sun.misc.Unsafe to create an instance of ChatCompletionResponse without constructor and Jakarta EE is restricting the access (just my understanding after some reading).

Here is my pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Chatbot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-open-ai</artifactId>
            <version>0.30.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-api</artifactId>
            <version>10.0.0</version>
            <scope>provided</scope>
        </dependency></dependencies>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <finalName>chatbot</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>4.2.1.Final</version>
                <configuration>
                    <version>30.0.1.Final</version>
                    <server-config>standalone-full.xml</server-config>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I tried executing the snippet in spring boot and I was able to receive response as a Json response. It also worked fine with Plain java classes.

PS : I am new to posting problems in Stack overflow. So, I am unsure that the provided info is enough. if not, I am eager to add more details.

Upvotes: 0

Views: 252

Answers (0)

Related Questions