File size: 4,903 Bytes
1cca70d
 
8df2e34
 
9e68e0a
1cca70d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b20aae
1cca70d
 
 
 
 
 
 
 
 
4fad525
1cca70d
 
 
 
 
 
 
 
 
 
 
 
 
6b20aae
1cca70d
 
 
6b20aae
1cca70d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate
from pydantic import BaseModel
from typing import Dict

class AttachmentStyle(BaseModel):
    speaker: str
    secured: float
    anxious_preoccupied: float
    dismissive_avoidant: float
    fearful_avoidant: float
    self_rating: int
    others_rating: int
    anxiety: int
    avoidance: int
    explanation: str

class BigFiveTraits(BaseModel):
    speaker: str
    extraversion: int
    agreeableness: int
    conscientiousness: int
    neuroticism: int
    openness: int
    explanation: str

class PersonalityDisorder(BaseModel):
    speaker: str
    depressed: int
    paranoid: int
    schizoid_schizotypal: int
    antisocial_psychopathic: int
    borderline_dysregulated: int
    narcissistic: int
    anxious_avoidant: int
    dependent_victimized: int
    obsessional: int
    explanation: str

attachment_response_schemas = [
    ResponseSchema(name="speaker", description="The name or number of the speaker"),
    ResponseSchema(name="secured", description="Probability of secured attachment style (0-1)"),
    ResponseSchema(name="anxious_preoccupied", description="Probability of anxious-preoccupied attachment style (0-1)"),
    ResponseSchema(name="dismissive_avoidant", description="Probability of dismissive-avoidant attachment style (0-1)"),
    ResponseSchema(name="fearful_avoidant", description="Probability of fearful-avoidant attachment style (0-1)"),
    ResponseSchema(name="self_rating", description="Self rating (0-10)"),
    ResponseSchema(name="others_rating", description="Others rating (0-10)"),
    ResponseSchema(name="anxiety", description="Anxiety rating (0-10)"),
    ResponseSchema(name="avoidance", description="Avoidance rating (0-10)"),
    ResponseSchema(name="explanation", description="Brief explanation of the attachment style")
]

bigfive_response_schemas = [
    ResponseSchema(name="speaker", description="The name or number of the speaker"),
    ResponseSchema(name="extraversion", description="Extraversion rating (-10 to 10)"),
    ResponseSchema(name="agreeableness", description="Agreeableness rating (-10 to 10)"),
    ResponseSchema(name="conscientiousness", description="Conscientiousness rating (-10 to 10)"),
    ResponseSchema(name="neuroticism", description="Neuroticism rating (-10 to 10)"),
    ResponseSchema(name="openness", description="Openness rating (-10 to 10)"),
    ResponseSchema(name="explanation", description="Brief explanation of the Big Five traits")
]

personality_response_schemas = [
    ResponseSchema(name="speaker", description="The name or number of the speaker"),
    ResponseSchema(name="depressed", description="Depressed rating (0-4)"),
    ResponseSchema(name="paranoid", description="Paranoid rating (0-4)"),
    ResponseSchema(name="schizoid_schizotypal", description="Schizoid-Schizotypal rating (0-4)"),
    ResponseSchema(name="antisocial_psychopathic", description="Antisocial-Psychopathic rating (0-4)"),
    ResponseSchema(name="borderline_dysregulated", description="Borderline-Dysregulated rating (0-4)"),
    ResponseSchema(name="narcissistic", description="Narcissistic rating (0-4)"),
    ResponseSchema(name="anxious_avoidant", description="Anxious-Avoidant rating (0-4)"),
    ResponseSchema(name="dependent_victimized", description="Dependent-Victimized rating (0-4)"),
    ResponseSchema(name="obsessional", description="Obsessional rating (0-4)"),
    ResponseSchema(name="explanation", description="Brief explanation of the personality disorders")
]

attachment_parser = StructuredOutputParser.from_response_schemas(attachment_response_schemas)
bigfive_parser = StructuredOutputParser.from_response_schemas(bigfive_response_schemas)
personality_parser = StructuredOutputParser.from_response_schemas(personality_response_schemas)

def get_prompt_template(task: str, parser: StructuredOutputParser) -> PromptTemplate:
    return PromptTemplate(
        template="Analyze the following text according to the given task:\n\n{task}\n\n{format_instructions}\n\nText: {text}\n\nAnalysis:",
        input_variables=["text"],
        partial_variables={
            "task": task,
            "format_instructions": parser.get_format_instructions()
        }
    )

def parse_analysis_output(output: str, analysis_type: str) -> Dict[str, BaseModel]:
    if analysis_type == "attachments":
        parsed = attachment_parser.parse(output)
        return {parsed['speaker']: AttachmentStyle(**parsed)}
    elif analysis_type == "bigfive":
        parsed = bigfive_parser.parse(output)
        return {parsed['speaker']: BigFiveTraits(**parsed)}
    elif analysis_type == "personalities":
        parsed = personality_parser.parse(output)
        return {parsed['speaker']: PersonalityDisorder(**parsed)}
    else:
        raise ValueError(f"Unknown analysis type: {analysis_type}")