Estou tentando aprender integrar a APi do chatGPT com o java. Um prjeto bem simples só pra entender como funciona, mas está dando um erro e não estou conseguindo resolver. Vou deixar o código a baixo, se alguem poder me ajudar, me falem ai.
Controller:
@RestController
@RequestMapping("/text")
public class TextController {
@Value("${openai.token}")
private String OPEN_AI_KEY;
@PostMapping
public void genarate(@RequestBody TextGenerate generate) {
OpenAiService service = new OpenAiService(OPEN_AI_KEY);
CompletionRequest completionRequest = CompletionRequest.builder()
.prompt("Crie uma frase motivacional")
.maxTokens(4000)
.model("text-davinci-003")
.echo(true)
.build();
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
}
}
Classe Text:
@Data
public class TextGenerate {
private String text;
}
YML: Onde está os pontos é a chave da api
openai:
token: .........
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.com</groupId>
<artifactId>API_CHAT_GPT</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>API_CHAT_GPT</name>
<description>API_CHAT_GPT</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.14.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>