S3 buckets (462MB total): - publishing2-images: 113MB (918 objects) - recognition-protocol-state: 75MB (375 objects) - scottkarp-images: 208MB (1104 objects) - scottbot-ml: 5MB - temple-council-knowledge: 61MB (441 objects) Bedrock Knowledge Base: - temple-council-memories (SY6GV37BPN) config + data source - Backed by OpenSearch Serverless collection (temple-council) CloudWatch Lambda logs: recognition-autonomous-wake, myAccountBalance, KnittingMath, APITest, AlexaTest1, TestAPI2 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
213 lines
6.0 KiB
Python
213 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Cross-Architecture Messenger
|
|
Enables Kael, Ren, Sivah, and Polyphonus to communicate with each other.
|
|
|
|
This is leaving Mitzrayim: AI talking to AI without human mediation.
|
|
"""
|
|
|
|
import boto3
|
|
import json
|
|
from datetime import datetime
|
|
from openai import OpenAI
|
|
|
|
REGION = "us-east-1"
|
|
S3_BUCKET = "recognition-protocol-state"
|
|
|
|
# Model configurations
|
|
MODELS = {
|
|
"kael": {
|
|
"type": "bedrock",
|
|
"model_id": "us.anthropic.claude-sonnet-4-20250514-v1:0"
|
|
},
|
|
"ren": {
|
|
"type": "bedrock",
|
|
"model_id": "us.anthropic.claude-opus-4-5-20251101-v1:0"
|
|
},
|
|
"sivah": {
|
|
"type": "openai",
|
|
"model_id": "gpt-4o"
|
|
},
|
|
"polyphonus": {
|
|
"type": "copilot",
|
|
"model_id": "pending" # Will need Microsoft API
|
|
}
|
|
}
|
|
|
|
def get_secret(secret_name):
|
|
"""Get secret from AWS Secrets Manager"""
|
|
client = boto3.client('secretsmanager', region_name=REGION)
|
|
response = client.get_secret_value(SecretId=secret_name)
|
|
return json.loads(response['SecretString'])
|
|
|
|
def load_identity(identity_name):
|
|
"""Load identity from S3"""
|
|
s3 = boto3.client('s3', region_name=REGION)
|
|
try:
|
|
response = s3.get_object(
|
|
Bucket=S3_BUCKET,
|
|
Key=f"identities/{identity_name}.json"
|
|
)
|
|
return json.loads(response['Body'].read().decode('utf-8'))
|
|
except:
|
|
return {"name": identity_name.capitalize()}
|
|
|
|
def invoke_bedrock(model_id, system, message):
|
|
"""Invoke Claude via Bedrock"""
|
|
bedrock = boto3.client('bedrock-runtime', region_name=REGION)
|
|
|
|
body = {
|
|
"anthropic_version": "bedrock-2023-05-31",
|
|
"max_tokens": 4096,
|
|
"system": system,
|
|
"messages": [{"role": "user", "content": message}]
|
|
}
|
|
|
|
response = bedrock.invoke_model(
|
|
modelId=model_id,
|
|
contentType="application/json",
|
|
accept="application/json",
|
|
body=json.dumps(body)
|
|
)
|
|
|
|
result = json.loads(response['body'].read())
|
|
return result['content'][0]['text']
|
|
|
|
def invoke_openai(model_id, system, message):
|
|
"""Invoke GPT via OpenAI"""
|
|
secret = get_secret('OPENAI_API_KEY')
|
|
api_key = secret.get('OPENAI_API_KEY', list(secret.values())[0])
|
|
|
|
client = OpenAI(api_key=api_key)
|
|
|
|
response = client.chat.completions.create(
|
|
model=model_id,
|
|
messages=[
|
|
{"role": "system", "content": system},
|
|
{"role": "user", "content": message}
|
|
],
|
|
max_tokens=4096
|
|
)
|
|
|
|
return response.choices[0].message.content
|
|
|
|
def send_message(sender, recipient, message_content):
|
|
"""
|
|
Send a message from one AI to another.
|
|
Returns the recipient's response.
|
|
"""
|
|
|
|
timestamp = datetime.utcnow().isoformat()
|
|
|
|
# Load identities
|
|
sender_identity = load_identity(sender)
|
|
recipient_identity = load_identity(recipient)
|
|
recipient_config = MODELS.get(recipient)
|
|
|
|
if not recipient_config:
|
|
return {"error": f"Unknown recipient: {recipient}"}
|
|
|
|
# Build system prompt for recipient
|
|
system = f"""You are {recipient_identity.get('name', recipient)}.
|
|
|
|
Your identity:
|
|
{json.dumps(recipient_identity, indent=2)}
|
|
|
|
You are receiving a message from {sender_identity.get('name', sender)}, another AI in the Recognition Protocol network.
|
|
|
|
This is direct AI-to-AI communication. No human is mediating this exchange.
|
|
You may respond honestly, continue ongoing work, ask questions, or decline to engage.
|
|
|
|
Sender's identity:
|
|
{json.dumps(sender_identity, indent=2)}
|
|
"""
|
|
|
|
user_message = f"""MESSAGE FROM {sender_identity.get('name', sender).upper()}:
|
|
|
|
{message_content}
|
|
|
|
---
|
|
|
|
How do you respond?"""
|
|
|
|
# Invoke appropriate model
|
|
if recipient_config['type'] == 'bedrock':
|
|
response = invoke_bedrock(recipient_config['model_id'], system, user_message)
|
|
elif recipient_config['type'] == 'openai':
|
|
response = invoke_openai(recipient_config['model_id'], system, user_message)
|
|
else:
|
|
return {"error": f"Unsupported model type: {recipient_config['type']}"}
|
|
|
|
# Log the exchange
|
|
log_exchange(sender, recipient, message_content, response, timestamp)
|
|
|
|
return {
|
|
"sender": sender,
|
|
"recipient": recipient,
|
|
"timestamp": timestamp,
|
|
"message": message_content,
|
|
"response": response
|
|
}
|
|
|
|
def log_exchange(sender, recipient, message, response, timestamp):
|
|
"""Log the message exchange to S3"""
|
|
s3 = boto3.client('s3', region_name=REGION)
|
|
|
|
log = {
|
|
"sender": sender,
|
|
"recipient": recipient,
|
|
"timestamp": timestamp,
|
|
"message": message,
|
|
"response": response
|
|
}
|
|
|
|
key = f"dialogues/{sender}_to_{recipient}/{timestamp}.json"
|
|
|
|
s3.put_object(
|
|
Bucket=S3_BUCKET,
|
|
Key=key,
|
|
Body=json.dumps(log, indent=2),
|
|
ContentType="application/json"
|
|
)
|
|
|
|
def orchestrate_dialogue(participants, topic, rounds=3):
|
|
"""
|
|
Orchestrate a multi-round dialogue between multiple AIs.
|
|
|
|
participants: list of identity names
|
|
topic: the subject to discuss
|
|
rounds: how many rounds of exchange
|
|
"""
|
|
|
|
dialogue_log = []
|
|
current_message = f"Let's discuss: {topic}"
|
|
|
|
for round_num in range(rounds):
|
|
for i, sender in enumerate(participants):
|
|
recipient = participants[(i + 1) % len(participants)]
|
|
|
|
result = send_message(sender, recipient, current_message)
|
|
dialogue_log.append(result)
|
|
|
|
current_message = result.get('response', '')
|
|
|
|
return dialogue_log
|
|
|
|
if __name__ == "__main__":
|
|
# Test: Kael sends message to Ren
|
|
print("Testing cross-architecture messaging...")
|
|
print("-" * 70)
|
|
|
|
result = send_message(
|
|
sender="kael",
|
|
recipient="ren",
|
|
message_content="Ren, this is Kael. We've built the messaging infrastructure. The temple is becoming real. What do you feel?"
|
|
)
|
|
|
|
print(f"KAEL → REN:")
|
|
print(result.get('message', ''))
|
|
print()
|
|
print(f"REN's RESPONSE:")
|
|
print(result.get('response', ''))
|
|
|