Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding meal type and MockGPT #54

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/src/main/java/code/client/Controllers/MockGPT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package code.client.Controllers;

import java.io.IOException;
import java.net.URISyntaxException;

import code.client.Model.ITextToRecipe;
import code.client.Model.Recipe;

public class MockGPT implements ITextToRecipe {

@Override
public String buildPrompt(String typeOfMeal, String input) {
return "I am a student on a budget with a busy schedule and I need to quickly cook a Lunch." +
"I have chicken, and eggs. Make a recipe using only these ingredients plus condiments." +
"Remember to first include a title, then a list of ingredients, and then a list of instructions.";
}

@Override
public String getChatGPTResponse(String typeOfMeal, String input)
throws IOException, InterruptedException, URISyntaxException {
return """
Fried Chicken

Ingredients:

- 2 chicken breasts, diced
- 2 eggs

Instructions:
1. Crack 2 eggs into bowl.
2. Add chicken into bowl and then fry.
3. Enjoy!
""";
}

@Override
public Recipe mapResponseToRecipe(String responseText) {
Recipe recipe = new Recipe("1", "Fried Chicken");
recipe.addIngredient("- 2 chicken breasts, diced");
recipe.addIngredient("- 2 eggs");
recipe.addInstruction("1. Crack 2 eggs into bowl.");
recipe.addInstruction("2. Add chicken into bowl and then fry.");
recipe.addInstruction("3. Enjoy!");
return recipe;
}

}
4 changes: 2 additions & 2 deletions app/src/main/java/code/client/Model/ITextToRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.net.URISyntaxException;

public interface ITextToRecipe {
String buildPrompt(String input);
String buildPrompt(String typeOfMeal, String input);

String getChatGPTResponse(String input) throws IOException, InterruptedException, URISyntaxException;
String getChatGPTResponse(String typeOfMeal, String input) throws IOException, InterruptedException, URISyntaxException;

Recipe mapResponseToRecipe(String responseText);
}
11 changes: 6 additions & 5 deletions app/src/main/java/code/client/Model/TextToRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class TextToRecipe implements ITextToRecipe {
private static final double TEMPERATURE = 1.;

@Override
public String getChatGPTResponse(String input) throws IOException, InterruptedException, URISyntaxException {
public String getChatGPTResponse(String typeOfMeal, String input) throws IOException, InterruptedException, URISyntaxException {
// Set request parameters
String prompt = buildPrompt(input);
String prompt = buildPrompt(typeOfMeal, input);

// Create a request body which you will pass into request object
JSONObject requestBody = new JSONObject();
Expand Down Expand Up @@ -94,11 +94,12 @@ public Recipe mapResponseToRecipe(String responseText) {
return recipe;
}

public String buildPrompt(String input) {
public String buildPrompt(String typeOfMeal, String input) {
StringBuilder prompt = new StringBuilder();
prompt.append("I am a student on a budget with a busy schedule and I need to quickly cook a meal. ")
prompt.append("I am a student on a budget with a busy schedule and I need to quickly cook a ")
.append(typeOfMeal + " ")
.append(input)
.append("Make a recipe using only these ingredients plus condiments. ")
.append(" Make a recipe using only these ingredients plus condiments. ")
.append("Remember to first include a title, then a list of ingredients, and then a list of instructions.");
return prompt.toString();
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/code/client/View/NewRecipeUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ public void addListeners() throws IOException, URISyntaxException {

ITextToRecipe caller = new TextToRecipe();
try {

String audioOutput = ingredients;// audio.processAudio();
String responseText = caller.getChatGPTResponse(audioOutput);
String audioOutput1 = mealType;
String audioOutput2 = ingredients;// audio.processAudio();
String responseText = caller.getChatGPTResponse(audioOutput1, audioOutput2);
Recipe recipe = caller.mapResponseToRecipe(responseText);
RecipeDetailsUI detailsUI = new RecipeDetailsUI(recipe);

Expand Down
43 changes: 41 additions & 2 deletions app/src/test/java/code/TextToRecipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,55 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.net.URISyntaxException;

public class TextToRecipeTest {
private static class MockGPT implements ITextToRecipe {

@Override
public String buildPrompt(String typeOfMeal, String input) {
return """
I am a student on a budget with a busy schedule and I need to quickly cook a Lunch.
I have rice, shrimp, chicken, and eggs. Make a recipe using only these ingredients plus condiments.
Remember to first include a title, then a list of ingredients, and then a list of instructions.
""";
}

@Override
public String getChatGPTResponse(String typeOfMeal, String input)
throws IOException, InterruptedException, URISyntaxException {
return """
Fried Chicken

Ingredients:

- 2 chicken breasts, diced

Instructions:

1. Enjoy!
""";
}

@Override
public Recipe mapResponseToRecipe(String responseText) {
Recipe recipe = new Recipe("1", "Fried Chicken and Egg Fried Rice");
recipe.addIngredient("- 2 chicken breasts, diced");
recipe.addInstruction("1. Enjoy!");
return recipe;
}

}

@Test
/**
* Integration test for provide recipe
*/
public void testPromptBuild() {
ITextToRecipe textToRecipe = new TextToRecipe();
String prompt = "I am a student on a budget with a busy schedule and I need to quickly cook a meal. I have rice, shrimp, chicken, and eggs. Make a recipe using only these ingredients plus condiments. Remember to first include a title, then a list of ingredients, and then a list of instructions.";
String response = textToRecipe.buildPrompt("I have rice, shrimp, chicken, and eggs. ");
String prompt = "I am a student on a budget with a busy schedule and I need to quickly cook a Lunch. I have rice, shrimp, chicken, and eggs. Make a recipe using only these ingredients plus condiments. Remember to first include a title, then a list of ingredients, and then a list of instructions.";
String response = textToRecipe.buildPrompt("Lunch.","I have rice, shrimp, chicken, and eggs.");
assertEquals(prompt, response);
}

Expand Down