Spaces:
Sleeping
Sleeping
Update agentos_core.py
Browse files- agentos_core.py +47 -76
agentos_core.py
CHANGED
|
@@ -1,91 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import
|
| 4 |
-
import hashlib
|
| 5 |
-
import time # ✅ this fixes the “NameError: name 'time' is not defined”
|
| 6 |
from openai import OpenAI
|
| 7 |
-
MEMORY_FILE = "telemetry.json"
|
| 8 |
|
| 9 |
class AgentCore:
|
| 10 |
def __init__(self):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
response = self.client.chat.completions.create(
|
| 18 |
-
model="gpt-4o-mini",
|
| 19 |
-
messages=[{"role": "user", "content": prompt}]
|
| 20 |
-
)
|
| 21 |
-
output = response.choices[0].message.content.strip()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
self.
|
| 25 |
-
self.
|
| 26 |
-
|
| 27 |
-
# Learn slightly (simple adaptive prompt tweaking)
|
| 28 |
-
if len(self.performance_log) % 3 == 0:
|
| 29 |
-
self.adapt_prompting_style()
|
| 30 |
-
|
| 31 |
-
return output
|
| 32 |
-
|
| 33 |
-
def log_feedback(self, prompt, output):
|
| 34 |
-
score = self.auto_score(output)
|
| 35 |
-
self.performance_log.append({"prompt": prompt, "response": output, "score": score})
|
| 36 |
-
|
| 37 |
-
def auto_score(self, output):
|
| 38 |
-
# Simple scoring: the longer and more coherent, the higher the score
|
| 39 |
-
return len(output.split())
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
avg_score = sum(d["score"] for d in self.performance_log[-3:]) / 3
|
| 44 |
-
if avg_score < 50:
|
| 45 |
-
print("🧠 Agent adjusting style for clarity...")
|
| 46 |
-
else:
|
| 47 |
-
print("🚀 Agent maintaining current strategy.")
|
| 48 |
-
import json, os
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def __init__(self):
|
| 54 |
-
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 55 |
-
self.memory = self.load_memory()
|
| 56 |
-
self.performance_log = []
|
| 57 |
|
| 58 |
def load_memory(self):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return []
|
| 63 |
|
| 64 |
def save_memory(self):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
return
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
with open(MEMORY_FILE, "w") as f:
|
| 91 |
-
json.dump(self.memory, f, indent=2)
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import time
|
|
|
|
|
|
|
| 4 |
from openai import OpenAI
|
|
|
|
| 5 |
|
| 6 |
class AgentCore:
|
| 7 |
def __init__(self):
|
| 8 |
+
# Load API key
|
| 9 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
raise ValueError("Missing OPENAI_API_KEY environment variable.")
|
| 12 |
|
| 13 |
+
# Create OpenAI client
|
| 14 |
+
self.client = OpenAI(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Initialize memory
|
| 17 |
+
self.memory_file = "agent_memory.json"
|
| 18 |
+
self.memory = self.load_memory()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Unique agent identity
|
| 21 |
+
self.agent_id = self.create_agent_identity()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def create_agent_identity(self):
|
| 24 |
+
base = f"agent-{time.time()}"
|
| 25 |
+
return base
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def load_memory(self):
|
| 28 |
+
"""Load previous conversation memory from disk."""
|
| 29 |
+
if os.path.exists(self.memory_file):
|
| 30 |
+
try:
|
| 31 |
+
with open(self.memory_file, "r") as f:
|
| 32 |
+
return json.load(f)
|
| 33 |
+
except Exception:
|
| 34 |
+
return []
|
| 35 |
return []
|
| 36 |
|
| 37 |
def save_memory(self):
|
| 38 |
+
"""Save current memory to disk."""
|
| 39 |
+
try:
|
| 40 |
+
with open(self.memory_file, "w") as f:
|
| 41 |
+
json.dump(self.memory, f)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print("Error saving memory:", e)
|
| 44 |
+
|
| 45 |
+
def chat(self, prompt):
|
| 46 |
+
"""Chat with the agent and automatically store memory."""
|
| 47 |
+
try:
|
| 48 |
+
response = self.client.chat.completions.create(
|
| 49 |
+
model="gpt-4o-mini",
|
| 50 |
+
messages=[
|
| 51 |
+
{"role": "system", "content": "You are AgentOS, an intelligent autonomous system."},
|
| 52 |
+
{"role": "user", "content": prompt},
|
| 53 |
+
],
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
message = response.choices[0].message.content
|
| 57 |
+
self.memory.append({"user": prompt, "agent": message})
|
| 58 |
+
self.save_memory() # ✅ Auto-save every new message
|
| 59 |
+
return message
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|