22
33
44import com .otavio .aifoodapp .config .MaritacaChatClient ;
5+ import com .otavio .aifoodapp .mapper .RecipeMapper ;
56import com .otavio .aifoodapp .model .FoodItem ;
67import com .otavio .aifoodapp .model .Recipe ;
78import org .springframework .ai .chat .messages .SystemMessage ;
1920@ Service
2021public class ChatService {
2122 private final MaritacaChatClient maritacaChatClient ;
23+ private final RecipeMapper recipeMapper ;
2224
2325
2426 @ Value ("${maritaca.system.prompt}" )
2527 private String systemPrompt ;
2628
27- public ChatService (MaritacaChatClient maritacaChatClient ) {
29+ public ChatService (MaritacaChatClient maritacaChatClient , RecipeMapper recipeMapper ) {
2830 this .maritacaChatClient = maritacaChatClient ;
31+ this .recipeMapper = recipeMapper ;
2932 }
3033
31- public Mono <String > generateRecipe (List <FoodItem > foodItems ) {
34+ public Mono <List < Recipe > > generateRecipe (List <FoodItem > foodItems ) {
3235 String food = foodItems .stream ()
3336 .map (item -> String .format ("%s- Quantidade: %d, Validade: %s" ,
3437 item .getName (), item .getQuantity (),
@@ -38,19 +41,8 @@ public Mono<String> generateRecipe(List<FoodItem> foodItems) {
3841 //Prompt de teste
3942// String prompt = "Me mostre qual a estrutura do nosso banco de dados e quais itens estao armazenados nele";
4043
41- String prompt = "Generate a recipe strictly following the criteria below.\n \n " +
42- "**Available Ingredients:**\n " +
43- food + "\n \n " +
44- "**Recipe Criteria:**\n " +
45- "1. **Language:** Portuguese\n " +
46- "2. **Servings:** 1 person\n " +
47- "3. **Style:** Practical (simple steps), quick (fitness-focused), healthy, and tasty.\n " +
48- "4. **Mandatory Output:**\n " +
49- " * Dish Name (or what it contains)\n " +
50- " * Estimated Total Preparation Time\n " +
51- " * Detailed Preparation Method (step by step)\n " +
52- " * Nutritional Estimate (Calories, Proteins, Carbohydrates, Fats)\n \n " +
53- "**Note:** The answer must be in Portuguese." ;
44+ String prompt = "Gere uma ou mais receitas... A resposta DEVE ser um array JSON válido. Cada objeto no array deve ter a seguinte estrutura: " +
45+ "{\" dishName\" : \" NOME_DO_PRATO\" , \" prepTime\" : \" TEMPO_DE_PREPARO\" , \" instructions\" : [\" Passo 1\" , \" Passo 2\" ], \" nutritionalInfo\" : [\" Calorias: X kcal\" , \" Proteínas: Y g\" ]}" ;
5446
5547
5648 Prompt chatPrompt = new Prompt (List .of (
@@ -59,7 +51,8 @@ public Mono<String> generateRecipe(List<FoodItem> foodItems) {
5951 ));
6052
6153 return maritacaChatClient .call (chatPrompt )
62- .map (response -> response .getResult ().getOutput ().getText ());
54+ .map (response -> response .getResult ().getOutput ().getText ())
55+ .map (recipeMapper ::parseRecipeFromJson );
6356 }
6457
6558 public Mono <String > analyzeNutritionalProfile (Recipe recipe ) {
@@ -87,39 +80,4 @@ public Mono<String> suggestDietaryAdjustments(List<FoodItem> foodItems, String d
8780
8881 return null ;
8982 }
90-
91- public Recipe parseRecipeFromText (String recipeText , List <FoodItem > foodItem ) {
92- Recipe recipe = new Recipe ();
93- List <String > instructions = new ArrayList <>();
94- List <String > nutritionalInfo = new ArrayList <>();
95- String currentSection = "" ;
96-
97- String [] lines = recipeText .split ("\\ r?\\ n" );
98- for (String line : lines ) {
99- line = line .trim ();
100- if (line .toLowerCase ().startsWith ("nome do prato:" )) {
101- recipe .setName (line .substring ("nome do prato:" .length ()).trim ());
102- } else if (line .toLowerCase ().startsWith ("tempo de preparo:" )) {
103- recipe .setDescription (line .substring ("tempo de preparo:" .length ()).trim ());
104- } else if (line .toLowerCase ().startsWith ("modo de preparo:" )) {
105- currentSection = "instructions" ;
106- } else if (line .toLowerCase ().startsWith ("informação nutricional:" )) {
107- currentSection = "nutritional" ;
108- } else if (!line .isEmpty ()) {
109- switch (currentSection ) {
110- case "instructions" :
111- instructions .add (line );
112- break ;
113- case "nutritional" :
114- nutritionalInfo .add (line );
115- break ;
116- }
117- }
118- }
119- recipe .setInstructions (instructions );
120- recipe .setNutritionalInfo (nutritionalInfo );
121- recipe .setQuantity (1 );
122- recipe .setExpiration ("" );
123- return recipe ;
124- }
12583}
0 commit comments