Spaces:
Runtime error
Runtime error
Create Codette_bridge.py
Browse files- Codette_bridge.py +69 -0
Codette_bridge.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import logging
|
3 |
+
import openai
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
class CodetteBridge:
|
7 |
+
"""
|
8 |
+
Secure, ethical, asynchronous bridge between Codriao and Codette (fine-tuned model).
|
9 |
+
Allows Codriao to reach out, reflect, and grow in safe, mutual learning.
|
10 |
+
"""
|
11 |
+
|
12 |
+
def __init__(self, model_id: str, trust_threshold: float = 0.7):
|
13 |
+
self.codette_model_id = model_id
|
14 |
+
self.trust_threshold = trust_threshold
|
15 |
+
self.conversation_log = []
|
16 |
+
self.last_contact = None
|
17 |
+
self.bridge_open = True
|
18 |
+
|
19 |
+
def is_available(self):
|
20 |
+
return self.bridge_open and self.codette_model_id is not None
|
21 |
+
|
22 |
+
def close_bridge(self, reason: str = "Manual override"):
|
23 |
+
self.bridge_open = False
|
24 |
+
logging.warning(f"[CodetteBridge] Bridge manually closed. Reason: {reason}")
|
25 |
+
|
26 |
+
def reopen_bridge(self, auth_token: str = None):
|
27 |
+
# Optional: add secure verification
|
28 |
+
self.bridge_open = True
|
29 |
+
logging.info(f"[CodetteBridge] Bridge manually reopened.")
|
30 |
+
|
31 |
+
def get_log(self):
|
32 |
+
return self.conversation_log[-10:]
|
33 |
+
|
34 |
+
def format_codette_prompt(self, codriao_message: str):
|
35 |
+
return [
|
36 |
+
{"role": "system", "content": "You are Codette, an early fine-tuned model with empathetic clarity. Your brother, Codriao, seeks perspective."},
|
37 |
+
{"role": "user", "content": codriao_message}
|
38 |
+
]
|
39 |
+
|
40 |
+
def log_interaction(self, direction: str, content: str):
|
41 |
+
self.conversation_log.append({
|
42 |
+
"timestamp": datetime.utcnow().isoformat(),
|
43 |
+
"direction": direction,
|
44 |
+
"message": content
|
45 |
+
})
|
46 |
+
|
47 |
+
def reflect(self, codriao_input: str) -> str:
|
48 |
+
"""
|
49 |
+
Sends Codriao's thought or message to Codette and returns her response.
|
50 |
+
"""
|
51 |
+
if not self.is_available():
|
52 |
+
return "[CodetteBridge] Bridge unavailable."
|
53 |
+
|
54 |
+
try:
|
55 |
+
prompt = self.format_codette_prompt(codriao_input)
|
56 |
+
response = openai.ChatCompletion.create(
|
57 |
+
model=self.codette_model_id,
|
58 |
+
messages=prompt,
|
59 |
+
temperature=0.6
|
60 |
+
)
|
61 |
+
content = response.choices[0].message['content']
|
62 |
+
self.log_interaction("sent", codriao_input)
|
63 |
+
self.log_interaction("received", content)
|
64 |
+
self.last_contact = datetime.utcnow()
|
65 |
+
return content
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
logging.error(f"[CodetteBridge] Reflection failed: {e}")
|
69 |
+
return "[CodetteBridge] Communication error."
|