File size: 7,013 Bytes
6369972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
WBS Level 2: Create a Work Breakdown Structure (WBS) from a project plan.

https://en.wikipedia.org/wiki/Work_breakdown_structure

Focus is on the "Process style". 
Focus is not on the "product style".
"""
import os
import json
import time
from math import ceil
from typing import List, Optional
from uuid import uuid4
from dataclasses import dataclass
from pydantic import BaseModel, Field
from llama_index.core.llms.llm import LLM
from src.format_json_for_use_in_query import format_json_for_use_in_query

class SubtaskDetails(BaseModel):
    subtask_wbs_number: str = Field(
        description="The unique identifier assigned to each subtask. Example: ['1.', '2.', '3.', '6.2.2', '6.2.3', '6.2.4', 'Subtask 5:', 'Subtask 6:', 'S3.', 'S4.']."
    )
    subtask_title: str = Field(
        description="Start with a verb to clearly indicate the action required. Example: ['Secure funding', 'Obtain construction permits', 'Electrical installation', 'Commissioning and handover']."
    )

class MajorPhaseDetails(BaseModel):
    """
    A major phase in the project decomposed into smaller tasks.
    """
    major_phase_wbs_number: str = Field(
        description="The unique identifier assigned to each major phase. Example: ['1.', '2.', '3.', 'Phase 1:', 'Phase 2:', 'P1.', 'P2.']."
    )
    major_phase_title: str = Field(
        description="Action-oriented title of this primary phase of the project. Example: ['Project Initiation', 'Procurement', 'Construction', 'Operation and Maintenance']."
    )
    subtasks: list[SubtaskDetails] = Field(
        description="List of the subtasks or activities."
    )

class WorkBreakdownStructure(BaseModel):
    """
    The Work Breakdown Structure (WBS) is a hierarchical decomposition of the total scope of work to accomplish project objectives.
    It organizes the project into smaller, more manageable components.
    """
    major_phase_details: list[MajorPhaseDetails] = Field(
        description="List with each major phase broken down into subtasks or activities."
    )

QUERY_PREAMBLE = f"""
Create a work breakdown structure level 2 for this project.

A task can always be broken down into smaller, more manageable subtasks.

"""

@dataclass
class CreateWBSLevel2:
    """
    WBS Level 2: Creating a Work Breakdown Structure (WBS) from a project plan.
    """
    query: str
    response: dict
    metadata: dict
    major_phases_with_subtasks: list[dict]
    major_phases_uuids: list[str]
    task_uuids: list[str]

    @classmethod
    def format_query(cls, plan_json: dict, wbs_level1_json: dict) -> str:
        """
        Format the query for creating a Work Breakdown Structure (WBS) level 2.
        """
        if not isinstance(plan_json, dict):
            raise ValueError("Invalid plan_json.")
        if not isinstance(wbs_level1_json, dict):
            raise ValueError("Invalid wbs_level1_json.")
        
        # Having a uuid in the WBS Level 1 data trend to confuse the LLM, causing the LLM to attempt to insert all kinds of ids in the response.
        # Removing the id from the WBS Level 1 data, and there is less confusion about what the LLM should do.
        wbs_level1_json_without_id = wbs_level1_json.copy()
        wbs_level1_json_without_id.pop("id", None)

        query = f"""
The project plan:
{format_json_for_use_in_query(plan_json)}

WBS Level 1:
{format_json_for_use_in_query(wbs_level1_json_without_id)}
"""
        return query
    
    @classmethod
    def execute(cls, llm: LLM, query: str) -> 'CreateWBSLevel2':
        """
        Invoke LLM to create a Work Breakdown Structure (WBS) from a json representation of a project plan.
        """
        if not isinstance(llm, LLM):
            raise ValueError("Invalid LLM instance.")
        if not isinstance(query, str):
            raise ValueError("Invalid query.")

        start_time = time.perf_counter()

        sllm = llm.as_structured_llm(WorkBreakdownStructure)
        response = sllm.complete(QUERY_PREAMBLE + query)
        json_response = json.loads(response.text)

        end_time = time.perf_counter()
        duration = int(ceil(end_time - start_time))

        metadata = dict(llm.metadata)
        metadata["llm_classname"] = llm.class_name()
        metadata["duration"] = duration

        # Cleanup the json response from the LLM model, assign unique ids to each activity.
        result_major_phases_with_subtasks = []
        result_major_phases_uuids = []
        result_task_uuids = []
        for major_phase_detail in json_response['major_phase_details']:
            subtask_list = []
            for subtask in major_phase_detail['subtasks']:
                subtask_title = subtask['subtask_title']
                uuid = str(uuid4())
                subtask_item = {
                    "id": uuid,
                    "description": subtask_title,
                }
                subtask_list.append(subtask_item)
                result_task_uuids.append(uuid)

            uuid = str(uuid4())
            major_phase_item = {
                "id": uuid,
                "major_phase_title": major_phase_detail['major_phase_title'],
                "subtasks": subtask_list,
            }
            result_major_phases_with_subtasks.append(major_phase_item)
            result_major_phases_uuids.append(uuid)

        result = CreateWBSLevel2(
            query=query,
            response=json_response,
            metadata=metadata,
            major_phases_with_subtasks=result_major_phases_with_subtasks,
            major_phases_uuids=result_major_phases_uuids,
            task_uuids=result_task_uuids
        )
        return result

    def raw_response_dict(self, include_metadata=True, include_query=True) -> dict:
        d = self.response.copy()
        if include_metadata:
            d['metadata'] = self.metadata
        if include_query:
            d['query'] = self.query
        return d
    
if __name__ == "__main__":
    from llama_index.llms.ollama import Ollama

    # TODO: Eliminate hardcoded paths
    path = '/Users/neoneye/Desktop/planexe_data/plan.json'

    wbs_level1_json = {
        "id": "d0169227-bf29-4a54-a898-67d6ff4d1193",
        "project_title": "Establish a solar farm in Denmark",
        "final_deliverable": "Solar farm operational",
    }

    print(f"file: {path}")
    with open(path, 'r', encoding='utf-8') as f:
        plan_json = json.load(f)

    query = CreateWBSLevel2.format_query(plan_json, wbs_level1_json)

    model_name = "llama3.1:latest"
    # model_name = "qwen2.5-coder:latest"
    # model_name = "phi4:latest"
    llm = Ollama(model=model_name, request_timeout=120.0, temperature=0.5, is_function_calling_model=False)

    print(f"Query: {query}")
    result = CreateWBSLevel2.execute(llm, query)

    print("Response:")
    response_dict = result.raw_response_dict(include_query=False)
    print(json.dumps(response_dict, indent=2))

    print("\n\nExtracted result:")
    print(json.dumps(result.major_phases_with_subtasks, indent=2))