[Deep Dive] Text-to-Config: Teaching AI to Speak "JSON"
In Part 2, we designed a beautiful JSON Schema for our parking lot system. It’s like building a perfect skeleton. But a skeleton cannot move on its own.
Now, in Part 3, we need to build the "Brain." We need to take a user's messy, vague request (e.g., "Make it cheap for the weekend") and convert it into that strict JSON format.
This is where Prompt Engineering comes in. We are not just "chatting" with the AI; we are programming it using English.
1. The "Strict Manager" Persona
If you ask ChatGPT normally, "Please make a config," it acts like a helpful assistant. It might give you explanations, Python code, or unnecessary polite text.
We don't want that. We want a machine.
We must define a System Prompt that forces the AI into a specific role. Think of this as the "Employee Handbook" you give to a new intern.
2. The Complete Python Implementation
Here is the actual Python code you can use. I have added detailed comments to explain how to handle the OpenAI API safely.
[Image of Python code structure diagram]import json
from openai import OpenAI
from datetime import datetime
client = OpenAI(api_key="YOUR_API_KEY")
# 1. Provide the Schema Context (The "Rulebook")
# We inject this into the prompt so the AI knows the structure.
SCHEMA_CONTEXT = """
SCHEMA DEFINITION:
{
"rules": [
{
"conditions": { "start_time": "ISO8601", "end_time": "ISO8601" },
"actions": [ { "type": "SET_PRICE", "amount": float } ]
}
]
}
"""
def generate_parking_config(user_input):
# Get current time for relative date calculations (e.g., "This Friday")
now_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 2. Construct the Messages
messages = [
{
"role": "system",
"content": f"You are a Strict Config Generator. Current Time: {now_str}. \nSchema: {SCHEMA_CONTEXT}"
},
{
"role": "user",
"content": user_input
}
]
try:
response = client.chat.completions.create(
model="gpt-4-turbo", # Use a smart model for logic
messages=messages,
temperature=0, # Important! 0 means 'no creativity'. We want logic.
response_format={ "type": "json_object" } # Force valid JSON
)
json_str = response.choices[0].message.content
return json.loads(json_str)
except Exception as e:
return { "error": "AI_PROCESSING_FAILED", "details": str(e) }
Key Technical Details:
temperature=0: This is critical. It tells the AI to be "deterministic" and boring. We don't want creative parking fees.response_format={"type": "json_object"}: This ensures the API doesn't return half-finished text. It guarantees syntax correctness.datetime.now(): By injecting the current time, the AI can correctly understand relative terms like "tonight" or "next weekend."
3. Real-World Examples: Handling Ambiguity
The biggest challenge is when users are vague. Let's see how our setup handles different inputs.
The input is specific. The AI has the amount ($10) and the time (tonight).
Output:{
"actions": [ { "type": "SET_PRICE", "amount": 10.0 } ],
"conditions": { "start": "2024-05-20T18:00:00", "end": "..." }
}
This is dangerous. What is "cheap"? $1? $0? 50% off? The AI should not guess.
Because we set the system prompt to be strict, the AI returns an error object asking for clarification.
Output:{
"error": "MISSING_PARAMETER",
"message": "You requested a 'cheap' rate, but did not specify the exact price amount."
}
4. Pro Tip: Chain-of-Thought (CoT)
Sometimes, complex logic fails. For example: "Charge $10 normally, but if it's a weekend AND a concert, charge double."
To solve this, ask the AI to "think" before it acts. Add a "reasoning" field to your JSON schema.
This forces the model to verify its own logic (e.g., "User said double $10, so the price is $20") before creating the final JSON. It dramatically reduces logical errors.
Conclusion
We now have a system that speaks JSON fluently and handles vague inputs safely. We have bridged the gap between human language and machine configuration.
However, there is one final danger. Even if the JSON is valid, the logic might be disastrous (e.g., setting the price to -$500). In the next post, we will build the final defense layer: Logic Guardrails.
No comments:
Post a Comment