-
Notifications
You must be signed in to change notification settings - Fork 613
feat(py): add support for ai.define_resource #3993
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @yesudeep, 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 significantly enhances the Genkit Python SDK by introducing a robust resource management system. It allows developers to define and register various types of resources, identified by URIs or URI templates, which can then be resolved and processed within Genkit flows. This feature provides a standardized way to handle external or dynamic content, improving the framework's flexibility and extensibility for AI applications. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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 adds support for defining resources in Python, a key feature for managing addressable content. The implementation is comprehensive, including registration, dynamic resources, and matching logic. I've identified a few areas for improvement in the new resource.py file. Key suggestions include using the uritemplate library for robust URI template matching, removing dead and commented-out code, and clarifying some complex logic. The addition of tests is great and covers the new functionality well.
| except Exception as e: | ||
| raise e | ||
|
|
||
| # Since p.metadata is a dict in Pydantic RootModel for Metadata usually, assuming it's accessible. | ||
| # Part -> ResourcePart | etc. ResourcePart has resource: Resource1. | ||
| # But the JS code puts it in metadata.resource. | ||
| # In Python typing.py, Metadata is RootModel[dict[str, Any]]. | ||
|
|
||
| if 'resource' in p_metadata: | ||
| if 'parent' not in p_metadata['resource']: | ||
| p_metadata['resource']['parent'] = {'uri': input_data.uri} | ||
| if opts.get('template'): | ||
| p_metadata['resource']['parent']['template'] = opts.get('template') | ||
| else: | ||
| p_metadata['resource'] = {'uri': input_data.uri} | ||
| if opts.get('template'): | ||
| p_metadata['resource']['template'] = opts.get('template') | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if template_opt: | ||
| # Naive implementation of template matching | ||
| # Converts {variable} to ([^/]+) | ||
| pattern_str = re.escape(template_opt).replace(r'\{', '{').replace(r'\}', '}') | ||
| # Handle simple variables {var} | ||
| pattern_str = re.sub(r'\{[^}]+\}', r'([^/]+)', pattern_str) | ||
| # Ensure full match | ||
| pattern = re.compile(f'^{pattern_str}$') | ||
|
|
||
| return lambda input: pattern.match(input.uri) is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The uritemplate library was added as a dependency, but create_matcher uses a naive regex-based implementation for URI templates. This implementation is limited and doesn't fully support the RFC6570 specification mentioned in the docstring. Using the uritemplate library would provide a more robust and compliant solution.
| if template_opt: | |
| # Naive implementation of template matching | |
| # Converts {variable} to ([^/]+) | |
| pattern_str = re.escape(template_opt).replace(r'\{', '{').replace(r'\}', '}') | |
| # Handle simple variables {var} | |
| pattern_str = re.sub(r'\{[^}]+\}', r'([^/]+)', pattern_str) | |
| # Ensure full match | |
| pattern = re.compile(f'^{pattern_str}$') | |
| return lambda input: pattern.match(input.uri) is not None | |
| if template_opt: | |
| template = uritemplate.URITemplate(template_opt) | |
| return lambda input: template.match(input.uri) is not None |
| p_metadata['resource']['template'] = opts.get('template') | ||
|
|
||
| return parts | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e9c7d65 to
51aa9f4
Compare
51aa9f4 to
fb47e1e
Compare
fb47e1e to
27a1ad0
Compare
No description provided.