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

feat(py/ai/generate): implemented basic veneer for the generate action #2179

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

pavelgj
Copy link
Collaborator

@pavelgj pavelgj commented Feb 27, 2025

Checklist (if applicable):

@github-actions github-actions bot added the python Python label Feb 27, 2025
Copy link

@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.

Hello @pavelgj, 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!

Summary of Changes

This pull request introduces a basic veneer for the generate action in the Genkit AI module. It includes changes to testing utilities, core codec functionality, and the Genkit veneer itself to support more flexible and comprehensive text generation capabilities. The changes involve adding new functions for defining programmable models and echo models for testing, updating the dump_json function to exclude None values, and enhancing the generate method in the Genkit veneer to support various input types and configurations.

Highlights

  • Testing Utilities: Introduces define_programmable_model and define_echo_model in testing_utils.py to simplify the creation of mock models for testing purposes.
  • Core Codec Update: Modifies dump_json in core/codec.py to exclude None values when serializing BaseModel instances, ensuring cleaner JSON outputs.
  • Veneer Enhancement: Updates the generate method in veneer/veneer.py to support Part objects and lists of Part objects as input, along with various configuration options for tool usage, output formatting, and constrained generation.
  • Veneer Testing: Adds comprehensive tests in veneer/veneer_test.py to verify the functionality of the generate method with different input types, configurations, and tool usage scenarios.

Changelog

Click here to see the changelog
  • py/packages/genkit/src/genkit/ai/generate_test.py
    • Replaces the local ProgrammableModel definition with the one from genkit.ai.testing_utils.
    • Updates tests to use define_programmable_model for setting up programmable models.
  • py/packages/genkit/src/genkit/ai/testing_utils.py
    • Introduces ProgrammableModel class for configurable model responses and streaming chunks.
    • Adds define_programmable_model function to define programmable models within Genkit.
    • Adds EchoModel class and define_echo_model function for simple echo model definitions.
  • py/packages/genkit/src/genkit/core/codec.py
    • Modifies dump_json to exclude None values when serializing BaseModel instances.
  • py/packages/genkit/src/genkit/veneer/veneer.py
    • Updates the generate method to support Part objects and lists of Part objects as input.
    • Adds support for various configuration options, including tools, return_tool_requests, tool_choice, output_format, content_type, output_schema, and constrained.
    • Normalizes prompt arguments using normalize_prompt_arg to handle different input types.
    • Introduces to_json_schema function to convert schemas to JSON schema format.
    • Returns an Action object when defining a model.
  • py/packages/genkit/src/genkit/veneer/veneer_test.py
    • Adds tests for generate method with different input types (string, Part, list of Parts).
    • Adds tests for system prompts, messages, and tool usage scenarios.
    • Adds tests for output configuration options like output_format, content_type, and output_schema.
  • py/samples/hello/src/foo.py
    • This file appears to be unrelated to the changes described in the PR title and description. It includes code for threading, asyncio, and generator consumption, but its purpose within the context of the PR is unclear.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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.


Did you know?

The term 'veneer' comes from the woodworking industry, where it refers to a thin layer of material applied to the surface of another material, typically for decorative purposes. In software, a veneer provides a similar function, offering a simplified or enhanced interface to an underlying system.

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.

Copy link

@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

The pull request introduces a basic veneer for the generate action, including testing utilities and updates to core codecs. The changes look good overall, with a few suggestions for improvement to enhance clarity and maintainability. Please have others review and approve this code before merging.

Summary of Findings

  • Clarity in testing utilities: The introduction of define_programmable_model and define_echo_model enhances testability. Consider adding docstrings to these functions to explain their purpose and usage.
  • JSON serialization: The change to dump_json to exclude None values is a good improvement. Ensure this behavior is consistent across the codebase.
  • Veneer API consistency: The generate method in Veneer now accepts more flexible input types (Part, list[Part]). Ensure all possible input combinations are tested thoroughly.

Assessment

The pull request introduces a basic veneer for the generate action, which is a valuable addition. The code is generally well-structured and the changes are clear. I recommend addressing the comments to improve clarity and consistency. After addressing these points, the pull request should be in good shape for merging, but please have others review and approve this code before merging.

def __init__(self):
self.request_idx = 0
self.responses = []
self.chunks = None
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to redefine them in init, as you already set them default values. They are defined on a class method, but if you like, you can move everything to init.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved everything to init... I can't figure out why, but somehow these fields behave like static fields otherwise. Literally, the tests fail because responses ends up with test data from multiple tests. It works fine when everything is in init. 🤷

Copy link
Contributor

Choose a reason for hiding this comment

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

The fields you have in class are called class attributes, and they have different behavious.
The fields defined in init are simple fields.

@pavelgj pavelgj requested a review from Irillit February 27, 2025 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Python
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants