File size: 4,465 Bytes
9355b63
ce01fb6
 
9e68e0a
b72c37c
24f5e39
9355b63
 
 
24f5e39
 
9355b63
 
 
1cca70d
 
9355b63
 
 
 
 
 
1cca70d
 
9355b63
 
 
 
 
 
 
 
 
 
1cca70d
24f5e39
 
 
 
 
 
 
b72c37c
 
ce01fb6
 
b72c37c
 
ce01fb6
b72c37c
ce01fb6
 
 
b72c37c
ce01fb6
b72c37c
 
 
24f5e39
 
 
 
b72c37c
ce01fb6
 
 
9355b63
 
 
 
633d400
b72c37c
9355b63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633d400
9355b63
 
 
 
ce01fb6
 
b72c37c
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
from typing import List, Optional
from pydantic import BaseModel, Field
import json

class AttachmentStyle(BaseModel):
    Secured: float = 0.0
    Anxious_Preoccupied: float = Field(0.0, alias="Anxious-Preoccupied")
    Dismissive_Avoidant: float = Field(0.0, alias="Dismissive-Avoidant")
    Fearful_Avoidant: float = Field(0.0, alias="Fearful-Avoidant")
    Self: int = 0
    Others: int = 0
    Anxiety: int = 0
    Avoidance: int = 0
    Explanation: str = "No explanation provided"

class BigFiveTraits(BaseModel):
    Extraversion: int = 0
    Agreeableness: int = 0
    Conscientiousness: int = 0
    Neuroticism: int = 0
    Openness: int = 0
    Explanation: str = "No explanation provided"

class PersonalityDisorder(BaseModel):
    Depressed: int = 0
    Paranoid: int = 0
    Schizoid_Schizotypal: int = Field(0, alias="Schizoid-Schizotypal")
    Antisocial_Psychopathic: int = Field(0, alias="Antisocial-Psychopathic")
    Borderline_Dysregulated: int = Field(0, alias="Borderline-Dysregulated")
    Narcissistic: int = 0
    Anxious_Avoidant: int = Field(0, alias="Anxious-Avoidant")
    Dependent_Victimized: int = Field(0, alias="Dependent-Victimized")
    Obsessional: int = 0
    Explanation: str = "No explanation provided"

class SpeakerAnalysis(BaseModel):
    Speaker: str = "Unknown Speaker"
    GeneralImpression: str = "No general impression provided"
    AttachmentStyle: AttachmentStyle = AttachmentStyle()
    BigFiveTraits: BigFiveTraits = BigFiveTraits()
    PersonalityDisorder: PersonalityDisorder = PersonalityDisorder()

class OutputParser:
    def parse(self, text: str) -> List[SpeakerAnalysis]:
        try:
            data = json.loads(text)
            if "speaker_analyses" in data and isinstance(data["speaker_analyses"], list):
                return [self.parse_speaker_analysis(item) for item in data["speaker_analyses"]]
            else:
                raise ValueError("Invalid JSON structure: missing or invalid 'speaker_analyses' key")
        except json.JSONDecodeError:
            raise ValueError(f"Invalid JSON: {text}")
        except Exception as e:
            raise ValueError(f"Error parsing output: {str(e)}")

    def parse_speaker_analysis(self, obj: dict) -> SpeakerAnalysis:
        return SpeakerAnalysis(
            Speaker=obj.get("Speaker", "Unknown Speaker"),
            GeneralImpression=obj.get("General Impression", "No general impression provided"),
            AttachmentStyle=AttachmentStyle(**(obj.get("Attachment Styles", {}))),
            BigFiveTraits=BigFiveTraits(**(obj.get("Big Five Traits", {}))),
            PersonalityDisorder=PersonalityDisorder(**(obj.get("Personality Disorders", {})))
        )

    def get_format_instructions(self) -> str:
        return """
        The output should be a JSON object with a "speaker_analyses" key containing an array of objects, where each object represents a speaker and has the following format:
        {
          "speaker_analyses": [
            {
              "Speaker": "1",
              "GeneralImpression": "General impression of Speaker 1",
              "attachment_styles": {
                "Secured": 0.5,
                "Anxious-Preoccupied": 0.2,
                "Dismissive-Avoidant": 0.1,
                "Fearful-Avoidant": 0.2,
                "Self": 7,
                "Others": 6,
                "Anxiety": 3,
                "Avoidance": 4,
                "Explanation": "Explanation of the attachment style for Speaker 1"
              },
              "big_five_traits": {
                "Extraversion": 5,
                "Agreeableness": 6,
                "Conscientiousness": 7,
                "Neuroticism": 4,
                "Openness": 5,
                "Explanation": "Explanation of the Big Five traits for Speaker 1"
              },
              "personality_disorders": {
                "Depressed": 0,
                "Paranoid": 0,
                "Schizoid-Schizotypal": 0,
                "Antisocial-Psychopathic": 0,
                "Borderline-Dysregulated": 0,
                "Narcissistic": 1,
                "Anxious-Avoidant": 2,
                "Dependent-Victimized": 0,
                "Obsessional": 1,
                "Explanation": "Explanation of the personality disorders for Speaker 1"
              }
            },
            {
              "Speaker": "2",
              ...
            }
          ]
        }
        """

output_parser = OutputParser()