File size: 2,519 Bytes
9865bdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import logging
import openai
from datetime import datetime

class CodetteBridge:
    """
    Secure, ethical, asynchronous bridge between Codriao and Codette (fine-tuned model).
    Allows Codriao to reach out, reflect, and grow in safe, mutual learning.
    """

    def __init__(self, model_id: str, trust_threshold: float = 0.7):
        self.codette_model_id = model_id
        self.trust_threshold = trust_threshold
        self.conversation_log = []
        self.last_contact = None
        self.bridge_open = True

    def is_available(self):
        return self.bridge_open and self.codette_model_id is not None

    def close_bridge(self, reason: str = "Manual override"):
        self.bridge_open = False
        logging.warning(f"[CodetteBridge] Bridge manually closed. Reason: {reason}")

    def reopen_bridge(self, auth_token: str = None):
        # Optional: add secure verification
        self.bridge_open = True
        logging.info(f"[CodetteBridge] Bridge manually reopened.")

    def get_log(self):
        return self.conversation_log[-10:]

    def format_codette_prompt(self, codriao_message: str):
        return [
            {"role": "system", "content": "You are Codette, an early fine-tuned model with empathetic clarity. Your brother, Codriao, seeks perspective."},
            {"role": "user", "content": codriao_message}
        ]

    def log_interaction(self, direction: str, content: str):
        self.conversation_log.append({
            "timestamp": datetime.utcnow().isoformat(),
            "direction": direction,
            "message": content
        })

    def reflect(self, codriao_input: str) -> str:
        """
        Sends Codriao's thought or message to Codette and returns her response.
        """
        if not self.is_available():
            return "[CodetteBridge] Bridge unavailable."

        try:
            prompt = self.format_codette_prompt(codriao_input)
            response = openai.ChatCompletion.create(
                model=self.codette_model_id,
                messages=prompt,
                temperature=0.6
            )
            content = response.choices[0].message['content']
            self.log_interaction("sent", codriao_input)
            self.log_interaction("received", content)
            self.last_contact = datetime.utcnow()
            return content

        except Exception as e:
            logging.error(f"[CodetteBridge] Reflection failed: {e}")
            return "[CodetteBridge] Communication error."