# Catatan Seekor: Prompt AI

## 💡 Catatan Seekor: Prompt AI

### 📚 Overview

Prompt Engineering adalah seni dan ilmu merancang input (prompts) yang efektif untuk AI models, terutama Large Language Models (LLMs) seperti GPT, Claude, dan lainnya. Tujuan utamanya adalah mendapatkan output yang akurat, relevan, dan sesuai dengan kebutuhan pengguna.

### 🎯 Why Prompt Engineering Matters

#### 1. **Model Performance**

* **Consistency**: Prompts yang baik menghasilkan output yang konsisten
* **Accuracy**: Meningkatkan akurasi dan relevansi respons
* **Efficiency**: Mengurangi kebutuhan untuk multiple attempts

#### 2. **Cost Optimization**

* **Token Efficiency**: Menggunakan tokens secara optimal
* **Fewer API Calls**: Mengurangi jumlah API calls yang diperlukan
* **Better ROI**: Maksimalisasi nilai dari setiap API call

#### 3. **User Experience**

* **Predictable Outputs**: Output yang dapat diprediksi dan konsisten
* **Reduced Iterations**: Mengurangi kebutuhan untuk multiple revisions
* **Professional Quality**: Output yang berkualitas profesional

### 🏗️ Prompt Engineering Techniques

#### 1. **Zero-Shot Prompting**

Memberikan instruksi langsung tanpa contoh.

```python
# Basic zero-shot prompt
prompt = """
Analyze the sentiment of the following text and classify it as positive, negative, or neutral.

Text: "I absolutely love this new restaurant! The food is amazing and the service is excellent."

Sentiment:
"""

# Using OpenAI API
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a sentiment analysis expert."},
        {"role": "user", "content": prompt}
    ]
)
```

**Use Cases:**

* Simple classification tasks
* Basic analysis requests
* When you have limited examples

**Best Practices:**

* Be specific and clear
* Use action-oriented language
* Specify output format

#### 2. **Few-Shot Prompting**

Memberikan beberapa contoh untuk membantu model memahami task.

```python
# Few-shot prompt with examples
prompt = """
Classify the sentiment of the following texts as positive, negative, or neutral.

Examples:
Text: "This movie was fantastic! I loved every minute of it."
Sentiment: positive

Text: "The food was terrible and the service was slow."
Sentiment: negative

Text: "The weather is okay today, nothing special."
Sentiment: neutral

Now classify this text:
Text: "The new smartphone has good features but the battery life is disappointing."
Sentiment:
"""

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a sentiment analysis expert. Follow the examples provided."},
        {"role": "user", "content": prompt}
    ]
)
```

**Use Cases:**

* Complex classification tasks
* When you need specific output formats
* Teaching new concepts to the model

**Best Practices:**

* Use diverse examples
* Maintain consistency in format
* Include edge cases

#### 3. **Chain-of-Thought Prompting**

Mendorong model untuk berpikir step-by-step.

```python
# Chain-of-thought prompt
prompt = """
Let's solve this math problem step by step:

Problem: If a store sells shirts for $25 each and pants for $40 each, and they sold 12 shirts and 8 pants today, how much revenue did they generate?

Let me think through this step by step:

1) First, let's calculate revenue from shirts:
   - Number of shirts sold: 12
   - Price per shirt: $25
   - Revenue from shirts: 12 × $25 = $300

2) Next, let's calculate revenue from pants:
   - Number of pants sold: 8
   - Price per pants: $40
   - Revenue from pants: 8 × $40 = $320

3) Finally, let's add them together:
   - Total revenue = $300 + $320 = $620

Therefore, the store generated $620 in revenue today.

Now solve this problem using the same step-by-step approach:

Problem: A bakery sells cakes for $30 each and cookies for $2 each. If they sold 5 cakes and 20 cookies, what was their total revenue?

Let me think through this step by step:
"""

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a math tutor. Always show your work step by step."},
        {"role": "user", "content": prompt}
    ]
)
```

**Use Cases:**

* Complex problem solving
* Mathematical reasoning
* Logical analysis
* Debugging and troubleshooting

**Best Practices:**

* Encourage detailed reasoning
* Ask for step-by-step breakdown
* Verify intermediate steps

#### 4. **Role-Based Prompting**

Memberikan model peran atau persona spesifik.

```python
# Role-based prompt
prompt = """
You are an expert software architect with 15 years of experience designing scalable systems. 
You specialize in microservices architecture and cloud-native applications.

A client wants to build an e-commerce platform that can handle 1 million users simultaneously.
They need recommendations for:
1. Technology stack
2. Architecture patterns
3. Database design
4. Scalability considerations
5. Security measures

Please provide detailed, professional recommendations suitable for a technical audience.
"""

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a senior software architect with extensive experience in enterprise systems."},
        {"role": "user", "content": prompt}
    ]
)
```

**Use Cases:**

* Professional consultations
* Domain-specific expertise
* Consistent tone and style
* Industry-specific knowledge

**Best Practices:**

* Define expertise level clearly
* Specify target audience
* Include relevant experience

### 🔧 Advanced Prompting Techniques

#### 1. **Template-Based Prompting**

Menggunakan template yang dapat digunakan kembali.

````python
# Template for code review
def create_code_review_prompt(code, language, focus_areas):
    template = f"""
You are a senior {language} developer conducting a code review.

Code to review:
```{language}
{code}
````

Focus areas for this review: {chr(10).join(\[f"- {area}" for area in focus\_areas])}

Please provide a comprehensive code review covering:

1. Code quality and best practices
2. Performance considerations
3. Security concerns
4. Maintainability
5. Specific feedback on focus areas

Format your response with clear sections and actionable recommendations. """ return template

## Usage

code = """ def calculate\_fibonacci(n): if n <= 1: return n return calculate\_fibonacci(n-1) + calculate\_fibonacci(n-2) """

focus\_areas = \["Performance", "Error handling", "Documentation"] prompt = create\_code\_review\_prompt(code, "python", focus\_areas)

````

### 2. **Conditional Prompting**
Menggunakan conditional logic dalam prompts.

```python
# Conditional prompt based on user type
def create_user_support_prompt(user_type, issue, priority):
    if user_type == "premium":
        base_prompt = "As a premium customer, you receive priority support."
        response_time = "within 2 hours"
    elif user_type == "enterprise":
        base_prompt = "As an enterprise customer, you have dedicated support."
        response_time = "within 1 hour"
    else:
        base_prompt = "Thank you for contacting our support team."
        response_time = "within 24 hours"
    
    if priority == "high":
        urgency = "URGENT - This issue requires immediate attention."
    else:
        urgency = "Standard priority issue."
    
    prompt = f"""
{base_prompt}

{urgency}

Issue Description: {issue}

Expected Response Time: {response_time}

Please provide a detailed solution and next steps.
"""
    return prompt
````

#### 3. **Multi-Turn Prompting**

Menggunakan conversation context untuk prompts yang lebih baik.

```python
# Multi-turn conversation
conversation_history = [
    {"role": "system", "content": "You are a helpful coding assistant."},
    {"role": "user", "content": "I'm building a web application with Python Flask."},
    {"role": "assistant", "content": "Great! Flask is an excellent choice for web development. What specific help do you need?"},
    {"role": "user", "content": "I need help with user authentication."}
]

# Continue the conversation
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=conversation_history + [
        {"role": "user", "content": "Can you show me how to implement JWT authentication?"}
    ]
)

# Add the response to history
conversation_history.append({"role": "user", "content": "Can you show me how to implement JWT authentication?"})
conversation_history.append({"role": "assistant", "content": response.choices[0].message.content})
```

### 📊 Prompt Testing & Evaluation

#### 1. **A/B Testing Prompts**

```python
def test_prompt_variations(base_prompt, variations, test_cases):
    results = {}
    
    for i, variation in enumerate(variations):
        variation_results = []
        
        for test_case in test_cases:
            # Test the variation
            response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": variation.format(**test_case)}
                ]
            )
            
            # Evaluate response (you can implement custom evaluation logic)
            evaluation = evaluate_response(response.choices[0].message.content, test_case)
            variation_results.append(evaluation)
        
        results[f"variation_{i+1}"] = {
            "prompt": variation,
            "results": variation_results,
            "average_score": sum(variation_results) / len(variation_results)
        }
    
    return results

# Example usage
base_prompt = "Analyze the sentiment of: {text}"
variations = [
    "Analyze the sentiment of: {text}",
    "What is the emotional tone of this text: {text}",
    "Classify the sentiment as positive, negative, or neutral: {text}"
]

test_cases = [
    {"text": "I love this product!"},
    {"text": "This is terrible."},
    {"text": "It's okay, nothing special."}
]

results = test_prompt_variations(base_prompt, variations, test_cases)
```

#### 2. **Response Quality Metrics**

```python
def evaluate_response(response, expected_criteria):
    score = 0
    
    # Check for required elements
    if "positive" in response.lower() or "negative" in response.lower() or "neutral" in response.lower():
        score += 1
    
    # Check for explanation
    if len(response.split()) > 10:
        score += 1
    
    # Check for confidence
    if any(word in response.lower() for word in ["confident", "certain", "sure"]):
        score += 1
    
    # Check for actionable insights
    if any(word in response.lower() for word in ["recommend", "suggest", "consider"]):
        score += 1
    
    return score / 4  # Normalize to 0-1

# Usage
evaluation = evaluate_response("This text has a positive sentiment because it uses words like 'love' and 'amazing'.", {})
print(f"Response Quality Score: {evaluation:.2f}")
```

### 🚀 Best Practices

#### 1. **Clarity & Specificity**

* **Be Explicit**: Jangan biarkan ada ambiguitas
* **Use Examples**: Berikan contoh yang jelas
* **Specify Format**: Tentukan format output yang diinginkan
* **Set Constraints**: Berikan batasan dan requirements

#### 2. **Context & Background**

* **Provide Context**: Berikan background information yang relevan
* **Set Expectations**: Jelaskan apa yang diharapkan
* **Define Scope**: Tentukan batasan scope
* **Include Constraints**: Sebutkan limitations dan constraints

#### 3. **Iterative Improvement**

* **Test & Refine**: Test prompts dan refine berdasarkan hasil
* **A/B Testing**: Bandingkan berbagai versi prompts
* **User Feedback**: Kumpulkan feedback dari users
* **Continuous Learning**: Update prompts berdasarkan learnings

#### 4. **Error Handling**

* **Fallback Prompts**: Siapkan prompts alternatif
* **Error Recovery**: Handle cases ketika output tidak sesuai
* **Validation**: Validate output sebelum digunakan
* **Retry Logic**: Implement retry logic untuk failed attempts

### 📚 References & Resources

#### 📖 Research Papers

* [**"Language Models are Few-Shot Learners"**](https://arxiv.org/abs/2005.14165) by Brown et al.
* [**"Chain-of-Thought Prompting Elicits Reasoning in Large Language Models"**](https://arxiv.org/abs/2201.11903) by Wei et al.
* [**"Self-Consistency Improves Chain of Thought Reasoning in Language Models"**](https://arxiv.org/abs/2203.11171) by Wang et al.

#### 🛠️ Tools & Libraries

* [**LangChain**](https://github.com/langchain-ai/langchain) - Framework untuk building LLM applications
* [**Prompt Engineering Guide**](https://www.promptingguide.ai/) - Comprehensive prompt engineering guide
* [**OpenAI Cookbook**](https://github.com/openai/openai-cookbook) - Examples and best practices
* [**PromptPerfect**](https://promptperfect.jina.ai/) - Prompt optimization tool

#### 🎓 Courses & Tutorials

* [**OpenAI Prompt Engineering Course**](https://platform.openai.com/docs/guides/prompt-engineering)
* [**DeepLearning.AI Prompt Engineering Course**](https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/)
* [**LangChain Prompt Engineering**](https://python.langchain.com/docs/modules/model_io/prompts/)

#### 📰 Articles & Blogs

* [**"Prompt Engineering Guide"**](https://www.promptingguide.ai/) by Anthropic
* [**"OpenAI Best Practices"**](https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api)
* [**"Prompt Engineering Patterns"**](https://microsoft.github.io/prompt-engineering/) by Microsoft

### 🔗 Related Topics

* [🧠 ML Fundamentals](https://mahbubzulkarnain.gitbook.io/catatan-seekor-the-series/machine-learning/fundamentals)
* [🤖 OpenAI Integration](https://mahbubzulkarnain.gitbook.io/catatan-seekor-the-series/machine-learning/catatan-seekor-open-ai)
* [🔍 RAG Systems](https://mahbubzulkarnain.gitbook.io/catatan-seekor-the-series/machine-learning/catatan-seekor-rag)
* [🎯 Fine-tuning](https://mahbubzulkarnain.gitbook.io/catatan-seekor-the-series/machine-learning/catatan-seekor-fine-tunning)

***

*Last updated: December 2024* *Contributors: \[Your Name]*
