File size: 6,676 Bytes
e54fd17
 
6f40440
e54fd17
 
 
6f40440
e54fd17
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f40440
e54fd17
 
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f40440
e54fd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Advanced Features

LLMPromptKit provides several advanced features for sophisticated prompt engineering.

## Advanced Templating

LLMPromptKit's templating system goes beyond simple variable substitution, offering conditionals and loops.

### Basic Variable Substitution

```python
from llmpromptkit import PromptTemplate

# Simple variable substitution
template = PromptTemplate("Hello, {name}!")
rendered = template.render(name="John")
# Result: "Hello, John!"
```

### Conditional Logic

```python
# Conditionals
template = PromptTemplate("""
{if is_formal}
Dear {name},

I hope this message finds you well.
{else}
Hey {name}!
{endif}

{message}
""")

formal = template.render(is_formal=True, name="Dr. Smith", message="Please review the attached document.")
casual = template.render(is_formal=False, name="Bob", message="Want to grab lunch?")
```

### Loops

```python
# Loops
template = PromptTemplate("""
Here are your tasks:

{for task in tasks}
- {task.priority}: {task.description}
{endfor}
""")

rendered = template.render(tasks=[
    {"priority": "High", "description": "Complete the report"},
    {"priority": "Medium", "description": "Schedule meeting"},
    {"priority": "Low", "description": "Organize files"}
])
```

### Nested Structures

```python
# Combining loops and conditionals
template = PromptTemplate("""
{system_message}

{for example in examples}
User: {example.input}
{if example.has_reasoning}
Reasoning: {example.reasoning}
{endif}
Assistant: {example.output}
{endfor}

User: {query}
Assistant:
""")

rendered = template.render(
    system_message="You are a helpful assistant.",
    examples=[
        {
            "input": "What's 2+2?",
            "has_reasoning": True,
            "reasoning": "Adding 2 and 2 gives 4",
            "output": "4"
        },
        {
            "input": "Hello",
            "has_reasoning": False,
            "output": "Hi there! How can I help you today?"
        }
    ],
    query="What's the capital of France?"
)
```

## Custom Evaluation Metrics

You can create custom metrics to evaluate prompt outputs based on your specific requirements.

### Creating a Custom Metric

```python
from llmpromptkit import EvaluationMetric

class RelevanceMetric(EvaluationMetric):
    """Evaluates relevance of output to a given topic."""
    
    def __init__(self, topics):
        super().__init__("relevance", "Evaluates relevance to specified topics")
        self.topics = topics
    
    def compute(self, generated_output, expected_output=None, **kwargs):
        """
        Compute relevance score based on topic presence.
        Returns a float between 0 and 1.
        """
        score = 0
        output_lower = generated_output.lower()
        
        for topic in self.topics:
            if topic.lower() in output_lower:
                score += 1
                
        # Normalize to 0-1 range
        return min(1.0, score / len(self.topics)) if self.topics else 0.0
```

### Using Custom Metrics

```python
from llmpromptkit import Evaluator, PromptManager

# Initialize components
prompt_manager = PromptManager()
evaluator = Evaluator(prompt_manager)

# Register custom metric
climate_relevance = RelevanceMetric(["climate", "temperature", "warming", "environment"])
evaluator.register_metric(climate_relevance)

# Use in evaluation
async def my_llm(prompt, vars):
    # Call your LLM API here
    return "Climate change is causing global temperature increases..."

results = await evaluator.evaluate_prompt(
    prompt_id="abc123",
    inputs=[{"topic": "climate change"}],
    llm_callback=my_llm,
    metric_names=["relevance"]  # Use our custom metric
)

print(f"Relevance score: {results['aggregated_metrics']['relevance']}")
```

## Customizing Storage

LLMPromptKit allows you to customize where and how prompts and related data are stored.

### Custom Storage Locations

```python
# Specify a custom storage location
prompt_manager = PromptManager("/path/to/my/prompts")

# Export/import prompts
import json

# Export a prompt to a file
prompt = prompt_manager.get("abc123")
with open("exported_prompt.json", "w") as f:
    json.dump(prompt.to_dict(), f, indent=2)

# Import a prompt from a file
with open("exported_prompt.json", "r") as f:
    data = json.load(f)
    imported_prompt = prompt_manager.import_prompt(data)
```

## LLM Integration

LLMPromptKit is designed to work with any LLM through callback functions. Here are examples of integrating with popular LLM APIs.

### OpenAI Integration

```python
import openai
from llmpromptkit import PromptManager, PromptTesting

prompt_manager = PromptManager()
testing = PromptTesting(prompt_manager)

# Configure OpenAI
openai.api_key = "your-api-key"

# OpenAI callback function
async def openai_callback(prompt, vars):
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=150
    )
    return response.choices[0].message.content

# Run tests with OpenAI
test_results = await testing.run_all_tests("abc123", openai_callback)
```

### Anthropic Integration

```python
import anthropic
from llmpromptkit import PromptManager, Evaluator

prompt_manager = PromptManager()
evaluator = Evaluator(prompt_manager)

# Configure Anthropic
client = anthropic.Anthropic(api_key="your-api-key")

# Anthropic callback function
async def anthropic_callback(prompt, vars):
    response = client.messages.create(
        model="claude-2",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=150
    )
    return response.content[0].text

# Evaluate with Anthropic
eval_results = await evaluator.evaluate_prompt(
    prompt_id="abc123",
    inputs=[{"query": "What is machine learning?"}],
    llm_callback=anthropic_callback
)
```

### Hugging Face Integration

```python
from transformers import pipeline
import asyncio
from llmpromptkit import PromptManager, VersionControl

prompt_manager = PromptManager()
version_control = VersionControl(prompt_manager)

# Set up Hugging Face pipeline
generator = pipeline('text-generation', model='gpt2')

# Hugging Face callback function
async def hf_callback(prompt, vars):
    # Run synchronously but in a way that doesn't block the asyncio event loop
    loop = asyncio.get_event_loop()
    result = await loop.run_in_executor(None, lambda: generator(prompt, max_length=100)[0]['generated_text'])
    return result

# Use with version control
prompt = prompt_manager.create(
    content="Complete this: {text}",
    name="Text Completion"
)
version_control.commit(prompt.id, "Initial version")

# Test with different models by swapping the callback
```