# {{name}} - Lab Agentic Process

{{description}}

## Overview

This project creates a complete Lab agentic process (flow) with:
- Artifact type for data classification
- Knowledge base for storing process artifacts
- Task definition for workflow steps
- Process orchestration with events and sequence flows

## Prerequisites

1. **PyGEAI installed and configured:**
   ```bash
   pip install pygeai
   geai configure --key YOUR_API_KEY --url YOUR_API_URL
   ```

2. **Lab agent must exist:**
   Ensure the Lab agent `{{agent_name}}` is created.
   
   If not, create it using the lab-agent-basic template:
   ```bash
   geai generate create --template lab-agent-basic --output ./my-agent --name {{agent_name}}
   cd my-agent
   python create_{{agent_name}}.py
   ```

## Workflow

Run the scripts in order:

### Step 1: Create Artifact Type

```bash
python create_artifact_type.py
```

This defines the type of artifacts (data) the process will handle.

### Step 2: Create Knowledge Base

```bash
python create_kb.py
```

This creates a knowledge base linked to the artifact type for storing process data.

### Step 3: Create Task

```bash
python create_task.py
```

This defines the task that the agent will execute during the process.

### Step 4: Create Process

```bash
python create_process.py
```

This creates the complete agentic process with:
- Workflow orchestration
- Event triggers
- Sequence flows
- Agent activities

## Process Architecture

The created process follows this flow:

```
[Artifact Upload Event]
         |
         v
  [Main Activity]
    (Agent Task)
         |
         v
 [Process Completed]
         |
         v
      [End]
```

### Components

1. **Artifact Type**: Classifies the data type (`{{name}}_artifact`)
2. **Knowledge Base**: Stores artifacts (`{{name}}_kb`)
3. **Task**: Defines what the agent does (`{{task_name}}`)
4. **Process**: Orchestrates the workflow (`{{name}}`)

### Events

- **Start Event**: Triggered when artifact is uploaded
- **End Event**: Process completion

### Signals

- **Artifact Signal**: Handles artifact uploads
- **User Signal**: Marks process completion

### Sequence Flows

1. Artifact upload → Main activity
2. Main activity → Process completed
3. Process completed → End

## Using the Process

After creation, interact with the process using:

### Chat Interface

```bash
python -c "
from pygeai.chat.clients import ChatClient

client = ChatClient()
response = client.chat_completion(
    model='saia:flow:{{name}}',
    messages=[{'role': 'user', 'content': 'Process this data'}],
    stream=False
)
print(response['choices'][0]['message']['content'])
"
```

### Streaming Chat Template

Use the chat-flow-streaming template:

```bash
geai generate create --template chat-flow-streaming --output ./chat --name {{name}}
cd chat
python chat_{{name}}.py
```

## Customization

### Add More Activities

Edit `create_process.py` to add multiple agent activities:

```python
agentic_activities=[
    AgenticActivity(
        key="activity1",
        name="First Step",
        task_name="{{task_name}}",
        agent_name="{{agent_name}}",
        agent_revision_id=0
    ),
    AgenticActivity(
        key="activity2",
        name="Second Step",
        task_name="another_task",
        agent_name="another_agent",
        agent_revision_id=0
    )
]
```

### Add Knowledge Base Metadata

Edit `create_kb.py`:

```python
knowledge_base = KnowledgeBase(
    name="{{name}}_kb",
    artifact_type_name=["{{name}}_artifact"],
    metadata=["priority", "category", "status"]
)
```

### Multiple Artifact Types

Edit `create_process.py`:

```python
artifact_signals=[
    ArtifactSignal(
        key="artifact1",
        name="artifact.upload",
        handling_type="C",
        artifact_type_name=["type1", "type2"]
    )
]
```

### Conditional Flows

Add conditional sequence flows:

```python
sequence_flows=[
    SequenceFlow(
        key="flow1",
        source_key="start",
        target_key="activity1"
    ),
    SequenceFlow(
        key="flow2_success",
        source_key="activity1",
        target_key="activity2",
        condition="status == 'success'"
    ),
    SequenceFlow(
        key="flow2_fail",
        source_key="activity1",
        target_key="error_handler",
        condition="status == 'error'"
    )
]
```

## Troubleshooting

**Agent not found:**
```
Error: Agent {{agent_name}} does not exist
```
Create the agent first using lab-agent-basic template.

**Artifact type already exists:**
The artifact type name must be unique. Change it in `create_artifact_type.py`.

**Knowledge base creation fails:**
Ensure the artifact type was created successfully in Step 1.

**Process creation fails:**
Verify that task and knowledge base exist before creating the process.

## Listing Resources

```bash
# List artifact types
python -c "from pygeai.lab.managers import AILabManager; print(AILabManager().list_artifact_types())"

# List knowledge bases
python -c "from pygeai.lab.managers import AILabManager; print(AILabManager().list_knowledge_bases())"

# List tasks
python -c "from pygeai.lab.managers import AILabManager; print(AILabManager().list_tasks())"

# List processes
python -c "from pygeai.lab.managers import AILabManager; print(AILabManager().list_processes())"
```

## Cleanup

To delete created resources:

```python
from pygeai.lab.managers import AILabManager

manager = AILabManager()

# Delete process
processes = manager.list_processes()
for p in processes:
    if p.name == "{{name}}":
        manager.delete_process(p.id)

# Delete task
tasks = manager.list_tasks()
for t in tasks:
    if t.name == "{{task_name}}":
        manager.delete_task(t.id)

# Delete knowledge base
kbs = manager.list_knowledge_bases()
for kb in kbs:
    if kb.name == "{{name}}_kb":
        manager.delete_knowledge_base(kb.id)

# Delete artifact type
types = manager.list_artifact_types()
for at in types:
    if at.name == "{{name}}_artifact":
        manager.delete_artifact_type(at.id)
```

## Next Steps

- Add more complex workflow logic with multiple agents
- Implement error handling and retry logic
- Create monitoring and logging for process execution
- Build UI for process management using web-ui-gradio template

## Documentation

- [PyGEAI Lab Processes](https://pygeai.readthedocs.io/en/latest/lab/processes/)
- [Agentic Workflows](https://pygeai.readthedocs.io/en/latest/lab/workflows/)
- [Knowledge Bases](https://pygeai.readthedocs.io/en/latest/lab/kb/)
