Skip to content

Commit 61433d6

Browse files
authored
Enhance Tools Template with Conditional Tool Selection (#5)
* Enhance Tools Template with Conditional Tool Selection - Add support for dynamic tool selection using `.with_var()` method - Implement tools_template processing to conditionally activate tools based on variables - Update README with comprehensive examples of conditional tool selection - Add new example script demonstrating advanced tools template usage - Improve builder's tools processing logic to handle various template output formats - Add tests for conditional tools and variable handling * Updated Builder pattern * Update CHANGELOG for version 0.1.10 release * Bump version to 0.1.10 in pyproject.toml
1 parent bedd37d commit 61433d6

File tree

10 files changed

+514
-294
lines changed

10 files changed

+514
-294
lines changed

CHANGELOG.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [0.1.10] - 2025-03-12
4+
5+
### Added
6+
- Enhanced tools_template functionality: Variables set with `.with_var()` are now accessible in tools_template for conditional tool selection
7+
- Added example showcasing conditional tools selection based on variables
8+
- Added comprehensive tests for conditional tools feature
9+
310
## [0.1.9] - 2025-03-03
411

512
## Changed
@@ -12,19 +19,19 @@
1219
- Updated PyProject.toml and Added MANIFEST.in
1320
- Making the Promptix Studio fully functional.
1421

15-
## [0.1.7] - 2024-05-18
22+
## [0.1.7] - 2025-03-02
1623

1724
### Changed
1825
- Updated code with latest improvements
1926
- Fixed minor issues from previous release
2027

21-
## [0.1.6] - 2024-05-17
28+
## [0.1.6] - 2025-03-02
2229

2330
### Added
2431
- Improved Promptix Studio with enhanced user interface and functionality
2532
- Updated License with additional clarifications
2633

27-
## [0.1.5] - 2024-05-15
34+
## [0.1.5] - 2025-02-27
2835

2936
### Added
3037
- Improved documentation for builder patterns
@@ -35,7 +42,7 @@
3542
- Refined API interface for better developer experience
3643
- Optimized template rendering for better performance
3744

38-
## [0.1.4] - 2024-02-02
45+
## [0.1.4] - 2025-02-02
3946

4047
### Added
4148
- Builder pattern support for creating model configurations
@@ -49,7 +56,7 @@
4956
- Improved documentation with builder pattern examples
5057
- Added type hints and validation for builder methods
5158

52-
## [0.1.3] - 2024-01-26
59+
## [0.1.3] - 2025-02-26
5360

5461
### Added
5562
- OpenAI integration support with prepare_model_config functionality
@@ -61,7 +68,7 @@
6168
- Improved error handling for invalid memory formats
6269
- Updated documentation with OpenAI integration examples
6370

64-
## [0.1.2] - 2024-03-19
71+
## [0.1.2] - 2025-02-19
6572

6673
### Added
6774
- New DungeonMaster template for RPG scenario generation
@@ -74,7 +81,7 @@
7481
- Improved test coverage for complex scenarios
7582
- Updated template validation for optional fields
7683

77-
## [0.1.1] - 2024-01-20
84+
## [0.1.1] - 2025-01-20
7885

7986
### Added
8087
- Enhanced schema validation with warning system for missing fields
@@ -92,7 +99,7 @@
9299
- Fixed handling of custom_data and nested fields
93100
- Fixed test environment cleanup and prompts.json handling
94101

95-
## [0.1.0] - 2024-01-19
102+
## [0.1.0] - 2025-01-19
96103

97104
### Added
98105
- Initial release of Promptix Library

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ system_instruction = (
112112
)
113113
```
114114

115+
### 🔧 Conditional Tool Selection
116+
Variables set using `.with_var()` are available in tools_template allowing for dynamic tool selection based on variables:
117+
118+
```python
119+
# Conditionally select tools based on variables
120+
config = (
121+
Promptix.builder("ComplexCodeReviewer")
122+
.with_var({
123+
'programming_language': 'Python', # This affects which tools are selected
124+
'severity': 'high',
125+
'review_focus': 'security'
126+
})
127+
.build()
128+
)
129+
130+
# Explicitly added tools will override template selections
131+
config = (
132+
Promptix.builder("ComplexCodeReviewer")
133+
.with_var({
134+
'programming_language': 'Java',
135+
'severity': 'medium'
136+
})
137+
.with_tool("complexity_analyzer") # This tool will be included regardless of template logic
138+
.with_tool_parameter("complexity_analyzer", "thresholds", {"cyclomatic": 10})
139+
.build()
140+
)
141+
```
142+
143+
This allows you to create sophisticated tools configurations that adapt based on input variables, with the ability to override the template logic when needed.
144+
115145
## 🚀 Getting Started
116146

117147
### Installation

examples/03_builder_usage.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,48 @@ def main():
9090
)
9191
print_config(config4)
9292

93+
# Example 6: Using with_var for multiple variables at once
94+
print("Example 6: Using with_var Method")
95+
config5 = (
96+
Promptix.builder("ComplexCodeReviewer")
97+
.with_var({
98+
'programming_language': 'Python',
99+
'severity': 'high',
100+
'review_focus': 'security and performance'
101+
})
102+
.build()
103+
)
104+
print_config(config5)
105+
106+
# Example 7: Using with_var for multiple variables which dicpates tools structure
107+
print("Example 7: Using with_var Method")
108+
config6 = (
109+
Promptix.builder("ComplexCodeReviewer")
110+
.with_var({
111+
'programming_language': 'Java',
112+
'severity': 'high',
113+
'review_focus': 'security and performance'
114+
})
115+
.with_tool_parameter("complexity_analyzer", "thresholds", {"cyclomatic": 8, "cognitive": 5})
116+
.build()
117+
118+
)
119+
print_config(config6)
120+
121+
# Example 8: Using with_var for multiple variables which dicpates tools structure
122+
print("Example 8: Using with_var Method")
123+
config7 = (
124+
Promptix.builder("ComplexCodeReviewer")
125+
.with_var({
126+
'programming_language': 'Java',
127+
'severity': 'high',
128+
'review_focus': 'security and performance'
129+
})
130+
.build()
131+
132+
)
133+
print_config(config7)
134+
135+
93136
if __name__ == "__main__":
94137
main()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Example demonstrating how to use variables with tools_template in Promptix.
4+
5+
This example shows how variables set with .with_var() can be used in tools_template
6+
for conditional tool selection based on variable values.
7+
"""
8+
9+
import sys
10+
import os
11+
import json
12+
13+
# Add the src directory to the path so we can import promptix
14+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
15+
16+
from src.promptix import Promptix
17+
18+
def print_config(config):
19+
"""Print a configuration in a readable format."""
20+
if isinstance(config, dict):
21+
print(json.dumps(config, indent=2))
22+
else:
23+
print(config)
24+
print()
25+
26+
def main():
27+
"""Run the examples."""
28+
# Example 1: Using variables to conditionally select tools via tools_template
29+
print("Example 1: Conditional Tools with Python")
30+
print("(Tools template automatically selects complexity_analyzer and security_scanner for Python)")
31+
config1 = (
32+
Promptix.builder("ComplexCodeReviewer")
33+
.with_var({
34+
'programming_language': 'Python', # This will activate Python tools
35+
'severity': 'high',
36+
'review_focus': 'security and performance'
37+
})
38+
.build()
39+
)
40+
print_config(config1)
41+
42+
# Example 2: Using different variables to select different tools
43+
print("Example 2: Conditional Tools with Java")
44+
print("(Tools template automatically selects style_checker for Java)")
45+
config2 = (
46+
Promptix.builder("ComplexCodeReviewer")
47+
.with_var({
48+
'programming_language': 'Java', # This will activate Java tools
49+
'severity': 'medium',
50+
'review_focus': 'security'
51+
})
52+
.build()
53+
)
54+
print_config(config2)
55+
56+
# Example 3: Overriding tools_template with explicit tool selection
57+
print("Example 3: Overriding tools_template with explicit tool selection")
58+
print("(Explicitly adding tools that wouldn't be selected by the template)")
59+
config3 = (
60+
Promptix.builder("ComplexCodeReviewer")
61+
.with_var({
62+
'programming_language': 'JavaScript', # No tools defined for JavaScript in template
63+
'severity': 'low',
64+
'review_focus': 'performance'
65+
})
66+
.with_tool("complexity_analyzer") # Explicitly adding tools
67+
.with_tool("security_scanner")
68+
.with_tool_parameter("complexity_analyzer", "thresholds", {"cyclomatic": 8, "cognitive": 5})
69+
.build()
70+
)
71+
print_config(config3)
72+
73+
# Example 4: Combining template-selected and explicitly-selected tools
74+
print("Example 4: Combining template and explicit tool selection")
75+
print("(Template selects style_checker for Java, and we explicitly add complexity_analyzer)")
76+
config4 = (
77+
Promptix.builder("ComplexCodeReviewer")
78+
.with_var({
79+
'programming_language': 'Java', # Template selects style_checker for Java
80+
'severity': 'high',
81+
'review_focus': 'security and performance'
82+
})
83+
.with_extra({
84+
'temperature': 0.9
85+
})
86+
.with_tool("complexity_analyzer") # Explicitly add another tool
87+
.build()
88+
)
89+
print_config(config4)
90+
91+
if __name__ == "__main__":
92+
main()

0 commit comments

Comments
 (0)