-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatapp.py
More file actions
126 lines (104 loc) · 4.15 KB
/
chatapp.py
File metadata and controls
126 lines (104 loc) · 4.15 KB
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
119
120
121
122
123
124
125
126
import os
import openai
import json
import logging
from abc import ABC, abstractmethod
logging.basicConfig(level=logging.INFO)
class DataStore(ABC):
"""Abstract base class for data storage."""
@abstractmethod
def load(self, user_id):
"""Load data for a user."""
pass
@abstractmethod
def save(self, user_id, data):
"""Save data for a user."""
pass
class JsonDataStore(DataStore):
"""Class for JSON file data storage."""
def __init__(self, filename):
self.filename = filename
def load(self, user_id):
"""Load data for a user from a JSON file."""
try:
with open(self.filename, 'r') as f:
all_data = json.load(f)
return all_data.get(user_id, {})
except (IOError, json.JSONDecodeError):
return {}
def save(self, user_id, data):
"""Save data for a user to a JSON file."""
try:
with open(self.filename, 'r') as f:
all_data = json.load(f)
except (IOError, json.JSONDecodeError):
all_data = {}
all_data[user_id] = data
with open(self.filename, 'w') as f:
json.dump(all_data, f)
class ChatApp:
"""Main application class."""
def __init__(self, user_id, datastore):
self.user_id = user_id
self.datastore = datastore
self.load_data()
def load_data(self):
"""Load user data from data store."""
try:
data = self.datastore.load(self.user_id)
self.user_profile = data.get('user_profile', {})
self.conversation_history = data.get('conversation_history', [])
except Exception as e:
logging.error(f"Error loading data: {e}")
self.user_profile = {}
self.conversation_history = []
def save_data(self):
"""Save user data to data store."""
data = {
'user_profile': self.user_profile,
'conversation_history': self.conversation_history
}
try:
self.datastore.save(self.user_id, data)
except Exception as e:
logging.error(f"Error saving data: {e}")
def generate_response(self, user_message, message_type):
"""Generate a response using OpenAI's GPT-3.5-turbo model."""
try:
if not user_message or not message_type:
raise ValueError("User message and message type are required.")
instruction = f"Generate a {message_type} response to the following message: {user_message}"
context = {
'user_profile': self.user_profile,
'conversation_history': self.conversation_history
}
messages = [
{"role": "system", "content": json.dumps(context)},
{"role": "user", "content": instruction}
]
response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
max_tokens=150
)
self.conversation_history.append(response.choices[0].message['content'])
self.save_data()
return response.choices[0].message['content']
except (ValueError, openai.Error) as e:
logging.error(f"Error generating response: {e}")
return None
def main():
logging.info("Disclaimer: This application is designed to help generate responses for social interactions. It should not be used to deceive or mislead others. Always be honest and respectful in your communications.")
openai.api_key = os.environ.get('OPENAI_API_KEY')
if not openai.api_key:
raise ValueError("OpenAI API key is required.")
user_id = input("Enter your user ID: ")
if not user_id:
raise ValueError("User ID is required.")
chat_app = ChatApp(user_id, JsonDataStore('data.json'))
user_message = input("Enter the message you received: ")
message_type = input("Enter the type of message (e.g. flirty,professional, friendly): ")
suggested_response = chat_app.generate_response(user_message, message_type)
print("Suggested response: " + suggested_response)
if __name__ == '__main__':
main()