Skip to content

Commit 602217f

Browse files
authored
feat: Implement builder pattern for model configuration with multi-pr… (#2)
* feat: Implement builder pattern for model configuration with multi-provider support - Add comprehensive builder pattern for creating model configurations - Introduce support for OpenAI and Anthropic API integrations - Enhance prompt template configuration with flexible builder methods - Update prompts.json with improved schema definitions - Add builder usage examples and test suite - Extend README with builder pattern documentation - Bump version to 0.1.4 * docs: Add PyPI downloads badge to README * ci: Refactor test dependencies and GitHub Actions workflow - Remove dev dependencies from main requirements.txt - Add separate requirements-test.txt for test dependencies - Update CI workflow to install test requirements separately - Add environment variables for API keys in test workflow * ci: Install package in editable mode for testing - Add `pip install -e .` to CI workflow - Ensures tests can import local package during GitHub Actions runs * - Update test_prepare_model_config_required_fields to use specific error message matching
1 parent b49d02d commit 602217f

File tree

11 files changed

+723
-50
lines changed

11 files changed

+723
-50
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ jobs:
2626
- name: Install dependencies
2727
run: |
2828
python -m pip install --upgrade pip
29-
pip install -e ".[dev]"
29+
pip install -r requirements.txt
30+
pip install -r requirements-test.txt
31+
pip install -e .
3032
3133
- name: Run tests
34+
env:
35+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
36+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
3237
run: |
3338
pytest --cov=promptix --cov-report=xml
3439

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [0.1.4] - 2024-02-02
4+
5+
### Added
6+
- Builder pattern support for creating model configurations
7+
- New builder classes for CustomerSupport and CodeReview templates
8+
- Integration with both OpenAI and Anthropic APIs through builders
9+
- Comprehensive test suite for builder pattern functionality
10+
- Example implementations showing builder pattern usage
11+
12+
### Changed
13+
- Enhanced model configuration preparation with builder pattern
14+
- Improved documentation with builder pattern examples
15+
- Added type hints and validation for builder methods
16+
317
## [0.1.3] - 2024-01-26
418

519
### Added

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![PyPI version](https://badge.fury.io/py/promptix.svg)](https://badge.fury.io/py/promptix)
44
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
55
[![Python Versions](https://img.shields.io/pypi/pyversions/promptix.svg)](https://pypi.org/project/promptix/)
6+
[![PyPI Downloads](https://static.pepy.tech/badge/promptix)](https://pepy.tech/projects/promptix)
67

78
A Python library for managing and using prompts with Promptix Studio integration. Promptix makes it easy to manage, version, and use prompts in your applications with a built-in web interface.
89

@@ -87,6 +88,61 @@ model_config = Promptix.prepare_model_config(
8788
response = client.chat.completions.create(**model_config)
8889
```
8990

91+
## Builder Pattern
92+
93+
Promptix provides a fluent builder pattern interface for creating model configurations:
94+
95+
```python
96+
from promptix import Promptix
97+
import openai
98+
99+
client = openai.OpenAI()
100+
101+
# Using builder pattern for CustomerSupport
102+
model_config = (
103+
Promptix.builder("CustomerSupport")
104+
.with_user_name("John Doe")
105+
.with_issue_type("account_settings")
106+
.with_issue_description("User cannot access account settings page")
107+
.with_technical_level("intermediate")
108+
.with_priority("medium")
109+
.with_memory([
110+
{"role": "user", "content": "I'm having trouble with my account settings"}
111+
])
112+
.build()
113+
)
114+
115+
response = client.chat.completions.create(**model_config)
116+
117+
# Using builder pattern for Code Review
118+
code_config = (
119+
Promptix.builder("CodeReview")
120+
.with_code_snippet(code_snippet)
121+
.with_programming_language("Python")
122+
.with_review_focus("Security and SQL Injection")
123+
.with_severity("high")
124+
.build()
125+
)
126+
127+
# Anthropic Integration
128+
anthropic_config = (
129+
Promptix.builder("CustomerSupport")
130+
.with_version("v5")
131+
.with_user_name("John Doe")
132+
.with_issue_type("account_settings")
133+
.with_memory(memory)
134+
.for_client("anthropic")
135+
.build()
136+
)
137+
```
138+
139+
The builder pattern provides:
140+
- Type-safe configuration building
141+
- Fluent interface for better code readability
142+
- Automatic validation of required fields
143+
- Support for multiple LLM providers
144+
- Clear separation of configuration concerns
145+
90146
## Advanced Usage
91147

92148
### Version Control

examples/builder_usage.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from promptix import Promptix
2+
import openai
3+
import anthropic
4+
from dotenv import load_dotenv
5+
6+
# Load environment variables
7+
load_dotenv()
8+
9+
def basic_builder_example() -> None:
10+
"""Basic example of using the builder pattern with OpenAI.
11+
12+
This example demonstrates how to use Promptix with OpenAI's API for a customer support scenario.
13+
Requires OPENAI_API_KEY to be set in environment variables.
14+
"""
15+
try:
16+
client = openai.OpenAI() # Will automatically use OPENAI_API_KEY from environment
17+
18+
memory= [
19+
{"role": "user", "content": "I'm having trouble with my account settings"},
20+
]
21+
22+
# Build configuration using the builder pattern with CustomerSupport
23+
model_config = (
24+
Promptix.builder("CustomerSupport")
25+
.with_user_name("John Doe")
26+
.with_issue_type("account_settings")
27+
.with_issue_description("User cannot access account settings page after recent update")
28+
.with_technical_level("intermediate")
29+
.with_priority("medium")
30+
.with_memory(memory)
31+
.build()
32+
)
33+
34+
print("Model Config:", model_config)
35+
print("\n\n")
36+
37+
response = client.chat.completions.create(**model_config)
38+
print("Basic Example Response:", response.choices[0].message.content)
39+
except Exception as e:
40+
print(f"Error in basic builder example: {str(e)}")
41+
42+
43+
def advanced_builder_example() -> None:
44+
"""Advanced example showing code review features with OpenAI.
45+
46+
This example demonstrates how to use Promptix for code review scenarios.
47+
Requires OPENAI_API_KEY to be set in environment variables.
48+
"""
49+
try:
50+
client = openai.OpenAI()
51+
52+
memory= [
53+
{"role": "user", "content": "Can you review this code for security issues?"},
54+
]
55+
56+
code_snippet = '''
57+
def process_user_input(data):
58+
query = f"SELECT * FROM users WHERE id = {data['user_id']}"
59+
return execute_query(query)
60+
'''
61+
62+
model_config = (
63+
Promptix.builder("CodeReview")
64+
.with_code_snippet(code_snippet)
65+
.with_programming_language("Python")
66+
.with_review_focus("Security and SQL Injection")
67+
.with_severity("high")
68+
.with_memory(memory)
69+
.build()
70+
)
71+
72+
print("Model Config:", model_config)
73+
print("\n\n")
74+
75+
response = client.chat.completions.create(**model_config)
76+
print("Advanced Example Response:", response.choices[0].message.content)
77+
except Exception as e:
78+
print(f"Error in advanced builder example: {str(e)}")
79+
80+
81+
def anthropic_builder_example() -> None:
82+
"""Example of using the builder with Anthropic.
83+
84+
This example demonstrates how to use Promptix with Anthropic's API.
85+
Requires ANTHROPIC_API_KEY to be set in environment variables.
86+
"""
87+
try:
88+
client = anthropic.Anthropic() # Will automatically use ANTHROPIC_API_KEY from environment
89+
90+
memory= [
91+
{"role": "user", "content": "I'm having trouble with my account settings"},
92+
]
93+
94+
model_config = (
95+
Promptix.builder("CustomerSupport")
96+
.with_version("v5")
97+
.with_user_name("John Doe")
98+
.with_issue_type("account_settings")
99+
.with_issue_description("User cannot access account settings page after recent update")
100+
.with_technical_level("intermediate")
101+
.with_priority("medium")
102+
.with_memory(memory)
103+
.for_client("anthropic")
104+
.build()
105+
)
106+
107+
print("Anthropic Model Config:", model_config)
108+
print("\n\n")
109+
110+
message = client.messages.create(**model_config)
111+
print("Anthropic Response:", message.content)
112+
except Exception as e:
113+
print(f"Error in Anthropic builder example: {str(e)}")
114+
115+
116+
if __name__ == "__main__":
117+
print("\n=== Basic Builder Example (CustomerSupport) ===")
118+
basic_builder_example()
119+
120+
print("\n=== Advanced Builder Example (CodeReview) ===")
121+
advanced_builder_example()
122+
123+
print("\n=== Anthropic Builder Example ===")
124+
anthropic_builder_example()

0 commit comments

Comments
 (0)