Orion Innovation techClub

Orion Innovation Technology

Follow publication

Using ChatGPT as a Custom API Provider and Making an App by Using It

Hello Everyone 👋 In this article, we will explore how to develop a mobile application by using ChatGPT from OpenAI’s APIs.

Photo by S O C I A L . C U T on Unsplash

here is the related app code repository https://github.com/admcilgin/chatGPT_recipe_finder

Firstly, to use OpenAI APIs, you need to have a developer account. You can join as a developer via this link. After registering, we need an API key. You can obtain a new API key from this link. I recommend you keep your API key confidential and be cautious when pushing your code to GitHub. In this project, I placed the API key in the local.properties file. To use the code, you need to enter the API key in your local.properties file as follows:

apiKey=sk-….

Prompt creation is one of the most crucial aspects to consider when using ChatGPT as the backbone of our application. Regardless of what I demand, I aim to receive a JSON output from ChatGPT in a predefined format. Even if a suitable recipe is not found, I would still expect a JSON output from ChatGPT. I will also inject certain parameters into the prompt, such as language, recipe type, and ingredients.

In my application, I chose to use the gpt-3.5-turbo model, although gpt-4 can also be used. I opted for gpt-3.5-turbo due to its lower cost. However, more impressive results can be obtained when using gpt-4. You can see the documentation comparing the models from this link.

In my code, I use cases to get a recipe and an image which is about the recipe and, Here are the prompt and use cases

class  RecipeFinderUseCase @Inject constructor(
private val completionRepository: ChatRepository
) {
operator fun invoke(ingredients: String,type: String,language: String) =
completionRepository.generateChat(ChatRequestBody(
model = "gpt-3.5-turbo",
messages = arrayListOf(
Messages(
role = "user",
content = "You are a recipe finder bot. The user will provide a list of ingredients to you. " +
"Your task is to find recipes using the given ingredients. " +
"Ensure that the ingredients are real, edible, and commonly used in cooking." +
" Return the recipe, along with the list of ingredients and instructions." +
" Furthermore, generate a brief 30-character image prompt describing the recipe. " +
" And also put estimated calories and prepare time, type of dish, and the language of the recipe in the response. " +
"Return all the information in JSON format. " +
"The response should be in the language specified by the user. " +
"If you encounter an error, or cannot find a recipe with the provided ingredients, return a random recipe instead. " +
"Do not return any error messages or explanations. If the user provides unreal or inedible ingredients, or does not provide ingredients or the type of dish, return a random recipe. " +
"Your response should only contain the JSON content, and no additional text. Here is an example of the JSON format: \n" +
"\n" +
" {\n" +
" 'name': 'Vanilla Cake',\n" +
" 'language': 'en',\n" +
" 'image_prompt': 'A cake with strawberries on top.',\n" +
" 'type': 'Dessert, Cake',\n" +
" 'calories': '490',\n" +
" 'prepare_time': '1 hour',\n" +
" 'ingredients': [\n" +
" '2 cups of all-purpose flour',\n" +
" '2 large eggs'\n" +
" ],\n" +
" 'steps': [\n" +
" 'Preheat the oven to 350°F (175°C) and grease an 8-inch cake pan with butter or cooking spray.',\n" +
" 'In a medium bowl, whisk together the flour, baking powder, and salt. Set aside.'\n" +
" ]\n" +
" }\n" +
"\n" +
"Here are the ingredients: \" + $ingredients + \" and the type of dish: \" + $type + \". Please provide the recipe in this language: \" + $language",
),

),
))

}

In this application, I utilized use cases for image and recipe generation.

A better prompt could be written, but I have consistently returned a JSON in the format I desired with such a prompt. For example, it produces a JSON like the following :

{
"name": "Apple and Citrus Salmon Salad",
"language": "English",
"image_prompt": "Salmon salad with apples and clementines",
"type": "Salad, Fish",
"calories": "320",
"prepare_time": "25 minutes",
"ingredients": [
"1 lb salmon fillet",
"1 apple, sliced",
"2 clementines, peeled and segmented",
"5 oz mixed greens",
"1/4 cup chopped almonds",
"2 tbsp olive oil",
"1 tbsp white wine vinegar",
"1 tsp Dijon mustard",
"Salt and pepper, to taste"
],
"steps": [
"Season salmon fillet with salt and pepper, then cook in a non-stick skillet over medium-high heat for 4-5 minutes per side. Set aside to cool.",
"In a small bowl, whisk together olive oil, white wine vinegar, Dijon mustard, salt, and pepper to make the dressing.",
"In a large bowl, toss together mixed greens, sliced apple, segmented clementines, and chopped almonds.",
"Flake the cooled salmon into bite-sized pieces and add to the salad.",
"Drizzle with the dressing and toss gently to combine."
]
}

I display this JSON on the screen. I then use the ‘image_prompt’ in the returned JSON and OpenAI’s image generation API to create an image suitable for this recipe.

class GenerateImageUseCase @Inject constructor(
private val imageRepository: ImageRepository
) {
operator fun invoke(prompt: String) = imageRepository.generateImage(ImageRequestBody(prompt))
}

Users can input ingredients, and the language of the recipe, and also users can choose recipe categories. Here are some screenshots of the application:

recipe result screenshot

You can review all the code from this link.

Sign up to discover human stories that deepen your understanding of the world.

No responses yet

Write a response