Basic Information Extraction
Copyright 2025 Google LLC.
Gemini API: Basic information extraction
This example notebook shows how Gemini API's Python SDK can be used to extract information from a block of text and return it in defined structure.
In this notebook, the LLM is given a recipe and is asked to extract all the ingredients to create a shopping list. According to best practices, complex tasks will be executed better if divided into separate steps, such as:
-
First, the model will extract all the groceries into a list.
-
Then, you will prompt it to convert this list into a shopping list.
You can find more tips for writing prompts here.
Configure your API key
To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see Authentication for an example.
Additionally, select the model you want to use from the available options below:
Example
First, start by extracting all the groceries. To dod this, set the system instructions when defining the model
Next, the recipe is defined. You will pass the recipe into generate_content, and see that the list of groceries was successfully extracted from the input.
- 3 garlic cloves - knob of fresh ginger - 3 spring onions - 2 tbsp of clear honey - 1 orange - 1 tbsp of light soy sauce - 2 tbsp of vegetable oil - 4 small chicken breast fillets - 20 button mushrooms - 20 cherry tomatoes - 2 large red peppers
The next step is to further format the shopping list based on the ingredients extracted.
Now that you have defined the instructions, you can also decide how you want to format your grocery list. Give the prompt a couple examples, or perform few-shot prompting, so it understands how to format your grocery list.
VEGETABLES
- 3 garlic cloves
- knob of fresh ginger
- 3 spring onions
- 20 button mushrooms
- 20 cherry tomatoes
- 2 large red peppers
FRUITS
- 1 orange
MEAT
- 4 small chicken breast fillets
SAUCES & OILS
- 2 tbsp of clear honey
- 1 tbsp of light soy sauce
- 2 tbsp of vegetable oil
Next steps
Be sure to explore other examples of prompting in the repository. Try creating your own prompts for information extraction or adapt the ones provided in the notebook.