```python
import os
import json
from groq import Groq

# Set your API key
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# Example of a poorly designed prompt
poor_prompt = """
Give me information about a movie in JSON format.
"""

# Example of a well-designed prompt
effective_prompt = """
You are a movie database API. Return information about a movie with the following 
JSON structure:

{
  "title": "string",
  "year": number,
  "director": "string",
  "genre": ["string"],
  "runtime_minutes": number,
  "rating": number (1-10 scale),
  "box_office_millions": number,
  "cast": [
    {
      "actor": "string",
      "character": "string"
    }
  ]
}

The response must:
1. Include ALL fields shown above
2. Use only the exact field names shown
3. Follow the exact data types specified
4. Contain ONLY the JSON object and nothing else

IMPORTANT: Do not include any explanatory text, markdown formatting, or code blocks.
"""

# Function to run the completion and display results
def get_movie_data(prompt, title="Example"):
    print(f"\n--- {title} ---")
    
    completion = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        response_format={"type": "json_object"},
        messages=[
            {"role": "system", "content": prompt},
            {"role": "user", "content": "Tell me about The Matrix"}
        ]
    )
    
    response_content = completion.choices[0].message.content
    print("Raw response:")
    print(response_content)
    
    # Try to parse as JSON
    try:
        movie_data = json.loads(response_content)
        print("\nSuccessfully parsed as JSON!")
        
        # Check for expected fields
        expected_fields = ["title", "year", "director", "genre", 
                          "runtime_minutes", "rating", "box_office_millions", "cast"]
        missing_fields = [field for field in expected_fields if field not in movie_data]
        
        if missing_fields:
            print(f"Missing fields: {', '.join(missing_fields)}")
        else:
            print("All expected fields present!")
            
    except json.JSONDecodeError:
        print("\nFailed to parse as JSON. Response is not valid JSON.")

# Compare the results of both prompts
get_movie_data(poor_prompt, "Poor Prompt Example")
get_movie_data(effective_prompt, "Effective Prompt Example")
```

---

## Text Chat: Prompt Engineering.doc (ts)