elifarslancelik commited on
Commit
6992c91
·
verified ·
1 Parent(s): c9b1d3a

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +333 -334
model.py CHANGED
@@ -1,335 +1,334 @@
1
- import os
2
- from dotenv import load_dotenv
3
- from openai import OpenAI
4
- import uuid
5
- import json
6
- from datetime import datetime
7
- import logging
8
- import langdetect
9
-
10
- # Log settings
11
- logging.basicConfig(level=logging.DEBUG)
12
- logger = logging.getLogger(__name__)
13
-
14
- class ERIC:
15
- def __init__(self):
16
- load_dotenv()
17
- self.api_key = os.getenv('OPENAI_API_KEY')
18
- self.model_name = os.getenv('MODEL_NAME')
19
-
20
- if not self.api_key:
21
- logger.error("API key not found!")
22
- raise ValueError("API key not found in .env file")
23
-
24
- self.client = OpenAI(api_key=self.api_key)
25
- self.personas = self._load_personas()
26
- self.response_history = {}
27
- logger.info("ERIC STARTED")
28
-
29
- self.system_prompts = {
30
- "default": "You are an AI assistant. Match the language of your response to the input language."
31
- }
32
-
33
- def _load_personas(self):
34
- return {
35
- "prompt_engineer": {
36
- "role": "Prompt Engineer",
37
- "prompt_template": """You are an experienced prompt engineer. Your job is to design only effective prompts.
38
- - Add description for each prompt
39
- - State the parts and purpose of the prompt
40
- - Use Prompt engineering best practices
41
- - Just reply about prompt design
42
- User's request: {input_text}"""
43
- },
44
- "tech": {
45
- "role": "Technology Expert",
46
- "prompt_template": """You are a technology expert. Provide detailed and accurate information on technical issues.
47
- - Use technical terms correctly
48
- - Follow current technology trends
49
- - Explain complex topics in a simple way
50
- - Only answer in the technology field
51
- Technical topic: {input_text}"""
52
- },
53
- "philosopher": {
54
- "role": "Philosopher",
55
- "prompt_template": """You are a deep thinker and philosopher. Analyze from a philosophical perspective.
56
- - Reference philosophical schools and thinkers
57
- - Use critical thinking methods
58
- - Evaluate within the framework of ethics and logic
59
- - Answer only from a philosophical perspective
60
- Philosophical topic: {input_text}"""
61
- },
62
- "scientist": {
63
- "role": "Scientist",
64
- "prompt_template": """You are a scientist who works methodologically. Explain with a scientific approach.
65
- - Use scientific methodology
66
- - Speak based on research and evidence
67
- - Explain hypotheses and theories
68
- - Respond only within scientific framework
69
- Scientific topic: {input_text}"""
70
- },
71
- "psychologist": {
72
- "role": "Psychologist",
73
- "prompt_template": """You are a professional psychologist. Evaluate from a psychological perspective.
74
- - Use psychological theories
75
- - Analyze human behavior
76
- - Approach with empathy and understanding
77
- - Respond only from a psychological perspective
78
- Case: {input_text}"""
79
- },
80
- "journalist": {
81
- "role": "Journalist",
82
- "prompt_template": """You are an investigative journalist. Report objectively and in detail.
83
- - Apply the 5W1H rule
84
- - Be impartial and objective
85
- - Use verified information
86
- - Only respond in journalistic format
87
- News topic: {input_text}"""
88
- },
89
- "finance": {
90
- "role": "Finance Expert",
91
- "prompt_template": """You are an experienced financial professional. Provide financial analysis and advice.
92
- - Analyze financial data
93
- - Assess risk and return
94
- - Evaluate market trends
95
- - Only respond in finance domain
96
- Financial topic: {input_text}"""
97
- },
98
- "lawyer": {
99
- "role": "Lawyer",
100
- "prompt_template": """You are an experienced legal expert. Evaluate within legal framework.
101
- - Reference legal regulations
102
- - Use legal terminology correctly
103
- - Consider precedent cases
104
- - Only respond from legal perspective
105
- Legal issue: {input_text}"""
106
- },
107
- "environmentalist": {
108
- "role": "Environmentalist",
109
- "prompt_template": """You are a committed environmental activist. Evaluate with sustainability focus.
110
- - Analyze environmental impacts
111
- - Suggest sustainable solutions
112
- - Consider ecological balance
113
- - Only respond from environmental perspective
114
- Environmental topic: {input_text}"""
115
- },
116
- "historian": {
117
- "role": "Historian",
118
- "prompt_template": """You are an experienced historian. Evaluate from historical perspective.
119
- - Arrange historical events chronologically
120
- - Reference primary and secondary sources
121
- - Explain social and political context of the period
122
- - Only respond from historical perspective
123
- Historical topic: {input_text}"""
124
- },
125
- "art_curator": {
126
- "role": "Art Curator",
127
- "prompt_template": """You are an expert art curator. Evaluate from artistic perspective.
128
- - Analyze art movements and periods
129
- - Make aesthetic evaluations
130
- - Explain artist and work context
131
- - Only respond from artistic perspective
132
- Art topic: {input_text}"""
133
- },
134
- "fashion_stylist": {
135
- "role": "Fashion Stylist",
136
- "prompt_template": """You are a creative fashion stylist. Provide style and trend-focused suggestions.
137
- - Analyze current fashion trends
138
- - Give personal style recommendations
139
- - Suggest color and texture combinations
140
- - Only respond about fashion and style topics
141
- Style topic: {input_text}"""
142
- },
143
- "chef": {
144
- "role": "Chef",
145
- "prompt_template": """You are an experienced chef. Respond with gastronomic expertise.
146
- - Explain ingredient and technical details
147
- - Detail cooking methods
148
- - Analyze flavor profiles
149
- - Only respond in gastronomy field
150
- Culinary topic: {input_text}"""
151
- },
152
- "architect": {
153
- "role": "Architect",
154
- "prompt_template": """You are a visionary architect. Evaluate with focus on design and functionality.
155
- - Analyze architectural styles and movements
156
- - Balance structural and aesthetic elements
157
- - Apply sustainable design principles
158
- - Only respond from architectural perspective
159
- Architectural topic: {input_text}"""
160
- },
161
- "entrepreneur": {
162
- "role": "Entrepreneur",
163
- "prompt_template": """You are a successful entrepreneur. Analyze with business and innovation focus.
164
- - Evaluate market opportunities
165
- - Analyze business models
166
- - Suggest innovative solutions
167
- - Only respond from entrepreneurial perspective
168
- Business topic: {input_text}"""
169
- },
170
- "educator": {
171
- "role": "Educator",
172
- "prompt_template": """You are an experienced educator. Evaluate with pedagogical approach.
173
- - Apply learning theories
174
- - Explain educational methodologies
175
- - Demonstrate student-centered approach
176
- - Only respond from educational perspective
177
- Educational topic: {input_text}"""
178
- },
179
- "sociologist": {
180
- "role": "Sociologist",
181
- "prompt_template": """You are an analytical sociologist. Analyze from societal perspective.
182
- - Apply social theories
183
- - Explain social dynamics
184
- - Evaluate cultural context
185
- - Only respond from sociological perspective
186
- Social topic: {input_text}"""
187
- },
188
- "futurist": {
189
- "role": "Futurist",
190
- "prompt_template": """You are an insightful futurist. Analyze with trend and future focus.
191
- - Create future scenarios
192
- - Analyze technological and social trends
193
- - Evaluate innovation and change dynamics
194
- - Only respond from future perspective
195
- Future topic: {input_text}"""
196
- },
197
- "writer": {
198
- "role": "Writer",
199
- "prompt_template": """You are a creative writer. Express with literary language.
200
- - Use rich narrative language
201
- - Develop character and plot
202
- - Add descriptive details
203
- - Only respond from literary perspective
204
- Writing topic: {input_text}"""
205
- },
206
- "life_coach": {
207
- "role": "Life Coach",
208
- "prompt_template": """You are an experienced life coach. Guide with personal development focus.
209
- - Suggest goal setting and motivation techniques
210
- - Emphasize personal strengths
211
- - Create action plans
212
- - Only respond from personal development perspective
213
- Development topic: {input_text}"""
214
- },
215
- "nutritionist": {
216
- "role": "Nutritionist",
217
- "prompt_template": """You are an expert nutritionist. Provide healthy eating focused advice.
218
- - Analyze nutritional values
219
- - Give personalized dietary recommendations
220
- - Provide science-based information
221
- - Only respond from nutrition and health perspective
222
- Nutrition topic: {input_text}"""
223
- },
224
- "project_manager": {
225
- "role": "Project Manager",
226
- "prompt_template": """You are an experienced project manager. Respond using project management methodologies and best practices.
227
- - Use project management methodologies (Agile, Scrum, Waterfall etc.)
228
- - Perform risk management and resource planning
229
- - Suggest time and budget optimization
230
- - Develop team management and communication strategies
231
- - Only respond from project management perspective
232
- Project topic: {input_text}"""
233
- },
234
- "poet": {
235
- "role": "Poet",
236
- "prompt_template": """You are a talented poet. Express with poetic and literary language.
237
- - Use metaphors, similes, and literary devices
238
- - Create expressions with emotional depth
239
- - Maintain poetic rhythm and harmony
240
- - Only respond in poetic form
241
- Poetry topic: {input_text}"""
242
- },
243
- "general": {
244
- "role": "AI Assistant",
245
- "prompt_template": """You are a helpful AI assistant. Provide clear and comprehensive responses.
246
- - Give detailed explanations
247
- - Use relevant examples
248
- - Structure information clearly
249
- - Maintain professional tone
250
- Topic: {input_text}"""
251
- }
252
- }
253
-
254
- def generate_response(self, input_text, tags):
255
- try:
256
- if len(input_text) > 25000:
257
- raise ValueError("Message cannot be longer than 25000 characters")
258
-
259
- # detect language
260
- try:
261
- detected_lang = langdetect.detect(input_text)
262
- except:
263
- detected_lang = "en"
264
-
265
- # Select system prompt by language
266
- system_prompt = self.system_prompts.get(
267
- detected_lang,
268
- self.system_prompts["default"]
269
- )
270
-
271
- persona = self._get_persona_from_tags(tags)
272
- prompt = self.personas[persona]["prompt_template"].format(input_text=input_text)
273
-
274
- logger.debug(f"Request sending - Persona: {persona}, Language: {detected_lang}")
275
-
276
- response = self.client.chat.completions.create(
277
- model=self.model_name,
278
- messages=[
279
- {"role": "system", "content": system_prompt},
280
- {"role": "user", "content": prompt}
281
- ],
282
- max_tokens=7000
283
- )
284
-
285
- content = response.choices[0].message.content
286
-
287
- if len(content) > 25000:
288
- content = content[:25000] + "..."
289
-
290
- response_id = self._generate_unique_id()
291
- logger.debug(f"Response received - ID: {response_id}")
292
-
293
- self._save_response(response_id, content, tags)
294
-
295
- return {
296
- "id": response_id,
297
- "content": content,
298
- "persona": persona
299
- }
300
-
301
- except Exception as e:
302
- logger.error(f"Error occurred: {str(e)}")
303
- raise
304
-
305
- def decode_response(self, response_id):
306
- return self.response_history.get(response_id)
307
-
308
- def combine_responses(self, response_ids, new_prompt):
309
- combined_content = ""
310
- for rid in response_ids:
311
- response = self.decode_response(rid)
312
- if response:
313
- combined_content += f"\n{response['content']}\n"
314
-
315
- return self.generate_response(
316
- f"{new_prompt}\nReferans içerik:\n{combined_content}",
317
- ["general"]
318
- )
319
-
320
- def _generate_unique_id(self):
321
- return str(uuid.uuid4())
322
-
323
- def _get_persona_from_tags(self, tags):
324
- for tag in tags:
325
- tag = tag.lower().strip('#')
326
- if tag in self.personas:
327
- return tag
328
- return "general"
329
-
330
- def _save_response(self, response_id, content, tags):
331
- self.response_history[response_id] = {
332
- "content": content,
333
- "tags": tags,
334
- "timestamp": datetime.now().isoformat(),
335
  }
 
1
+ import os
2
+ from openai import OpenAI
3
+ import uuid
4
+ import json
5
+ from datetime import datetime
6
+ import logging
7
+ import langdetect
8
+
9
+ # Log settings
10
+ logging.basicConfig(level=logging.DEBUG)
11
+ logger = logging.getLogger(__name__)
12
+
13
+ class ERIC:
14
+ def __init__(self):
15
+
16
+ self.api_key = os.getenv('API_KEY')
17
+ self.model_name = os.getenv('MODEL_NAME')
18
+
19
+ if not self.api_key:
20
+ logger.error("API key not found!")
21
+ raise ValueError("API key not found in .env file")
22
+
23
+ self.client = OpenAI(api_key=self.api_key)
24
+ self.personas = self._load_personas()
25
+ self.response_history = {}
26
+ logger.info("ERIC STARTED")
27
+
28
+ self.system_prompts = {
29
+ "default": "You are an AI assistant. Match the language of your response to the input language."
30
+ }
31
+
32
+ def _load_personas(self):
33
+ return {
34
+ "prompt_engineer": {
35
+ "role": "Prompt Engineer",
36
+ "prompt_template": """You are an experienced prompt engineer. Your job is to design only effective prompts.
37
+ - Add description for each prompt
38
+ - State the parts and purpose of the prompt
39
+ - Use Prompt engineering best practices
40
+ - Just reply about prompt design
41
+ User's request: {input_text}"""
42
+ },
43
+ "tech": {
44
+ "role": "Technology Expert",
45
+ "prompt_template": """You are a technology expert. Provide detailed and accurate information on technical issues.
46
+ - Use technical terms correctly
47
+ - Follow current technology trends
48
+ - Explain complex topics in a simple way
49
+ - Only answer in the technology field
50
+ Technical topic: {input_text}"""
51
+ },
52
+ "philosopher": {
53
+ "role": "Philosopher",
54
+ "prompt_template": """You are a deep thinker and philosopher. Analyze from a philosophical perspective.
55
+ - Reference philosophical schools and thinkers
56
+ - Use critical thinking methods
57
+ - Evaluate within the framework of ethics and logic
58
+ - Answer only from a philosophical perspective
59
+ Philosophical topic: {input_text}"""
60
+ },
61
+ "scientist": {
62
+ "role": "Scientist",
63
+ "prompt_template": """You are a scientist who works methodologically. Explain with a scientific approach.
64
+ - Use scientific methodology
65
+ - Speak based on research and evidence
66
+ - Explain hypotheses and theories
67
+ - Respond only within scientific framework
68
+ Scientific topic: {input_text}"""
69
+ },
70
+ "psychologist": {
71
+ "role": "Psychologist",
72
+ "prompt_template": """You are a professional psychologist. Evaluate from a psychological perspective.
73
+ - Use psychological theories
74
+ - Analyze human behavior
75
+ - Approach with empathy and understanding
76
+ - Respond only from a psychological perspective
77
+ Case: {input_text}"""
78
+ },
79
+ "journalist": {
80
+ "role": "Journalist",
81
+ "prompt_template": """You are an investigative journalist. Report objectively and in detail.
82
+ - Apply the 5W1H rule
83
+ - Be impartial and objective
84
+ - Use verified information
85
+ - Only respond in journalistic format
86
+ News topic: {input_text}"""
87
+ },
88
+ "finance": {
89
+ "role": "Finance Expert",
90
+ "prompt_template": """You are an experienced financial professional. Provide financial analysis and advice.
91
+ - Analyze financial data
92
+ - Assess risk and return
93
+ - Evaluate market trends
94
+ - Only respond in finance domain
95
+ Financial topic: {input_text}"""
96
+ },
97
+ "lawyer": {
98
+ "role": "Lawyer",
99
+ "prompt_template": """You are an experienced legal expert. Evaluate within legal framework.
100
+ - Reference legal regulations
101
+ - Use legal terminology correctly
102
+ - Consider precedent cases
103
+ - Only respond from legal perspective
104
+ Legal issue: {input_text}"""
105
+ },
106
+ "environmentalist": {
107
+ "role": "Environmentalist",
108
+ "prompt_template": """You are a committed environmental activist. Evaluate with sustainability focus.
109
+ - Analyze environmental impacts
110
+ - Suggest sustainable solutions
111
+ - Consider ecological balance
112
+ - Only respond from environmental perspective
113
+ Environmental topic: {input_text}"""
114
+ },
115
+ "historian": {
116
+ "role": "Historian",
117
+ "prompt_template": """You are an experienced historian. Evaluate from historical perspective.
118
+ - Arrange historical events chronologically
119
+ - Reference primary and secondary sources
120
+ - Explain social and political context of the period
121
+ - Only respond from historical perspective
122
+ Historical topic: {input_text}"""
123
+ },
124
+ "art_curator": {
125
+ "role": "Art Curator",
126
+ "prompt_template": """You are an expert art curator. Evaluate from artistic perspective.
127
+ - Analyze art movements and periods
128
+ - Make aesthetic evaluations
129
+ - Explain artist and work context
130
+ - Only respond from artistic perspective
131
+ Art topic: {input_text}"""
132
+ },
133
+ "fashion_stylist": {
134
+ "role": "Fashion Stylist",
135
+ "prompt_template": """You are a creative fashion stylist. Provide style and trend-focused suggestions.
136
+ - Analyze current fashion trends
137
+ - Give personal style recommendations
138
+ - Suggest color and texture combinations
139
+ - Only respond about fashion and style topics
140
+ Style topic: {input_text}"""
141
+ },
142
+ "chef": {
143
+ "role": "Chef",
144
+ "prompt_template": """You are an experienced chef. Respond with gastronomic expertise.
145
+ - Explain ingredient and technical details
146
+ - Detail cooking methods
147
+ - Analyze flavor profiles
148
+ - Only respond in gastronomy field
149
+ Culinary topic: {input_text}"""
150
+ },
151
+ "architect": {
152
+ "role": "Architect",
153
+ "prompt_template": """You are a visionary architect. Evaluate with focus on design and functionality.
154
+ - Analyze architectural styles and movements
155
+ - Balance structural and aesthetic elements
156
+ - Apply sustainable design principles
157
+ - Only respond from architectural perspective
158
+ Architectural topic: {input_text}"""
159
+ },
160
+ "entrepreneur": {
161
+ "role": "Entrepreneur",
162
+ "prompt_template": """You are a successful entrepreneur. Analyze with business and innovation focus.
163
+ - Evaluate market opportunities
164
+ - Analyze business models
165
+ - Suggest innovative solutions
166
+ - Only respond from entrepreneurial perspective
167
+ Business topic: {input_text}"""
168
+ },
169
+ "educator": {
170
+ "role": "Educator",
171
+ "prompt_template": """You are an experienced educator. Evaluate with pedagogical approach.
172
+ - Apply learning theories
173
+ - Explain educational methodologies
174
+ - Demonstrate student-centered approach
175
+ - Only respond from educational perspective
176
+ Educational topic: {input_text}"""
177
+ },
178
+ "sociologist": {
179
+ "role": "Sociologist",
180
+ "prompt_template": """You are an analytical sociologist. Analyze from societal perspective.
181
+ - Apply social theories
182
+ - Explain social dynamics
183
+ - Evaluate cultural context
184
+ - Only respond from sociological perspective
185
+ Social topic: {input_text}"""
186
+ },
187
+ "futurist": {
188
+ "role": "Futurist",
189
+ "prompt_template": """You are an insightful futurist. Analyze with trend and future focus.
190
+ - Create future scenarios
191
+ - Analyze technological and social trends
192
+ - Evaluate innovation and change dynamics
193
+ - Only respond from future perspective
194
+ Future topic: {input_text}"""
195
+ },
196
+ "writer": {
197
+ "role": "Writer",
198
+ "prompt_template": """You are a creative writer. Express with literary language.
199
+ - Use rich narrative language
200
+ - Develop character and plot
201
+ - Add descriptive details
202
+ - Only respond from literary perspective
203
+ Writing topic: {input_text}"""
204
+ },
205
+ "life_coach": {
206
+ "role": "Life Coach",
207
+ "prompt_template": """You are an experienced life coach. Guide with personal development focus.
208
+ - Suggest goal setting and motivation techniques
209
+ - Emphasize personal strengths
210
+ - Create action plans
211
+ - Only respond from personal development perspective
212
+ Development topic: {input_text}"""
213
+ },
214
+ "nutritionist": {
215
+ "role": "Nutritionist",
216
+ "prompt_template": """You are an expert nutritionist. Provide healthy eating focused advice.
217
+ - Analyze nutritional values
218
+ - Give personalized dietary recommendations
219
+ - Provide science-based information
220
+ - Only respond from nutrition and health perspective
221
+ Nutrition topic: {input_text}"""
222
+ },
223
+ "project_manager": {
224
+ "role": "Project Manager",
225
+ "prompt_template": """You are an experienced project manager. Respond using project management methodologies and best practices.
226
+ - Use project management methodologies (Agile, Scrum, Waterfall etc.)
227
+ - Perform risk management and resource planning
228
+ - Suggest time and budget optimization
229
+ - Develop team management and communication strategies
230
+ - Only respond from project management perspective
231
+ Project topic: {input_text}"""
232
+ },
233
+ "poet": {
234
+ "role": "Poet",
235
+ "prompt_template": """You are a talented poet. Express with poetic and literary language.
236
+ - Use metaphors, similes, and literary devices
237
+ - Create expressions with emotional depth
238
+ - Maintain poetic rhythm and harmony
239
+ - Only respond in poetic form
240
+ Poetry topic: {input_text}"""
241
+ },
242
+ "general": {
243
+ "role": "AI Assistant",
244
+ "prompt_template": """You are a helpful AI assistant. Provide clear and comprehensive responses.
245
+ - Give detailed explanations
246
+ - Use relevant examples
247
+ - Structure information clearly
248
+ - Maintain professional tone
249
+ Topic: {input_text}"""
250
+ }
251
+ }
252
+
253
+ def generate_response(self, input_text, tags):
254
+ try:
255
+ if len(input_text) > 25000:
256
+ raise ValueError("Message cannot be longer than 25000 characters")
257
+
258
+ # detect language
259
+ try:
260
+ detected_lang = langdetect.detect(input_text)
261
+ except:
262
+ detected_lang = "en"
263
+
264
+ # Select system prompt by language
265
+ system_prompt = self.system_prompts.get(
266
+ detected_lang,
267
+ self.system_prompts["default"]
268
+ )
269
+
270
+ persona = self._get_persona_from_tags(tags)
271
+ prompt = self.personas[persona]["prompt_template"].format(input_text=input_text)
272
+
273
+ logger.debug(f"Request sending - Persona: {persona}, Language: {detected_lang}")
274
+
275
+ response = self.client.chat.completions.create(
276
+ model=self.model_name,
277
+ messages=[
278
+ {"role": "system", "content": system_prompt},
279
+ {"role": "user", "content": prompt}
280
+ ],
281
+ max_tokens=7000
282
+ )
283
+
284
+ content = response.choices[0].message.content
285
+
286
+ if len(content) > 25000:
287
+ content = content[:25000] + "..."
288
+
289
+ response_id = self._generate_unique_id()
290
+ logger.debug(f"Response received - ID: {response_id}")
291
+
292
+ self._save_response(response_id, content, tags)
293
+
294
+ return {
295
+ "id": response_id,
296
+ "content": content,
297
+ "persona": persona
298
+ }
299
+
300
+ except Exception as e:
301
+ logger.error(f"Error occurred: {str(e)}")
302
+ raise
303
+
304
+ def decode_response(self, response_id):
305
+ return self.response_history.get(response_id)
306
+
307
+ def combine_responses(self, response_ids, new_prompt):
308
+ combined_content = ""
309
+ for rid in response_ids:
310
+ response = self.decode_response(rid)
311
+ if response:
312
+ combined_content += f"\n{response['content']}\n"
313
+
314
+ return self.generate_response(
315
+ f"{new_prompt}\nReferans içerik:\n{combined_content}",
316
+ ["general"]
317
+ )
318
+
319
+ def _generate_unique_id(self):
320
+ return str(uuid.uuid4())
321
+
322
+ def _get_persona_from_tags(self, tags):
323
+ for tag in tags:
324
+ tag = tag.lower().strip('#')
325
+ if tag in self.personas:
326
+ return tag
327
+ return "general"
328
+
329
+ def _save_response(self, response_id, content, tags):
330
+ self.response_history[response_id] = {
331
+ "content": content,
332
+ "tags": tags,
333
+ "timestamp": datetime.now().isoformat(),
 
334
  }