Decision Trees & Scoring
Use decision points to route generation based on scoring criteria. This enables quality control, content refinement, and conditional logic flows.
Overview
Decision trees allow you to:
- Score generated content against multiple dimensions
- Branch conditionally based on score thresholds
- Refine outputs that don't meet quality criteria
- Route to different workflows based on content characteristics
Blog Post Quality Router Example
This example generates a technical blog post, scores it for technical accuracy and readability, and refines it if needed:
- cURL
- Python
- JavaScript/Node.js
curl -X POST http://localhost:2008/objectGen \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"prompt": "Generate a technical blog post about microservices architecture and best practices",
"definition": {
"type": "object",
"properties": {
"content": {
"type": "string",
"instruction": "Generate a technical blog post about microservices",
"scoringCriteria": {
"dimensions": {
"technical_accuracy": {
"description": "Rate the technical accuracy from 0-100",
"type": "number"
},
"readability": {
"description": "Rate the readability from 0-100",
"type": "number"
}
}
},
"decisionPoint": {
"name": "ContentQualityRouter",
"branches": [
{
"name": "HighAccuracyLowReadability",
"conditions": [
{
"field": "technical_accuracy",
"operator": "gt",
"number_value": 95
},
{
"field": "readability",
"operator": "lt",
"number_value": 95
}
],
"then": {
"type": "object",
"instruction": "Improve readability while maintaining technical accuracy",
"selectFields": ["content"],
"properties": {
"simplified_content": {
"type": "string",
"instruction": "Simplify the language"
}
}
}
}
]
}
}
}
}
}'
import requests
url = "http://localhost:2008/objectGen"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
data = {
"prompt": "Generate a technical blog post about microservices architecture and best practices",
"definition": {
"type": "object",
"properties": {
"content": {
"type": "string",
"instruction": "Generate a technical blog post about microservices",
"scoringCriteria": {
"dimensions": {
"technical_accuracy": {
"description": "Rate the technical accuracy from 0-100",
"type": "number"
},
"readability": {
"description": "Rate the readability from 0-100",
"type": "number"
}
}
},
"decisionPoint": {
"name": "ContentQualityRouter",
"branches": [
{
"name": "HighAccuracyLowReadability",
"conditions": [
{
"field": "technical_accuracy",
"operator": "gt",
"number_value": 95
},
{
"field": "readability",
"operator": "lt",
"number_value": 95
}
],
"then": {
"type": "object",
"instruction": "Improve readability while maintaining technical accuracy",
"selectFields": ["content"],
"properties": {
"simplified_content": {
"type": "string",
"instruction": "Simplify the language"
}
}
}
}
]
}
}
}
}
}
response = requests.post(url, headers=headers, json=data)
result = response.json()
# Access the generated fields
fields = result.get("data", {}).get("fields", {})
content = fields.get("content", {}).get("string_value", "")
simplified_content = fields.get("simplified_content", {}).get("string_value", "")
print("Original Content:", content[:200] + "...")
print("\nSimplified Content:", simplified_content[:200] + "...")
print("\nCost:", result.get("usdCost"))
const fetch = require('node-fetch');
const url = 'http://localhost:2008/objectGen';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
};
const data = {
prompt: 'Generate a technical blog post about microservices architecture and best practices',
definition: {
type: 'object',
properties: {
content: {
type: 'string',
instruction: 'Generate a technical blog post about microservices',
scoringCriteria: {
dimensions: {
technical_accuracy: {
description: 'Rate the technical accuracy from 0-100',
type: 'number'
},
readability: {
description: 'Rate the readability from 0-100',
type: 'number'
}
}
},
decisionPoint: {
name: 'ContentQualityRouter',
branches: [
{
name: 'HighAccuracyLowReadability',
conditions: [
{
field: 'technical_accuracy',
operator: 'gt',
number_value: 95
},
{
field: 'readability',
operator: 'lt',
number_value: 95
}
],
then: {
type: 'object',
instruction: 'Improve readability while maintaining technical accuracy',
selectFields: ['content'],
properties: {
simplified_content: {
type: 'string',
instruction: 'Simplify the language'
}
}
}
}
]
}
}
}
}
};
fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
})
.then(response => response.json())
.then(result => {
// Access the generated fields
const fields = result.data?.fields || {};
const content = fields.content?.string_value || '';
const simplifiedContent = fields.simplified_content?.string_value || '';
console.log('Original Content:', content.substring(0, 200) + '...');
console.log('\nSimplified Content:', simplifiedContent.substring(0, 200) + '...');
console.log('\nCost:', result.usdCost);
})
.catch(error => console.error('Error:', error));
Decision Point Components
Scoring Criteria
Define multiple dimensions to evaluate the generated content:
"scoringCriteria": {
"dimensions": {
"dimension_name": {
"description": "Clear description of what to evaluate (0-100)",
"type": "number"
}
}
}
Decision Point Structure
| Field | Type | Description |
|---|---|---|
name | string | Identifier for the decision point |
branches | array | List of conditional branches to evaluate |
Branch Configuration
| Field | Type | Description |
|---|---|---|
name | string | Branch identifier |
conditions | array | List of conditions (AND logic) |
then | object | Schema to execute if conditions match |
Condition Operators
| Operator | Description |
|---|---|
gt | Greater than |
lt | Less than |
gte | Greater than or equal |
lte | Less than or equal |
eq | Equal to |
ne | Not equal to |
Expected Response
The response follows the gRPC structure with nested fields:
{
"data": {
"fields": {
"content": {
"string_value": "Microservices: Building Blocks for Scalable and Resilient Applications\n\nMicroservices architecture has emerged as a popular approach to building complex applications. Instead of a monolithic application, microservices structure an application as a collection of small, autonomous services, modeled around a business domain..."
},
"simplified_content": {
"string_value": "Microservices: Small, independent services that work together to form an application. They each own their data, communicate over a network, and can be deployed independently.\n\nBenefits include better scaling, faster development, improved resilience, technology flexibility, and easier maintenance..."
}
}
},
"usdCost": 0
}
Response Structure
data.fields: Contains all generated fields- Each field has a
string_valueproperty containing the generated content usdCost: The cost of the generation in USD (when applicable)
Use Cases
- Content Quality Control: Score and refine generated content
- Multi-path Workflows: Route based on content characteristics
- Iterative Refinement: Automatically improve outputs that don't meet thresholds
- A/B Testing: Generate multiple variants based on scoring
- Compliance Checking: Validate generated content meets requirements
Best Practices
- Clear Scoring Criteria: Be specific about what each dimension measures
- Reasonable Thresholds: Set achievable score thresholds (70-80 is often good)
- Multiple Dimensions: Use 2-4 dimensions for nuanced evaluation
- Actionable Branches: Make branch actions specific and targeted
- Use selectFields: Pass relevant context to branch schemas
tip
Decision points can be nested! You can have a decision point in a branch's then schema for multi-stage refinement.
Next Steps
- See Recursive Generation for nested structures
- Learn about Epistemic Uncertainty for confidence modeling
- Explore Code Assessment for code review workflows