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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# Integration Examples

This document provides concrete examples of integrating LLMPromptKit into various applications and workflows.

## Customer Support Chatbot

### Setup

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

# Initialize components
prompt_manager = PromptManager()
version_control = VersionControl(prompt_manager)

# Create prompt templates for different scenarios
greeting_prompt = prompt_manager.create(
    content="You are a helpful customer service agent for {company_name}. Greet the customer politely.",
    name="Customer Greeting",
    tags=["customer-service", "greeting"]
)

inquiry_prompt = prompt_manager.create(
    content="""
You are a helpful customer service agent for {company_name}.
Customer inquiry: {customer_message}

Based on this inquiry:
1. Identify the main issue
2. Provide a helpful response
3. Offer additional assistance

Keep your tone professional but friendly.
""",
    name="Customer Inquiry Response",
    tags=["customer-service", "inquiry"]
)

# Version them
version_control.commit(greeting_prompt.id, "Initial version")
version_control.commit(inquiry_prompt.id, "Initial version")

# OpenAI callback
def generate_response(prompt_text):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt_text}]
    )
    return response.choices[0].message.content

# Main handler function
def handle_customer_message(customer_name, message, is_new_conversation):
    if is_new_conversation:
        # Use greeting prompt for new conversations
        prompt = prompt_manager.get(greeting_prompt.id)
        prompt_text = prompt.render(company_name="Acme Inc.")
        return generate_response(prompt_text)
    else:
        # Use inquiry prompt for ongoing conversations
        prompt = prompt_manager.get(inquiry_prompt.id)
        prompt_text = prompt.render(
            company_name="Acme Inc.",
            customer_message=message
        )
        return generate_response(prompt_text)
```

## Content Generation System

### Setup

```python
from llmpromptkit import PromptManager, PromptTesting, Evaluator
import asyncio

# Initialize components
prompt_manager = PromptManager("content_system_prompts")
testing = PromptTesting(prompt_manager)
evaluator = Evaluator(prompt_manager)

# Create content generation prompt
blog_prompt = prompt_manager.create(
    content="""
Write a blog post about {topic}.

Title: {title}

The post should:
- Be approximately {word_count} words
- Be written in a {tone} tone
- Include {num_sections} main sections
- Target audience: {audience}
- Include a compelling call-to-action at the end

Keywords to include: {keywords}
""",
    name="Blog Post Generator",
    tags=["content", "blog"]
)

# Test cases
test_case = testing.create_test_case(
    prompt_id=blog_prompt.id,
    input_vars={
        "topic": "Sustainable Living",
        "title": "10 Simple Ways to Reduce Your Carbon Footprint",
        "word_count": "800",
        "tone": "informative yet casual",
        "num_sections": "5",
        "audience": "environmentally-conscious millennials",
        "keywords": "sustainability, eco-friendly, carbon footprint, climate change, lifestyle changes"
    }
)

# LLM callback
async def content_llm_callback(prompt, vars):
    # Call your preferred LLM API here
    # This is a placeholder
    return f"Generated content about {vars.get('topic', 'unknown topic')}"

# Content generation function
async def generate_content(content_type, parameters):
    if content_type == "blog":
        prompt = prompt_manager.get(blog_prompt.id)
        rendered_prompt = prompt.render(**parameters)
        
        # Generate content
        content = await content_llm_callback(rendered_prompt, parameters)
        
        # Evaluate quality
        evaluation = await evaluator.evaluate_prompt(
            prompt_id=blog_prompt.id,
            inputs=[parameters],
            llm_callback=content_llm_callback
        )
        
        quality_score = evaluation["aggregated_metrics"].get("length", 0)
        
        return {
            "content": content,
            "quality_score": quality_score,
            "metadata": {
                "prompt_id": blog_prompt.id,
                "prompt_version": prompt.version,
                "parameters": parameters
            }
        }
    else:
        raise ValueError(f"Unsupported content type: {content_type}")
```

## AI-Assisted Research Tool

### Setup

```python
from llmpromptkit import PromptManager, VersionControl
import json
import openai

# Initialize components
prompt_manager = PromptManager("research_prompts")
version_control = VersionControl(prompt_manager)

# Create research prompts
article_summary_prompt = prompt_manager.create(
    content="""
Summarize the following research article:

Title: {article_title}
Abstract: {article_abstract}

Provide a summary that:
1. Identifies the main research question
2. Outlines the methodology
3. Summarizes key findings
4. Highlights limitations
5. Explains the significance of the results

Keep the summary concise, approximately 250 words.
""",
    name="Article Summarizer",
    tags=["research", "summary"]
)

research_question_prompt = prompt_manager.create(
    content="""
Based on the following information:

Research Area: {research_area}
Existing Knowledge: {existing_knowledge}
Observed Gap: {knowledge_gap}

Generate 5 potential research questions that:
1. Address the identified knowledge gap
2. Are specific and answerable
3. Have theoretical or practical significance
4. Can be investigated with available research methods
""",
    name="Research Question Generator",
    tags=["research", "question-generation"]
)

# Version control
version_control.commit(article_summary_prompt.id, "Initial version")
version_control.commit(research_question_prompt.id, "Initial version")

# OpenAI callback
def research_assistant(prompt_text):
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt_text}]
    )
    return response.choices[0].message.content

# Research functions
def summarize_article(article_title, article_abstract):
    prompt = prompt_manager.get(article_summary_prompt.id)
    prompt_text = prompt.render(
        article_title=article_title,
        article_abstract=article_abstract
    )
    return research_assistant(prompt_text)

def generate_research_questions(research_area, existing_knowledge, knowledge_gap):
    prompt = prompt_manager.get(research_question_prompt.id)
    prompt_text = prompt.render(
        research_area=research_area,
        existing_knowledge=existing_knowledge,
        knowledge_gap=knowledge_gap
    )
    return research_assistant(prompt_text)

# Save results
def save_research_data(research_project, data_type, content):
    # Save the data along with prompt metadata for reproducibility
    if data_type == "summary":
        prompt_id = article_summary_prompt.id
        prompt = prompt_manager.get(prompt_id)
    elif data_type == "questions":
        prompt_id = research_question_prompt.id
        prompt = prompt_manager.get(prompt_id)
    
    research_data = {
        "content": content,
        "metadata": {
            "prompt_id": prompt_id,
            "prompt_version": prompt.version,
            "timestamp": datetime.datetime.now().isoformat()
        }
    }
    
    # Save to file (in real application, might save to database)
    with open(f"{research_project}_{data_type}.json", "w") as f:
        json.dump(research_data, f, indent=2)
```

## Educational Quiz Generator

### Setup

```python
from llmpromptkit import PromptManager, PromptTemplate
import asyncio
import aiohttp

# Initialize components
prompt_manager = PromptManager("education_prompts")

# Quiz generation prompt
quiz_prompt = prompt_manager.create(
    content="""
Generate a quiz on the topic of {topic} at a {difficulty_level} difficulty level.

The quiz should:
- Have {num_questions} multiple-choice questions
- Cover the following subtopics: {subtopics}
- Include {include_explanation} explanations for the correct answers
- Be appropriate for {grade_level} students

For each question, provide:
1. The question text
2. Four possible answers (A, B, C, D)
3. The correct answer
{if include_explanation == "yes"}
4. An explanation of why the answer is correct
{endif}

Format the output as valid JSON.
""",
    name="Quiz Generator",
    tags=["education", "quiz"]
)

# Quiz rendering template using advanced templating
render_template = PromptTemplate("""
<h1>{quiz_title}</h1>

<form id="quiz-form">
{for question in questions}
  <div class="question">
    <p><strong>Question {question.number}:</strong> {question.text}</p>
    <ul style="list-style-type: none;">
      {for option in question.options}
      <li>
        <input type="radio" name="q{question.number}" id="q{question.number}_{option.letter}" value="{option.letter}">
        <label for="q{question.number}_{option.letter}">{option.letter}. {option.text}</label>
      </li>
      {endfor}
    </ul>
    
    {if show_answers}
    <div class="answer">
      <p><strong>Correct Answer:</strong> {question.correct_answer}</p>
      {if question.has_explanation}
      <p><strong>Explanation:</strong> {question.explanation}</p>
      {endif}
    </div>
    {endif}
  </div>
{endfor}

{if !show_answers}
<button type="submit">Submit Quiz</button>
{endif}
</form>
""")

# LLM callback
async def education_llm_callback(prompt, vars):
    # This would call your LLM API
    # Simulated response for this example
    await asyncio.sleep(1)  # Simulate API call
    if "quiz" in prompt:
        return """
        {
          "questions": [
            {
              "text": "What is the capital of France?",
              "options": [
                {"letter": "A", "text": "London"},
                {"letter": "B", "text": "Berlin"},
                {"letter": "C", "text": "Paris"},
                {"letter": "D", "text": "Madrid"}
              ],
              "correct_answer": "C",
              "explanation": "Paris is the capital and most populous city of France."
            },
            {
              "text": "Who wrote 'Romeo and Juliet'?",
              "options": [
                {"letter": "A", "text": "Charles Dickens"},
                {"letter": "B", "text": "William Shakespeare"},
                {"letter": "C", "text": "Jane Austen"},
                {"letter": "D", "text": "Mark Twain"}
              ],
              "correct_answer": "B",
              "explanation": "William Shakespeare wrote 'Romeo and Juliet' around 1594-1596."
            }
          ]
        }
        """
    return "Default response"

# Quiz generation function
async def generate_quiz(topic, difficulty, num_questions, grade_level, include_explanations=True):
    prompt = prompt_manager.get(quiz_prompt.id)
    rendered_prompt = prompt.render(
        topic=topic,
        difficulty_level=difficulty,
        num_questions=num_questions,
        subtopics=", ".join(["key concepts", "historical context", "practical applications"]),
        include_explanation="yes" if include_explanations else "no",
        grade_level=grade_level
    )
    
    # Get quiz content from LLM
    quiz_json = await education_llm_callback(rendered_prompt, {})
    
    # Parse JSON
    quiz_data = json.loads(quiz_json)
    
    # Prepare data for HTML template
    template_data = {
        "quiz_title": f"{topic} Quiz ({difficulty} Level)",
        "questions": [],
        "show_answers": False
    }
    
    # Format questions
    for i, q in enumerate(quiz_data["questions"]):
        question = {
            "number": i + 1,
            "text": q["text"],
            "options": q["options"],
            "correct_answer": q["correct_answer"],
            "has_explanation": "explanation" in q,
            "explanation": q.get("explanation", "")
        }
        template_data["questions"].append(question)
    
    # Render HTML
    return render_template.render(**template_data)
```

## Automated Coding Assistant

### Setup

```python
from llmpromptkit import PromptManager, PromptTesting
import asyncio
import subprocess
import tempfile

# Initialize components
prompt_manager = PromptManager("coding_prompts")
testing = PromptTesting(prompt_manager)

# Create code generation prompts
function_prompt = prompt_manager.create(
    content="""
Write a {language} function that solves the following problem:

{problem_description}

Function signature: {function_signature}

Requirements:
- The function should handle edge cases
- Include appropriate comments
- Follow {language} best practices
- Be optimized for {optimization_goal}

{if include_tests == "yes"}
Also include unit tests for the function.
{endif}
""",
    name="Function Generator",
    tags=["coding", "function"]
)

bug_fix_prompt = prompt_manager.create(
    content="""
Debug the following {language} code which has an issue:

```{language}
{buggy_code}
```

Error message or problem description:
{error_description}

Please:
1. Identify the issue
2. Explain the root cause
3. Provide a fixed version of the code
4. Suggest how to prevent similar issues
""",
    name="Bug Fix Assistant",
    tags=["coding", "debugging"]
)

# LLM callback
async def coding_llm_callback(prompt, vars):
    # This would call your LLM API
    # Simplified example response
    await asyncio.sleep(1)
    
    if "function" in prompt:
        return """
        ```python
        def find_max_subarray_sum(arr):
            """
            Finds the maximum sum of any contiguous subarray.
            Uses Kadane's algorithm with O(n) time complexity.
            
            Args:
                arr: List of integers
            Returns:
                Maximum subarray sum
            """
            if not arr:
                return 0
                
            current_max = global_max = arr[0]
            
            for num in arr[1:]:
                current_max = max(num, current_max + num)
                global_max = max(global_max, current_max)
                
            return global_max
            
        # Unit tests
        def test_find_max_subarray_sum():
            assert find_max_subarray_sum([]) == 0
            assert find_max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) == 6
            assert find_max_subarray_sum([-1, -2, -3]) == -1
            print("All tests passed!")
        ```
        """
    elif "debug" in prompt:
        return """
        The issue is a classic off-by-one error in the loop boundary.
        
        Root cause:
        The loop is using `i <= len(arr)` which accesses an index that's out of bounds.
        
        Fixed code:
        ```python
        def process_array(arr):
            result = []
            for i in range(len(arr)):  # Changed from i <= len(arr)
                result.append(arr[i] * 2)
            return result
        ```
        
        Prevention:
        - Remember that array indices are 0-based and go up to len(arr)-1
        - Use range() or enumerate() when iterating through arrays by index
        - Add bounds checking for critical operations
        """
    
    return "Default response"

# Function to test generated code
def test_generated_code(code, language):
    """Test the generated code by running it in a safe environment."""
    if language.lower() == "python":
        with tempfile.NamedTemporaryFile(suffix=".py") as temp:
            temp.write(code.encode())
            temp.flush()
            
            try:
                result = subprocess.run(["python", temp.name], 
                                       capture_output=True, 
                                       text=True, 
                                       timeout=5)
                if result.returncode == 0:
                    return {"success": True, "output": result.stdout}
                else:
                    return {"success": False, "error": result.stderr}
            except subprocess.TimeoutExpired:
                return {"success": False, "error": "Code execution timed out"}
    
    return {"success": False, "error": f"Testing not implemented for {language}"}

# Main coding assistant function
async def generate_function(problem, language="python", optimization_goal="readability", include_tests=True):
    function_name = problem.lower().replace(" ", "_").replace("-", "_")
    signature = f"def {function_name}(parameters):"
    
    prompt = prompt_manager.get(function_prompt.id)
    rendered_prompt = prompt.render(
        language=language,
        problem_description=problem,
        function_signature=signature,
        optimization_goal=optimization_goal,
        include_tests="yes" if include_tests else "no"
    )
    
    # Get code from LLM
    generated_code = await coding_llm_callback(rendered_prompt, {})
    
    # Extract code from markdown if present
    if "```" in generated_code:
        code_blocks = re.findall(r"```(?:\w+)?\n(.+?)```", generated_code, re.DOTALL)
        if code_blocks:
            clean_code = code_blocks[0]
        else:
            clean_code = generated_code
    else:
        clean_code = generated_code
    
    # Test the code
    test_result = test_generated_code(clean_code, language)
    
    return {
        "code": clean_code,
        "test_result": test_result,
        "prompt_id": function_prompt.id
    }
```