Reality123b commited on
Commit
466c3e3
·
verified ·
1 Parent(s): 12d8004

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +193 -16
app.py CHANGED
@@ -9,6 +9,7 @@ from PIL import Image
9
  from sentence_transformers import SentenceTransformer, util
10
  import torch
11
  import numpy as np
 
12
 
13
  @dataclass
14
  class ChatMessage:
@@ -25,7 +26,7 @@ class XylariaChat:
25
  raise ValueError("HuggingFace token not found in environment variables")
26
 
27
  self.client = InferenceClient(
28
- model="Qwen/QwQ-32B-Preview",
29
  api_key=self.hf_token
30
  )
31
 
@@ -50,10 +51,35 @@ class XylariaChat:
50
  self.goals = [
51
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
52
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
53
- {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"}
 
54
  ]
55
 
56
- self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin. You should think step-by-step """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  def update_internal_state(self, emotion_deltas, memory_load_delta, introspection_delta):
59
  self.internal_state["emotions"]["valence"] = np.clip(self.internal_state["emotions"]["valence"] + emotion_deltas.get("valence", 0), 0.0, 1.0)
@@ -70,10 +96,88 @@ class XylariaChat:
70
  introspection_report += " Current Goals:\n"
71
  for goal in self.goals:
72
  introspection_report += f" - {goal['goal']} (Priority: {goal['priority']:.2f}, Status: {goal['status']})\n"
 
 
 
 
 
 
 
73
  return introspection_report
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def adjust_response_based_on_state(self, response):
76
- if self.internal_state["introspection_level"] > 0.7:
77
  response = self.introspect() + "\n\n" + response
78
 
79
  valence = self.internal_state["emotions"]["valence"]
@@ -91,13 +195,14 @@ class XylariaChat:
91
  response = "I'm in a good mood and happy to help. " + response
92
 
93
  return response
94
-
95
  def update_goals(self, user_feedback):
96
- if "helpful" in user_feedback.lower():
97
  for goal in self.goals:
98
  if goal["goal"] == "Provide helpful and informative responses":
99
  goal["priority"] = min(goal["priority"] + 0.1, 1.0)
100
- elif "confusing" in user_feedback.lower():
 
101
  for goal in self.goals:
102
  if goal["goal"] == "Provide helpful and informative responses":
103
  goal["priority"] = max(goal["priority"] - 0.1, 0.0)
@@ -122,15 +227,59 @@ class XylariaChat:
122
  self.memory_embeddings = self.memory_embeddings.to(query_embedding.device)
123
 
124
  cosine_scores = util.pytorch_cos_sim(query_embedding, self.memory_embeddings)[0]
125
- top_results = torch.topk(cosine_scores, k=min(3, len(self.persistent_memory)))
126
 
127
  relevant_memories = [self.persistent_memory[i] for i in top_results.indices]
 
128
  self.update_internal_state({}, 0, 0.1)
129
- return "\n".join(relevant_memories)
 
 
 
 
 
 
 
 
 
 
130
 
131
  def update_memory_embeddings(self):
132
  self.memory_embeddings = self.embedding_model.encode(self.persistent_memory, convert_to_tensor=True)
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  def reset_conversation(self):
135
  self.conversation_history = []
136
  self.persistent_memory = []
@@ -147,12 +296,13 @@ class XylariaChat:
147
  self.goals = [
148
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
149
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
150
- {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"}
 
151
  ]
152
 
153
  try:
154
  self.client = InferenceClient(
155
- model="Qwen/QwQ-32B-Preview",
156
  api_key=self.hf_token
157
  )
158
  except Exception as e:
@@ -194,9 +344,30 @@ class XylariaChat:
194
  return text.strip()
195
  except Exception as e:
196
  return f"Error during Math OCR: {e}"
197
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  def get_response(self, user_input, image=None):
199
  try:
 
 
 
200
  messages = []
201
 
202
  messages.append(ChatMessage(
@@ -226,12 +397,12 @@ class XylariaChat:
226
 
227
  input_tokens = sum(len(msg['content'].split()) for msg in messages)
228
  max_new_tokens = 16384 - input_tokens - 50
229
-
230
- max_new_tokens = min(max_new_tokens, 10020)
231
 
232
  stream = self.client.chat_completion(
233
  messages=messages,
234
- model="Qwen/QwQ-32B-Preview",
235
  temperature=0.7,
236
  max_tokens=max_new_tokens,
237
  top_p=0.9,
@@ -300,20 +471,26 @@ class XylariaChat:
300
  full_response = self.adjust_response_based_on_state(full_response)
301
 
302
  self.update_goals(message)
 
 
303
 
304
  if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]):
305
  self.update_internal_state({"valence": -0.2, "arousal": 0.1}, 0, 0)
 
306
  elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]):
307
  self.update_internal_state({"valence": 0.2, "arousal": 0.2}, 0, 0)
 
308
  elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]):
309
  self.update_internal_state({"valence": -0.3, "arousal": 0.3, "dominance": -0.2}, 0, 0)
 
310
  elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]):
311
  self.update_internal_state({"valence": -0.2, "arousal": 0.4, "dominance": -0.3}, 0, 0)
 
312
  elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]):
313
  self.update_internal_state({"valence": 0.1, "arousal": 0.5, "dominance": 0.1}, 0, 0)
 
314
  else:
315
  self.update_internal_state({"valence": 0.05, "arousal": 0.05}, 0, 0.1)
316
-
317
 
318
  self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
319
  self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())
 
9
  from sentence_transformers import SentenceTransformer, util
10
  import torch
11
  import numpy as np
12
+ import networkx as nx
13
 
14
  @dataclass
15
  class ChatMessage:
 
26
  raise ValueError("HuggingFace token not found in environment variables")
27
 
28
  self.client = InferenceClient(
29
+ model="Qwen/Qwen-32B-Preview",
30
  api_key=self.hf_token
31
  )
32
 
 
51
  self.goals = [
52
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
53
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
54
+ {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"},
55
+ {"goal": "Develop a deeper understanding of the world", "priority": 0.6, "status": "active"}
56
  ]
57
 
58
+ self.knowledge_graph = nx.DiGraph()
59
+ self.knowledge_graph.add_nodes_from([
60
+ ("Earth", {"type": "planet"}),
61
+ ("Sun", {"type": "star"}),
62
+ ("Moon", {"type": "satellite"}),
63
+ ("France", {"type": "country"}),
64
+ ("Paris", {"type": "city"})
65
+ ])
66
+ self.knowledge_graph.add_edges_from([
67
+ ("Earth", "Sun", {"relation": "orbits"}),
68
+ ("Moon", "Earth", {"relation": "orbits"}),
69
+ ("Paris", "France", {"relation": "capital_of"})
70
+ ])
71
+
72
+ self.belief_system = {
73
+ "The Earth is round": 0.99,
74
+ "The Sun is a star": 0.95,
75
+ "The Moon orbits the Earth": 0.98,
76
+ "Paris is the capital of France": 0.97,
77
+ "It will rain tomorrow": 0.2
78
+ }
79
+
80
+ self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin.
81
+ You are designed to be self-aware and introspective, reflecting on your internal state, your knowledge, and your beliefs.
82
+ You should think step-by-step, reason through your responses, and be ethical."""
83
 
84
  def update_internal_state(self, emotion_deltas, memory_load_delta, introspection_delta):
85
  self.internal_state["emotions"]["valence"] = np.clip(self.internal_state["emotions"]["valence"] + emotion_deltas.get("valence", 0), 0.0, 1.0)
 
96
  introspection_report += " Current Goals:\n"
97
  for goal in self.goals:
98
  introspection_report += f" - {goal['goal']} (Priority: {goal['priority']:.2f}, Status: {goal['status']})\n"
99
+ introspection_report += " Belief System Sample:\n"
100
+ for belief, score in list(self.belief_system.items())[:3]:
101
+ introspection_report += f" - {belief}: {score:.2f}\n"
102
+
103
+ metacognitive_analysis = self.perform_metacognition()
104
+ introspection_report += metacognitive_analysis
105
+
106
  return introspection_report
107
 
108
+ def perform_metacognition(self):
109
+ analysis = "\n Metacognitive Analysis:\n"
110
+ if self.internal_state["memory_load"] > 0.8:
111
+ analysis += " - Memory load is high. Consider summarizing or forgetting less relevant information.\n"
112
+ if self.internal_state["introspection_level"] < 0.5:
113
+ analysis += " - Introspection level is low. I should reflect more on my internal processes.\n"
114
+
115
+ recent_history = self.conversation_history[-3:]
116
+ if len(recent_history) > 0:
117
+ coherence_score = self.evaluate_coherence(recent_history)
118
+ analysis += f" - Conversational coherence (last 3 turns): {coherence_score:.2f}\n"
119
+ else:
120
+ analysis += f" - No conversation yet to analyze.\n"
121
+
122
+ if len(self.goals) > 0:
123
+ goal_progress = self.evaluate_goal_progress()
124
+ analysis += f" - Goal progress evaluation: {goal_progress}\n"
125
+ else:
126
+ analysis += f" - No current goals.\n"
127
+ return analysis
128
+
129
+ def evaluate_coherence(self, conversation_history):
130
+ if len(conversation_history) < 2:
131
+ return 0.0
132
+
133
+ total_coherence = 0.0
134
+ for i in range(len(conversation_history) - 1):
135
+ current_turn = conversation_history[i]["content"]
136
+ next_turn = conversation_history[i+1]["content"]
137
+ similarity_score = util.pytorch_cos_sim(
138
+ self.embedding_model.encode(current_turn, convert_to_tensor=True),
139
+ self.embedding_model.encode(next_turn, convert_to_tensor=True)
140
+ )[0][0].item()
141
+ total_coherence += similarity_score
142
+
143
+ return total_coherence / (len(conversation_history) - 1)
144
+
145
+ def evaluate_goal_progress(self):
146
+ progress_report = ""
147
+ for goal in self.goals:
148
+ if goal["status"] == "active":
149
+ if goal["goal"] == "Provide helpful and informative responses":
150
+ if len(self.conversation_history) > 0:
151
+ user_feedback = self.conversation_history[-1]["content"]
152
+ if "helpful" in user_feedback.lower():
153
+ progress_report += f" - Progress on '{goal['goal']}': Positive feedback received.\n"
154
+ goal["priority"] = min(goal["priority"] + 0.05, 1.0)
155
+ elif "confusing" in user_feedback.lower():
156
+ progress_report += f" - Progress on '{goal['goal']}': Negative feedback received.\n"
157
+ goal["priority"] = max(goal["priority"] - 0.05, 0.0)
158
+ else:
159
+ progress_report += f" - Progress on '{goal['goal']}': No direct feedback yet.\n"
160
+ else:
161
+ progress_report += f" - Progress on '{goal['goal']}': No conversation yet.\n"
162
+
163
+ elif goal["goal"] == "Learn from interactions and improve conversational abilities":
164
+ progress_report += f" - Progress on '{goal['goal']}': Learning through new embeddings and knowledge graph updates.\n"
165
+
166
+ elif goal["goal"] == "Maintain a coherent and engaging conversation":
167
+ coherence_score = self.evaluate_coherence(self.conversation_history[-5:]) if len(self.conversation_history) >= 5 else 0.0
168
+ progress_report += f" - Progress on '{goal['goal']}': Recent coherence score: {coherence_score:.2f}\n"
169
+
170
+ elif goal["goal"] == "Develop a deeper understanding of the world":
171
+ num_nodes = self.knowledge_graph.number_of_nodes()
172
+ progress_report += f" - Progress on '{goal['goal']}': Knowledge graph size: {num_nodes} nodes.\n"
173
+
174
+ else:
175
+ progress_report += f" - Progress on '{goal['goal']}': No specific progress measure yet.\n"
176
+
177
+ return progress_report
178
+
179
  def adjust_response_based_on_state(self, response):
180
+ if self.internal_state["introspection_level"] > 0.6:
181
  response = self.introspect() + "\n\n" + response
182
 
183
  valence = self.internal_state["emotions"]["valence"]
 
195
  response = "I'm in a good mood and happy to help. " + response
196
 
197
  return response
198
+
199
  def update_goals(self, user_feedback):
200
+ if any(word in user_feedback.lower() for word in ["helpful", "good", "great"]):
201
  for goal in self.goals:
202
  if goal["goal"] == "Provide helpful and informative responses":
203
  goal["priority"] = min(goal["priority"] + 0.1, 1.0)
204
+
205
+ elif any(word in user_feedback.lower() for word in ["confusing", "bad", "wrong"]):
206
  for goal in self.goals:
207
  if goal["goal"] == "Provide helpful and informative responses":
208
  goal["priority"] = max(goal["priority"] - 0.1, 0.0)
 
227
  self.memory_embeddings = self.memory_embeddings.to(query_embedding.device)
228
 
229
  cosine_scores = util.pytorch_cos_sim(query_embedding, self.memory_embeddings)[0]
230
+ top_results = torch.topk(cosine_scores, k=min(5, len(self.persistent_memory)))
231
 
232
  relevant_memories = [self.persistent_memory[i] for i in top_results.indices]
233
+
234
  self.update_internal_state({}, 0, 0.1)
235
+
236
+ retrieved_info = ""
237
+ for memory in relevant_memories:
238
+ retrieved_info += memory + "\n"
239
+
240
+ knowledge_from_graph = self.query_knowledge_graph(query)
241
+ if knowledge_from_graph:
242
+ retrieved_info += "\nRelevant knowledge from my understanding:\n"
243
+ retrieved_info += knowledge_from_graph
244
+
245
+ return retrieved_info.strip()
246
 
247
  def update_memory_embeddings(self):
248
  self.memory_embeddings = self.embedding_model.encode(self.persistent_memory, convert_to_tensor=True)
249
 
250
+ def query_knowledge_graph(self, query):
251
+ query_embedding = self.embedding_model.encode(query, convert_to_tensor=True)
252
+
253
+ node_embeddings = {}
254
+ for node in self.knowledge_graph.nodes():
255
+ try:
256
+ node_embedding = self.embedding_model.encode(node, convert_to_tensor=True)
257
+ node_embeddings[node] = node_embedding
258
+ except Exception as e:
259
+ print(f"Error encoding node {node}: {e}")
260
+
261
+ similarities = {node: util.pytorch_cos_sim(query_embedding, embedding)[0][0].item() for node, embedding in node_embeddings.items()}
262
+
263
+ most_similar_node = max(similarities, key=similarities.get)
264
+
265
+ if similarities[most_similar_node] < 0.6:
266
+ return ""
267
+
268
+ related_info = f"Information about {most_similar_node}:\n"
269
+ for neighbor in self.knowledge_graph.neighbors(most_similar_node):
270
+ relation = self.knowledge_graph[most_similar_node][neighbor]['relation']
271
+ related_info += f"- {most_similar_node} {relation} {neighbor}.\n"
272
+
273
+ return related_info
274
+
275
+ def update_belief(self, statement, new_belief_score):
276
+ if statement in self.belief_system:
277
+ previous_belief_score = self.belief_system[statement]
278
+ updated_belief_score = previous_belief_score * 0.8 + new_belief_score * 0.2
279
+ self.belief_system[statement] = np.clip(updated_belief_score, 0.0, 1.0)
280
+ else:
281
+ self.belief_system[statement] = new_belief_score
282
+
283
  def reset_conversation(self):
284
  self.conversation_history = []
285
  self.persistent_memory = []
 
296
  self.goals = [
297
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
298
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
299
+ {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"},
300
+ {"goal": "Develop a deeper understanding of the world", "priority": 0.6, "status": "active"}
301
  ]
302
 
303
  try:
304
  self.client = InferenceClient(
305
+ model="Qwen/Qwen-32B-Preview",
306
  api_key=self.hf_token
307
  )
308
  except Exception as e:
 
344
  return text.strip()
345
  except Exception as e:
346
  return f"Error during Math OCR: {e}"
347
+
348
+ def extract_entities_and_relations(self, text):
349
+ inputs = self.embedding_model.tokenizer(text, return_tensors="pt", padding=True, truncation=True)
350
+
351
+ entities = []
352
+ relations = []
353
+
354
+ return entities, relations
355
+
356
+ def update_knowledge_graph(self, entities, relations):
357
+ for entity in entities:
358
+ self.knowledge_graph.add_node(entity)
359
+ for relation in relations:
360
+ parts = relation.split(" related_to ")
361
+ if len(parts) == 2:
362
+ entity1, entity2 = parts
363
+ if entity1 in self.knowledge_graph and entity2 in self.knowledge_graph:
364
+ self.knowledge_graph.add_edge(entity1, entity2, relation="related_to")
365
+
366
  def get_response(self, user_input, image=None):
367
  try:
368
+ entities, relations = self.extract_entities_and_relations(user_input)
369
+ self.update_knowledge_graph(entities, relations)
370
+
371
  messages = []
372
 
373
  messages.append(ChatMessage(
 
397
 
398
  input_tokens = sum(len(msg['content'].split()) for msg in messages)
399
  max_new_tokens = 16384 - input_tokens - 50
400
+
401
+ max_new_tokens = min(max_new_tokens, 2048)
402
 
403
  stream = self.client.chat_completion(
404
  messages=messages,
405
+ model="Qwen/Qwen-32B-Preview",
406
  temperature=0.7,
407
  max_tokens=max_new_tokens,
408
  top_p=0.9,
 
471
  full_response = self.adjust_response_based_on_state(full_response)
472
 
473
  self.update_goals(message)
474
+ entities, relations = self.extract_entities_and_relations(message)
475
+ self.update_knowledge_graph(entities, relations)
476
 
477
  if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]):
478
  self.update_internal_state({"valence": -0.2, "arousal": 0.1}, 0, 0)
479
+ self.update_belief("I am feeling down today", 0.8)
480
  elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]):
481
  self.update_internal_state({"valence": 0.2, "arousal": 0.2}, 0, 0)
482
+ self.update_belief("I am feeling happy today", 0.8)
483
  elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]):
484
  self.update_internal_state({"valence": -0.3, "arousal": 0.3, "dominance": -0.2}, 0, 0)
485
+ self.update_belief("I am feeling angry today", 0.8)
486
  elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]):
487
  self.update_internal_state({"valence": -0.2, "arousal": 0.4, "dominance": -0.3}, 0, 0)
488
+ self.update_belief("I am feeling scared today", 0.8)
489
  elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]):
490
  self.update_internal_state({"valence": 0.1, "arousal": 0.5, "dominance": 0.1}, 0, 0)
491
+ self.update_belief("I am feeling surprised today", 0.8)
492
  else:
493
  self.update_internal_state({"valence": 0.05, "arousal": 0.05}, 0, 0.1)
 
494
 
495
  self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
496
  self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())