Last active
September 17, 2023 12:16
-
-
Save tiny-rawr/b7bc898fc468108b7acedeb35bd8093a to your computer and use it in GitHub Desktop.
Extracts cognitive distortions from a journal entry as a quote, label and explanation, using ChatGPT's function calling feature.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
import json | |
openai.api_key = "YOUR API KEY" | |
journal_entry = """ | |
Today was terrible, just like every other day. It started off with me burning my toast—what a perfect example of how I can't do anything right. Clearly, I'm a total failure, not just in toasting bread but in life. I'm so clumsy and useless; it's no wonder people don't want to be around me. | |
I got to work late because of traffic. As I walked in, I caught a glimpse of my boss's face, and I knew he was thinking, "Here's that useless employee who can't even show up on time." Everyone at work must hate me; it's the only explanation. To top it off, a meeting was scheduled for tomorrow, and I'm convinced it's going to be about laying people off. I'm sure I'll be the first one to go. | |
My colleague complimented me on my presentation, but she was just being nice. Any idiot could have done it, and it probably didn't even matter because I stuttered during the Q&A. My whole career is a joke, built on some lucky breaks. | |
I came home, and the house was a mess. I should be better at keeping things clean. Anyone else would be. My partner said it was okay, but he must be so disappointed in me. I'm a disappointment to everyone; I'm sure of it. If I were a better person, I would have a cleaner house, be more punctual, and wouldn't mess up simple things like making toast. I should be more disciplined, more organized, more everything. | |
I got an email from an old friend inviting me to a party, but I already know I won't go. They wouldn't have invited me if they knew how pathetic I am. I'll just mess things up and make it awkward for everyone. It's better that I don't go; I don't want to ruin their good time. I blame myself for not being the person everyone wants me to be. | |
At the same time, if people were more understanding, they'd get why I'm like this. But they won't because people are generally terrible and uncaring. It's their fault I feel so miserable. | |
So, another day ends, and the cycle continues. I'm trapped in this pit of despair because of who I am—a complete and utter failure. There's no way out. | |
""" | |
# Make API call with function calling capability | |
api_call = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You extract every single example of cognitive distortions present in a journal entry, as direct quotes."}, | |
{"role": "user", "content": journal_entry} | |
], | |
functions=[{ | |
"name": "extract_cognitive_distortions", | |
"description": "You identify cognitive distortions present in a journal entry.", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"thinking patterns": { | |
"type": "array", | |
"items": { | |
"type": "object", | |
"properties": { | |
"quote": { | |
"type": "string", | |
"description": "A direct quote from the journal entry that most represents this thinking pattern" | |
}, | |
"thinking pattern": { | |
"type": "string", | |
"enum": ["Black or white thinking", "Overgeneralisation", "Labelling", "Fortune telling","Mind reading", "Blaming", "Catastrophising","Discounting the positives", "Emotional reasoning"] | |
}, | |
"explanation": { | |
"type": "string", | |
"description": "Explain why this is an example of the thinking pattern." | |
} | |
}, | |
"required": ["quote", "thinking pattern", "explanation"] | |
} | |
} | |
}, | |
"required": ["thinking patterns"] | |
} | |
}], | |
function_call={"name": "extract_cognitive_distortions"} | |
) | |
# Access output | |
output = api_call["choices"][0]["message"] | |
data = json.loads(output["function_call"]["arguments"]) if output.get("function_call") else {} | |
thinking_patterns = data.get('thinking patterns', []) | |
print(len(thinking_patterns)) | |
print(thinking_patterns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment