File size: 2,944 Bytes
af30a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# llm_integration/task_extraction.py
from openai import OpenAI
import re
import json

def extract_json_from_raw_response(raw_response):
    """
    Extract the JSON part from the raw response string.
    
    Args:
        raw_response (str): The raw response from the LLM containing JSON and additional text.

    Returns:
        dict: Parsed JSON object.
    """
    # Use regex to extract the JSON block between ```json and ```
    match = re.search(r"```json(.*?)```", raw_response, re.DOTALL)
    if match:
        json_string = match.group(1).strip()  # Extract the matched JSON part
        try:
            json_data = json.loads(json_string)  # Parse the JSON string into a Python dictionary
            return json_data
        except json.JSONDecodeError as e:
            print(f"Error decoding JSON: {e}")
            return None
    else:
        print("No valid JSON block found in the response.")
        return None

    
    
def extract_tasks_from_text(conversation_text):
    """Send conversation text to the LLM and extract tasks in JSON format."""
    # Define the prompt
    prompt = f"""
    Extract detailed project information from the following text and structure it in JSON format. 
    The JSON should have each project as a main key, with tasks as subkeys. For each task, include 
    the following fields: "description", "priority", "assigned_to", and "current_status". 
    Use the conversation details to populate the values accurately.

    Text:
    '''
    {conversation_text}
    '''

    Expected JSON Output:
    {{
      "project_name_1": {{
        "Task-1": {{
          "description": "Brief description of the task",
          "priority": "high/medium/low",
          "assigned_to": "Person responsible",
          "current_status": "Status of the task (e.g., completed, in progress, pending)"
        }},
        "Task-2": {{
          "description": "Brief description of the task",
          "priority": "high/medium/low",
          "assigned_to": "Person responsible",
          "current_status": "Status of the task (e.g., completed, in progress, pending)"
        }}
      }},
      "project_name_2": {{
        "Task-1": {{
          "description": "Brief description of the task",
          "priority": "high/medium/low",
          "assigned_to": "Person responsible",
          "current_status": "Status of the task (e.g., completed, in progress, pending)"
        }}
      }}
    }}
    """

    client = OpenAI(api_key='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')
    
    stream = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content":prompt}],
        # stream=True,
    )
    raw_response = stream.choices[0].message.content
    final_response= extract_json_from_raw_response(raw_response)
    return final_response