Skip to content

Commit 8910c1f

Browse files
feat: update generateRecipe method to return a list of Recipe entities and integrate RecipeMapper for JSON parsing
1 parent 9376c73 commit 8910c1f

File tree

1 file changed

+9
-51
lines changed

1 file changed

+9
-51
lines changed

src/main/java/com/otavio/aifoodapp/service/ChatService.java

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.otavio.aifoodapp.config.MaritacaChatClient;
5+
import com.otavio.aifoodapp.mapper.RecipeMapper;
56
import com.otavio.aifoodapp.model.FoodItem;
67
import com.otavio.aifoodapp.model.Recipe;
78
import org.springframework.ai.chat.messages.SystemMessage;
@@ -19,16 +20,18 @@
1920
@Service
2021
public 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

Comments
 (0)