Skip to main content

Workflows

Learn how to create and manage workflows in Gentic. Workflows enable you to define complex, multi-step processes that your agents can execute.

Workflow Basics

Creating a Workflow

const workflow = await client.createWorkflow({
name: 'Customer Support Workflow',
description: 'Handles customer support requests',
steps: [
{
type: 'classify',
input: '{{user_message}}',
categories: ['technical', 'billing', 'general']
},
{
type: 'search',
query: '{{user_message}}',
knowledgeSourceId: 'support-kb'
},
{
type: 'generate',
prompt: 'Based on the classification and search results, provide a helpful response',
model: 'gpt-4'
}
]
});

Step Types

1. Classification Steps

{
type: 'classify',
input: '{{user_message}}',
categories: ['category1', 'category2'],
model: 'gpt-4'
}

2. Search Steps

{
type: 'search',
query: '{{user_message}}',
knowledgeSourceId: 'kb-id',
maxResults: 5
}

3. Generation Steps

{
type: 'generate',
prompt: 'Generate a response based on: {{search_results}}',
model: 'gpt-4',
parameters: {
temperature: 0.7,
maxTokens: 1000
}
}

4. API Call Steps

{
type: 'api',
method: 'POST',
url: 'https://api.example.com/endpoint',
headers: {
'Authorization': 'Bearer {{api_key}}'
},
body: {
query: '{{user_message}}'
}
}

Workflow Control Flow

Sequential Execution

const workflow = await client.createWorkflow({
name: 'Sequential Workflow',
steps: [
{
type: 'step1',
// ...
},
{
type: 'step2',
// ...
}
]
});

Conditional Execution

const workflow = await client.createWorkflow({
name: 'Conditional Workflow',
steps: [
{
type: 'classify',
// ...
},
{
type: 'branch',
condition: '{{classification}} === "technical"',
trueBranch: [
{
type: 'search',
knowledgeSourceId: 'technical-kb'
}
],
falseBranch: [
{
type: 'search',
knowledgeSourceId: 'general-kb'
}
]
}
]
});

Parallel Execution

const workflow = await client.createWorkflow({
name: 'Parallel Workflow',
steps: [
{
type: 'parallel',
branches: [
{
steps: [
{
type: 'search',
knowledgeSourceId: 'kb1'
}
]
},
{
steps: [
{
type: 'search',
knowledgeSourceId: 'kb2'
}
]
}
]
},
{
type: 'merge',
strategy: 'combine'
}
]
});

Workflow Variables

Using Variables

const workflow = await client.createWorkflow({
name: 'Variable Workflow',
variables: {
user_query: '{{user_message}}',
search_results: '{{step1.results}}'
},
steps: [
{
type: 'search',
query: '{{user_query}}'
},
{
type: 'generate',
prompt: 'Based on: {{search_results}}'
}
]
});

Error Handling

Try-Catch Blocks

const workflow = await client.createWorkflow({
name: 'Error Handling Workflow',
steps: [
{
type: 'try',
steps: [
{
type: 'api',
// ...
}
],
catch: [
{
type: 'generate',
prompt: 'An error occurred: {{error}}'
}
]
}
]
});

Retry Logic

const workflow = await client.createWorkflow({
name: 'Retry Workflow',
steps: [
{
type: 'retry',
maxAttempts: 3,
delay: '1s',
steps: [
{
type: 'api',
// ...
}
]
}
]
});

Workflow Monitoring

Execution History

// Get workflow executions
const executions = await client.getWorkflowExecutions(workflowId, {
limit: 10,
offset: 0
});

// Get execution details
const execution = await client.getWorkflowExecution(executionId);

Performance Metrics

// Get workflow metrics
const metrics = await client.getWorkflowMetrics(workflowId, {
startDate: '2024-01-01',
endDate: '2024-01-31'
});

Best Practices

  1. Modular Design: Break complex workflows into smaller, reusable components
  2. Error Handling: Implement proper error handling and recovery
  3. Monitoring: Track workflow execution and performance
  4. Testing: Test workflows thoroughly before deployment
  5. Documentation: Document workflow purpose and behavior

Example Workflows

Customer Support Workflow

const supportWorkflow = await client.createWorkflow({
name: 'Customer Support',
steps: [
{
type: 'classify',
input: '{{user_message}}',
categories: ['technical', 'billing', 'general']
},
{
type: 'search',
query: '{{user_message}}',
knowledgeSourceId: 'support-kb'
},
{
type: 'generate',
prompt: 'Provide a helpful response based on the classification and search results'
}
]
});

Data Processing Workflow

const dataWorkflow = await client.createWorkflow({
name: 'Data Processing',
steps: [
{
type: 'parallel',
branches: [
{
steps: [
{
type: 'api',
url: 'https://api.data-source-1.com'
}
]
},
{
steps: [
{
type: 'api',
url: 'https://api.data-source-2.com'
}
]
}
]
},
{
type: 'merge',
strategy: 'combine'
},
{
type: 'generate',
prompt: 'Analyze and summarize the combined data'
}
]
});

Next Steps