Spaces:
Running
Running
Update app.py
Browse files
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:
|
|
@@ -37,6 +38,15 @@ class XylariaChat:
|
|
| 37 |
self.memory_embeddings = None
|
| 38 |
self.embedding_model = SentenceTransformer('all-mpnet-base-v2')
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
self.internal_state = {
|
| 41 |
"emotions": {
|
| 42 |
"valence": 0.5,
|
|
@@ -62,6 +72,41 @@ class XylariaChat:
|
|
| 62 |
self.internal_state["memory_load"] = np.clip(self.internal_state["memory_load"] + memory_load_delta, 0.0, 1.0)
|
| 63 |
self.internal_state["introspection_level"] = np.clip(self.internal_state["introspection_level"] + introspection_delta, 0.0, 1.0)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def introspect(self):
|
| 66 |
introspection_report = "Introspection Report:\n"
|
| 67 |
introspection_report += f" Current Emotional State (VAD): {self.internal_state['emotions']}\n"
|
|
@@ -70,6 +115,11 @@ 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):
|
|
@@ -150,6 +200,15 @@ class XylariaChat:
|
|
| 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",
|
|
@@ -223,6 +282,19 @@ class XylariaChat:
|
|
| 223 |
role="user",
|
| 224 |
content=user_input
|
| 225 |
).to_dict())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
input_tokens = sum(len(msg['content'].split()) for msg in messages)
|
| 228 |
max_new_tokens = 16384 - input_tokens - 50
|
|
@@ -244,6 +316,12 @@ class XylariaChat:
|
|
| 244 |
print(f"Detailed error in get_response: {e}")
|
| 245 |
return f"Error generating response: {str(e)}"
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
def messages_to_prompt(self, messages):
|
| 248 |
prompt = ""
|
| 249 |
for msg in messages:
|
|
|
|
| 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:
|
|
|
|
| 38 |
self.memory_embeddings = None
|
| 39 |
self.embedding_model = SentenceTransformer('all-mpnet-base-v2')
|
| 40 |
|
| 41 |
+
self.knowledge_graph = nx.DiGraph()
|
| 42 |
+
self.belief_system = {}
|
| 43 |
+
self.metacognitive_layer = {
|
| 44 |
+
"coherence_score": 0.0,
|
| 45 |
+
"relevance_score": 0.0,
|
| 46 |
+
"bias_detection": 0.0,
|
| 47 |
+
"strategy_adjustment": ""
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
self.internal_state = {
|
| 51 |
"emotions": {
|
| 52 |
"valence": 0.5,
|
|
|
|
| 72 |
self.internal_state["memory_load"] = np.clip(self.internal_state["memory_load"] + memory_load_delta, 0.0, 1.0)
|
| 73 |
self.internal_state["introspection_level"] = np.clip(self.internal_state["introspection_level"] + introspection_delta, 0.0, 1.0)
|
| 74 |
|
| 75 |
+
def update_knowledge_graph(self, entities, relationships):
|
| 76 |
+
for entity in entities:
|
| 77 |
+
self.knowledge_graph.add_node(entity)
|
| 78 |
+
for relationship in relationships:
|
| 79 |
+
subject, predicate, object_ = relationship
|
| 80 |
+
self.knowledge_graph.add_edge(subject, object_, relation=predicate)
|
| 81 |
+
|
| 82 |
+
def update_belief_system(self, statement, belief_score):
|
| 83 |
+
self.belief_system[statement] = belief_score
|
| 84 |
+
|
| 85 |
+
def run_metacognitive_layer(self):
|
| 86 |
+
coherence_score = self.calculate_coherence()
|
| 87 |
+
relevance_score = self.calculate_relevance()
|
| 88 |
+
bias_score = self.detect_bias()
|
| 89 |
+
strategy_adjustment = self.suggest_strategy_adjustment()
|
| 90 |
+
|
| 91 |
+
self.metacognitive_layer = {
|
| 92 |
+
"coherence_score": coherence_score,
|
| 93 |
+
"relevance_score": relevance_score,
|
| 94 |
+
"bias_detection": bias_score,
|
| 95 |
+
"strategy_adjustment": strategy_adjustment
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
def calculate_coherence(self):
|
| 99 |
+
return 0.9
|
| 100 |
+
|
| 101 |
+
def calculate_relevance(self):
|
| 102 |
+
return 0.85
|
| 103 |
+
|
| 104 |
+
def detect_bias(self):
|
| 105 |
+
return 0.1
|
| 106 |
+
|
| 107 |
+
def suggest_strategy_adjustment(self):
|
| 108 |
+
return "Focus on providing more concise answers."
|
| 109 |
+
|
| 110 |
def introspect(self):
|
| 111 |
introspection_report = "Introspection Report:\n"
|
| 112 |
introspection_report += f" Current Emotional State (VAD): {self.internal_state['emotions']}\n"
|
|
|
|
| 115 |
introspection_report += " Current Goals:\n"
|
| 116 |
for goal in self.goals:
|
| 117 |
introspection_report += f" - {goal['goal']} (Priority: {goal['priority']:.2f}, Status: {goal['status']})\n"
|
| 118 |
+
introspection_report += "Metacognitive Layer Report\n"
|
| 119 |
+
introspection_report += f"Coherence Score: {self.metacognitive_layer['coherence_score']}\n"
|
| 120 |
+
introspection_report += f"Relevance Score: {self.metacognitive_layer['relevance_score']}\n"
|
| 121 |
+
introspection_report += f"Bias Detection: {self.metacognitive_layer['bias_detection']}\n"
|
| 122 |
+
introspection_report += f"Strategy Adjustment: {self.metacognitive_layer['strategy_adjustment']}\n"
|
| 123 |
return introspection_report
|
| 124 |
|
| 125 |
def adjust_response_based_on_state(self, response):
|
|
|
|
| 200 |
{"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"}
|
| 201 |
]
|
| 202 |
|
| 203 |
+
self.knowledge_graph = nx.DiGraph()
|
| 204 |
+
self.belief_system = {}
|
| 205 |
+
self.metacognitive_layer = {
|
| 206 |
+
"coherence_score": 0.0,
|
| 207 |
+
"relevance_score": 0.0,
|
| 208 |
+
"bias_detection": 0.0,
|
| 209 |
+
"strategy_adjustment": ""
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
try:
|
| 213 |
self.client = InferenceClient(
|
| 214 |
model="Qwen/QwQ-32B-Preview",
|
|
|
|
| 282 |
role="user",
|
| 283 |
content=user_input
|
| 284 |
).to_dict())
|
| 285 |
+
|
| 286 |
+
entities = []
|
| 287 |
+
relationships = []
|
| 288 |
+
|
| 289 |
+
for message in messages:
|
| 290 |
+
if message['role'] == 'user':
|
| 291 |
+
extracted_entities = self.extract_entities(message['content'])
|
| 292 |
+
extracted_relationships = self.extract_relationships(message['content'])
|
| 293 |
+
entities.extend(extracted_entities)
|
| 294 |
+
relationships.extend(extracted_relationships)
|
| 295 |
+
|
| 296 |
+
self.update_knowledge_graph(entities, relationships)
|
| 297 |
+
self.run_metacognitive_layer()
|
| 298 |
|
| 299 |
input_tokens = sum(len(msg['content'].split()) for msg in messages)
|
| 300 |
max_new_tokens = 16384 - input_tokens - 50
|
|
|
|
| 316 |
print(f"Detailed error in get_response: {e}")
|
| 317 |
return f"Error generating response: {str(e)}"
|
| 318 |
|
| 319 |
+
def extract_entities(self, text):
|
| 320 |
+
return []
|
| 321 |
+
|
| 322 |
+
def extract_relationships(self, text):
|
| 323 |
+
return []
|
| 324 |
+
|
| 325 |
def messages_to_prompt(self, messages):
|
| 326 |
prompt = ""
|
| 327 |
for msg in messages:
|