-
Notifications
You must be signed in to change notification settings - Fork 116
feat: python tools requirement #1040
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
Open
akihikokuroda
wants to merge
13
commits into
generative-computing:main
Choose a base branch
from
akihikokuroda:pythonrequirement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,626
−14
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dbef526
python tools requirement
akihikokuroda 119699e
review comments
akihikokuroda 9055101
review comments
akihikokuroda 036cfee
review comments
akihikokuroda e3d00f3
review comments
akihikokuroda 577eb00
review comments
akihikokuroda 41f09cc
matplotlib restructure
akihikokuroda 85c5c67
review cmments
akihikokuroda 8f391da
fix doc strings
akihikokuroda 8e24e97
review comments
akihikokuroda b7261ea
review comment
akihikokuroda 94051c9
review comments
akihikokuroda acbef75
review comments
akihikokuroda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
|
markstur marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # pytest: ollama, e2e, qualitative | ||
| """Repair plotting code with Python-tool and plotting-specific requirements. | ||
|
|
||
| This example demonstrates the full tool lifecycle: | ||
| 1. Model generates code and creates tool calls | ||
| 2. Sampling validation checks code quality without execution | ||
| 3. Tool is explicitly invoked after sampling succeeds (via _call_tools) | ||
| 4. Results are returned to caller for inspection/handling | ||
|
|
||
| Key insight: Tool execution is explicit and controlled by the caller, | ||
| not automatic within the sampling pipeline. This allows fine-grained control | ||
| over when/if tools are invoked, and enables safety checks (see tool_hooks.py). | ||
|
|
||
| Prerequisites: | ||
| matplotlib must be installed for code execution to succeed: | ||
| $ uv pip install matplotlib | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just run it like this:
And I think it is really cool that temporary deps work like that but I'm not sure how many people have bought into using --with |
||
| """ | ||
|
|
||
| import tempfile | ||
| import traceback | ||
| from pathlib import Path | ||
|
|
||
| import mellea | ||
| from mellea.backends import ModelOption | ||
| from mellea.backends.tools import MelleaTool | ||
| from mellea.stdlib.functional import _call_tools | ||
| from mellea.stdlib.requirements import ( | ||
| python_plotting_requirements, | ||
| python_tool_requirements, | ||
| ) | ||
| from mellea.stdlib.sampling import SOFAISamplingStrategy | ||
| from mellea.stdlib.tools import local_code_interpreter | ||
| from mellea.stdlib.tools.interpreter import ExecutionResult | ||
|
|
||
|
|
||
| def python(code: str) -> ExecutionResult: | ||
| """Execute Python code. | ||
|
|
||
| Args: | ||
| code: Python code to execute | ||
|
|
||
| Returns: | ||
| Execution result containing stdout, stderr, and success status | ||
| """ | ||
| return local_code_interpreter(code) | ||
|
|
||
|
|
||
| def main(): | ||
| """Run the plotting repair example.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| output_path = str(Path(tmpdir) / "plot.png") | ||
|
|
||
| m = mellea.start_session(context_type="chat") | ||
|
|
||
| requirements = [ | ||
| *python_tool_requirements(allowed_imports=["numpy", "matplotlib", "math"]), | ||
| *python_plotting_requirements(output_path=output_path), | ||
| ] | ||
|
|
||
| sampling_strategy = SOFAISamplingStrategy( | ||
| s1_solver_backend=m.backend, | ||
| s2_solver_backend=m.backend, | ||
| s2_solver_mode="fresh_start", | ||
| loop_budget=3, | ||
| feedback_strategy="first_error", | ||
| ) | ||
|
|
||
| task_summary = ( | ||
| f"Create a plot of sin(x) for x in 0..2π and save it to {output_path}" | ||
| ) | ||
|
|
||
| print("=" * 70) | ||
| print("Testing plotting-code repair with Python tool requirements") | ||
| print("=" * 70) | ||
| print(f"Task: {task_summary}\n") | ||
|
|
||
| try: | ||
| result = m.instruct( | ||
| task_summary, | ||
| requirements=requirements, | ||
| strategy=sampling_strategy, | ||
| return_sampling_results=True, | ||
| tool_calls=True, | ||
| model_options={ModelOption.TOOLS: [MelleaTool.from_callable(python)]}, | ||
| ) | ||
|
|
||
| print(f"\nResult: {'SUCCESS' if result.success else 'FAILED'}\n") | ||
|
|
||
| if result.success: | ||
| print("✓ Model successfully generated plotting code") | ||
|
|
||
| # Invoke the generated tools from the final result | ||
| if ( | ||
| result.result | ||
| and hasattr(result.result, "tool_calls") | ||
| and result.result.tool_calls | ||
| ): | ||
| # Print the generated code | ||
| for tool_name, tool_call in result.result.tool_calls.items(): | ||
| if tool_call.args and "code" in tool_call.args: | ||
| code = tool_call.args["code"] | ||
| print(f"\n{'=' * 70}") | ||
| print(f"Generated Python code for tool '{tool_name}':") | ||
| print(f"{'=' * 70}") | ||
| print(code) | ||
| print(f"{'=' * 70}\n") | ||
|
|
||
| tool_outputs = _call_tools(result.result, m.backend) | ||
|
|
||
| if tool_outputs: | ||
| print("✓ Tool executed successfully") | ||
| for i, output in enumerate(tool_outputs, 1): | ||
| print(f" Output {i}: {output.content}") | ||
| else: | ||
| print("ℹ No tool calls in final result") | ||
|
|
||
| print(f"\nCode saved to: {output_path}") | ||
|
|
||
| print(f"\nRepair iterations: {len(result.sample_validations)}") | ||
| for attempt_idx, validations in enumerate(result.sample_validations, 1): | ||
| passed = sum(1 for _, val in validations if val.as_bool()) | ||
| total = len(validations) | ||
| status = "✓" if passed == total else "✗" | ||
| print( | ||
| f" {status} Attempt {attempt_idx}: {passed}/{total} " | ||
| f"requirements passed" | ||
| ) | ||
|
|
||
| for req, val in validations: | ||
| if not val.as_bool(): | ||
| print(f" - {req.description}") | ||
| if val.reason: | ||
| reason_preview = val.reason[:100].replace("\n", " ") | ||
| print(f" Error: {reason_preview}...") | ||
|
|
||
| else: | ||
| print("✗ Failed to generate working plotting code after all attempts\n") | ||
| print("Last attempt output:") | ||
| print("-" * 70) | ||
| print(result.result.value) | ||
| print("-" * 70) | ||
|
|
||
| print(f"\nFailure history ({len(result.sample_validations)} attempts):") | ||
| for attempt_idx, validations in enumerate(result.sample_validations, 1): | ||
| failed_count = sum(1 for _, val in validations if not val.as_bool()) | ||
| if failed_count > 0: | ||
| print(f"\n Attempt {attempt_idx}:") | ||
| for req, val in validations: | ||
| if not val.as_bool(): | ||
| print(f" - {req.description}") | ||
| if val.reason: | ||
| reason_lines = val.reason.split("\n")[:2] | ||
| for line in reason_lines: | ||
| print(f" {line}") | ||
|
|
||
| except Exception as e: | ||
| print(f"✗ Exception during sampling: {e}") | ||
| traceback.print_exc() | ||
|
|
||
| print("\n" + "=" * 70) | ||
| print("Test completed") | ||
| print("=" * 70) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
| # Made with Bob | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Plotting-specific requirements for Python tool validation. | ||
|
|
||
| Provides matplotlib and plotting-focused requirement factories separate from | ||
| generic Python tool requirements. | ||
| """ | ||
|
|
||
| from .matplotlib import python_plotting_requirements | ||
|
|
||
| __all__ = ["python_plotting_requirements"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We haven't been putting files directly into docs/examples. Please create a folder for this. I'm not sure what the folder should be; maybe it can go in the existing tools dir? I also see that
docs/examples/as_generic_chat_history.pyis in that same directory, can you please move it as well (either in this PR or a separate one).