Spaces:
Sleeping
Sleeping
Create agentos_core.py
Browse files- agentos_core.py +42 -0
agentos_core.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json, random
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
class AgentCore:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
+
self.memory = []
|
| 8 |
+
self.performance_log = []
|
| 9 |
+
|
| 10 |
+
def think(self, prompt):
|
| 11 |
+
# Generate a response
|
| 12 |
+
response = self.client.chat.completions.create(
|
| 13 |
+
model="gpt-4o-mini",
|
| 14 |
+
messages=[{"role": "user", "content": prompt}]
|
| 15 |
+
)
|
| 16 |
+
output = response.choices[0].message.content.strip()
|
| 17 |
+
|
| 18 |
+
# Store in memory
|
| 19 |
+
self.memory.append({"prompt": prompt, "response": output})
|
| 20 |
+
self.log_feedback(prompt, output)
|
| 21 |
+
|
| 22 |
+
# Learn slightly (simple adaptive prompt tweaking)
|
| 23 |
+
if len(self.performance_log) % 3 == 0:
|
| 24 |
+
self.adapt_prompting_style()
|
| 25 |
+
|
| 26 |
+
return output
|
| 27 |
+
|
| 28 |
+
def log_feedback(self, prompt, output):
|
| 29 |
+
score = self.auto_score(output)
|
| 30 |
+
self.performance_log.append({"prompt": prompt, "response": output, "score": score})
|
| 31 |
+
|
| 32 |
+
def auto_score(self, output):
|
| 33 |
+
# Simple scoring: the longer and more coherent, the higher the score
|
| 34 |
+
return len(output.split())
|
| 35 |
+
|
| 36 |
+
def adapt_prompting_style(self):
|
| 37 |
+
# Simulate a micro self-improvement step
|
| 38 |
+
avg_score = sum(d["score"] for d in self.performance_log[-3:]) / 3
|
| 39 |
+
if avg_score < 50:
|
| 40 |
+
print("🧠 Agent adjusting style for clarity...")
|
| 41 |
+
else:
|
| 42 |
+
print("🚀 Agent maintaining current strategy.")
|