Skip to content

Add Self-Improving Prompts notebook#1120

Open
Karanjot786 wants to merge 3 commits intogoogle-gemini:mainfrom
Karanjot786:add-self-improving-prompts
Open

Add Self-Improving Prompts notebook#1120
Karanjot786 wants to merge 3 commits intogoogle-gemini:mainfrom
Karanjot786:add-self-improving-prompts

Conversation

@Karanjot786
Copy link

Closes #1110

What this PR does

Adds a new prompting example showing how to use Gemini to optimize prompts through self-critique.

Changes

  • New notebook: examples/prompting/Self_Improving_Prompts.ipynb
  • Updated examples/prompting/README.md with new entry

Notebook contents

  1. Start with a weak prompt
  2. Run the prompt and get output
  3. Have Gemini critique the prompt
  4. Have Gemini rewrite the prompt based on critique
  5. Run the improved prompt
  6. Compare before and after results
  7. Reusable improve_prompt() function for any prompt

Example output

Before: Generic 2-sentence summary
After: Single sentence, 25 words, specific tone and constraints

The improve_prompt() function took "Write a poem about coding" and after 2 iterations produced a full Petrarchan sonnet about refactoring code.

Testing

  • Ran all cells in Colab
  • Verified outputs show clear improvement
  • Formatted with nbfmt
  • Linted with nblint

cc @Giom-V @patrickloeber

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Jan 25, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Karanjot786, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a novel approach to prompt engineering by enabling the Gemini model to autonomously refine its own prompts. The new notebook provides a comprehensive guide and a reusable function for this self-critique and iterative improvement process, aiming to enhance prompt effectiveness and streamline the development of high-quality AI interactions.

Highlights

  • New Self-Improving Prompts Notebook: A new Jupyter notebook, Self_Improving_Prompts.ipynb, has been added to demonstrate how to use the Gemini model to iteratively critique and improve prompts.
  • Iterative Prompt Refinement Pattern: The notebook introduces a pattern where an initial prompt is run, Gemini critiques its output, Gemini rewrites the prompt based on the critique, and the improved prompt is then executed, repeating until satisfactory results are achieved.
  • Reusable improve_prompt() Function: A Python function, improve_prompt(), is provided to encapsulate the self-improvement loop, making it easy to apply this technique to any prompt.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@Karanjot786
Copy link
Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new notebook, Self_Improving_Prompts.ipynb, which is a great example of using Gemini to iteratively refine prompts. The notebook is well-structured and clearly demonstrates the self-improvement pattern. I've added a few comments with suggestions to align the notebook with the repository's style guide, mainly concerning notebook formatting, use of modern HTML, and docstring completeness. Overall, this is a valuable addition to the cookbook.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new notebook, Self_Improving_Prompts.ipynb, which is a great example of using Gemini to iteratively refine prompts. The notebook is well-structured and the concept is clearly explained. The update to the README.md is also correct. My review includes a few suggestions to align the notebook with the repository's style guide, mainly concerning notebook metadata, HTML formatting, and docstring completeness.

@Karanjot786
Copy link
Author

Thanks for the review! I've addressed the feedback:

  • Ran nbfmt on the notebook (note: similar to other existing notebooks in the repo, execution counts are preserved)
  • Replaced all deprecated HTML (bgcolor, <font>) with modern CSS
  • Added # @title to hide the helper function
  • Improved the improve_prompt docstring with Google-style type hints

@Karanjot786
Copy link
Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an excellent new notebook, Self_Improving_Prompts.ipynb, which clearly demonstrates how to use Gemini for iterative prompt improvement. The example is well-structured, starting with a simple case and building up to a reusable function. The update to the README.md is also correct.

My review includes a couple of suggestions to improve consistency with the rest of the cookbook, mainly regarding notebook formatting and the method for retrieving API keys in Colab. These changes will help maintain a uniform style across all examples, making them easier for users to follow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:examples Issues/PR referencing examples folder status:awaiting review PR awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add example notebook: Self-Improving Prompts with Gemini

1 participant