-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatCBT.py
119 lines (96 loc) · 3.43 KB
/
chatCBT.py
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
# Combining the logic from both scripts
# Import necessary libraries
import json
import openai
from textblob import TextBlob
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
from rake_nltk import Rake
import dotenv
import os
# Load the .env file
dotenv.load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
class State:
def __init__(self, thought, feeling, behavior, memory):
self.thought = thought
self.feeling = feeling
self.behavior = behavior
self.memory = memory
def save(self):
with open('state.json', 'w') as f:
json.dump(self.__dict__, f)
@classmethod
def load(cls):
with open('state.json', 'r') as f:
data = json.load(f)
return cls(**data)
# Sentiment analysis
def analyze_sentiment(text):
analysis = TextBlob(text)
if analysis.sentiment.polarity > 0:
return 'positive'
elif analysis.sentiment.polarity < 0:
return 'negative'
else:
return 'neutral'
# Function to compare responses
def compare_responses(text1, text2):
vectorizer = CountVectorizer().fit_transform([text1, text2])
vectors = vectorizer.toarray()
return cosine_similarity(vectors)[0][1]
def chatbot(message, state):
# Thought generation
# Here, we are assuming that the "message" parameter is the user query.
prompt = f"User query: {message}\\n\\nSome thoughts on this query are:\\n"
completion = openai.Completion.create(
engine="text-davinci-001",
prompt=prompt,
max_tokens=100,
temperature=0.8,
top_p=0.9,
stop="\\n\\n"
)
thought = completion["choices"][0]["text"]
state.thought = thought
# Sentiment analysis for feeling determination
state.feeling = analyze_sentiment(thought)
# Behavior adjustment
thought_sentiment = analyze_sentiment(state.thought)
feeling_sentiment = analyze_sentiment(state.feeling)
if thought_sentiment == feeling_sentiment:
state.behavior += 0.1
else:
state.behavior -= 0.1
# Message generation
state_representation = f"Thought: {state.thought}, Feeling: {state.feeling}, Behavior: {state.behavior}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "system", "content": state_representation},
{"role": "user", "content": message}
],
temperature=state.behavior
)
response_content = response.choices[0].message.content
match_score = compare_responses(message, response_content)
state.memory.append({"message": message, "response": response_content, "match_score": match_score})
state.save()
return response_content
def main():
# Initialize a default state for the agent
state = State(thought="", feeling="", behavior=0.5, memory=[])
print("Welcome to the CBT Chatbot! Type 'exit' or 'quit' to end the conversation.")
while True:
# Get user input
message = input("You: ")
# Check if user wants to exit
if message.lower() in ["exit", "quit"]:
print("Exiting the chat. Have a great day!")
break
# Get response from chatbot and display it
response = chatbot(message, state)
print("Chatbot:", response)
if __name__ == "__main__":
main()