1
resposta

[Bug] erro ao salvar no banco postgres

2024-04-17T20:09:13.646-03:00 INFO 10160 --- [ main] b.c.a.s.ScreenmatchApplication : Started ScreenmatchApplication in 3.894 seconds (process running for 4.342) 1 - Buscar séries 2 - Buscar episódios 3 - Listar séries buscadas

0 - Sair

1 Digite o nome da série para busca game of thrones 2024-04-17T20:10:10.345-03:00 INFO 10160 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2024-04-17T20:10:10.363-03:00 ERROR 10160 --- [ main] o.s.boot.SpringApplication : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:774) ~[spring-boot-3.1.1.jar:3.1.1] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) ~[spring-boot-3.1.1.jar:3.1.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) ~[spring-boot-3.1.1.jar:3.1.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1] at br.com.alura.screenmatch.ScreenmatchApplication.main(ScreenmatchApplication.java:16) ~[classes/:na] Caused by: java.lang.NullPointerException: OpenAI token required at java.base/java.util.Objects.requireNonNull(Objects.java:259) ~[na:na] at com.theokanning.openai.service.AuthenticationInterceptor.(AuthenticationInterceptor.java:18) ~[service-0.14.0.jar:na] at com.theokanning.openai.service.OpenAiService.defaultClient(OpenAiService.java:396) ~[service-0.14.0.jar:na] at com.theokanning.openai.service.OpenAiService.(OpenAiService.java:78) ~[service-0.14.0.jar:na] at com.theokanning.openai.service.OpenAiService.(OpenAiService.java:67) ~[service-0.14.0.jar:na] at br.com.alura.screenmatch.service.ConsultaChatGPT.obterTraducao(ConsultaChatGPT.java:8) ~[classes/:na] at br.com.alura.screenmatch.model.Serie.(Serie.java:37) ~[classes/:na] at br.com.alura.screenmatch.principal.Principal.buscarSerieWeb(Principal.java:67) ~[classes/:na] at br.com.alura.screenmatch.principal.Principal.exibeMenu(Principal.java:48) ~[classes/:na] at br.com.alura.screenmatch.ScreenmatchApplication.run(ScreenmatchApplication.java:22) ~[classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-3.1.1.jar:3.1.1] ... 5 common frames omitted

2024-04-17T20:10:10.372-03:00 INFO 10160 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2024-04-17T20:10:10.373-03:00 INFO 10160 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2024-04-17T20:10:10.381-03:00 INFO 10160 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

Process finished with exit code 1

public class ConsultaChatGPT {
public static String obterTraducao(String texto) {
    OpenAiService service = new OpenAiService(System.getenv("OPENAI_APIKEY"));


    CompletionRequest requisicao = CompletionRequest.builder()
            .model("text-davinci-003")
            .prompt("traduza para o português o texto: " + texto)
            .maxTokens(1000)
            .temperature(0.7)
            .build();


    var resposta = service.createCompletion(requisicao);
    return resposta.getChoices().get(0).getText();
}

}

@SpringBootApplication

public class ScreenmatchApplication implements CommandLineRunner { @Autowired private SerieRepository repositorio;

public static void main(String[] args) {
    SpringApplication.run(ScreenmatchApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    Principal principal = new Principal(repositorio);
    principal.exibeMenu();
}

}

1 resposta

Oi Adriano, de acordo com a linha: Caused by: java.lang.NullPointerException: OpenAI token required at java.base/java.util.Objects.requireNonNull(Objects.java:259); entende-se que no seu request não está incluso o API Key da OpenAI.

Parece que o System.getenv() está retornando null. Você configurou a sua API Key para a variável de ambiente OPENAI_KEY? Se você configurou e está retornando null, o problema pode ser de escopo do sistema, se você definiu a variável como uma variável de usuário, tente definir ela como uma variável global (Variaveis de Sistema).

Sobre variável global, me refiro a isso aqui:

Insira aqui a descrição dessa imagem para ajudar na acessibilidade