53 lines
1.8 KiB
Java
53 lines
1.8 KiB
Java
package app.mealsmadeeasy.api.inference;
|
|
|
|
import org.springframework.ai.chat.client.ChatClient;
|
|
import org.springframework.ai.content.Media;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.core.io.InputStreamResource;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.MimeType;
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import java.io.InputStream;
|
|
|
|
@Service
|
|
public class InferenceService {
|
|
|
|
private final ChatClient chatClient;
|
|
|
|
public InferenceService(ChatClient.Builder chatClientBuilder) {
|
|
this.chatClient = chatClientBuilder.build();
|
|
}
|
|
|
|
public String extractRecipe(InputStream recipeImageInputStream) {
|
|
final Media media = Media.builder()
|
|
.data(new InputStreamResource(recipeImageInputStream))
|
|
.mimeType(MimeType.valueOf("image/jpeg"))
|
|
.build();
|
|
|
|
final String markdownResponse = this.chatClient.prompt()
|
|
.user(u ->
|
|
u.text(new ClassPathResource("app/mealsmadeeasy/api/inference/recipe-extract-user-prompt.md"))
|
|
.media(media)
|
|
)
|
|
.call()
|
|
.content();
|
|
return markdownResponse;
|
|
}
|
|
|
|
public Flux<String> extractRecipeStream(InputStream recipeImageInputStream) {
|
|
final Media media = Media.builder()
|
|
.data(new InputStreamResource(recipeImageInputStream))
|
|
.mimeType(MimeType.valueOf("image/jpeg"))
|
|
.build();
|
|
|
|
return this.chatClient.prompt()
|
|
.user(u ->
|
|
u.text(new ClassPathResource("app/mealsmadeeasy/api/inference/recipe-extract-user-prompt.md"))
|
|
.media(media)
|
|
)
|
|
.stream().content();
|
|
}
|
|
|
|
}
|