Já consultei o chatGPT, mas não consegui desvendar o problema, suspeito de incompatibilidade do Spring AI com Open AI.
Ao rodar a aplicação no primeiro teste apareceu o bug:
Error while extracting response for type [org.springframework.ai.openai.api.OpenAiApi$ChatCompletion] and content type [application/json;charset=utf-8]
Meu pom: 4.0.0 org.springframework.boot spring-boot-starter-parent 3.3.7 br.com.alura ecomart 0.0.1-SNAPSHOT ecomart Demo project for Spring Boot <java.version>21</java.version> <spring-ai.version>1.0.0-M4</spring-ai.version> org.springframework.boot spring-boot-starter-web org.springframework.ai spring-ai-openai-spring-boot-starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
classe controller:
import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("gerador") public class GeradorDeProdutosController {
private final ChatClient chatClient;
public GeradorDeProdutosController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
@GetMapping
public String gerarProdutos(){
var pergunta = "Gere 5 produtos ecológicos";
return this.chatClient.prompt()
.user(pergunta)
.call()
.content();
}
}