Spaces:
Running
Running
Commit ·
1305c82
1
Parent(s): a3edf9d
low-level experimental epistemic intelligence
Browse files- api_server.py +28 -5
- core/epistemic/epistemic_cross.py +57 -0
- core/epistemic/profile_manager.py +81 -0
- core/epistemic/profile_store.py +25 -0
api_server.py
CHANGED
|
@@ -5,6 +5,11 @@ import requests
|
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from core.conversation_manager import ConversationState
|
| 9 |
from core.conversation_engine import handle_turn
|
| 10 |
|
|
@@ -13,13 +18,11 @@ from core.input_classifier import is_meaningful_input
|
|
| 13 |
from core.memory_store import save_state, load_state # session persistence handling
|
| 14 |
from core.conversation_manager import from_dict # to have the retrieved conversation in DICT/JSON converted to ConversationState Object
|
| 15 |
|
| 16 |
-
from fastapi.responses import HTMLResponse
|
| 17 |
-
from fastapi.staticfiles import StaticFiles
|
| 18 |
-
from fastapi.responses import FileResponse
|
| 19 |
-
|
| 20 |
-
from fastapi import BackgroundTasks
|
| 21 |
from agents.epistemic_agent import epistemic_response
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
# CREATE APP FIRST
|
| 24 |
app = FastAPI()
|
| 25 |
|
|
@@ -225,6 +228,26 @@ def chat(req: ChatRequest, background_tasks: BackgroundTasks):
|
|
| 225 |
# -----------------------------
|
| 226 |
output = handle_turn(state, message)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
# -----------------------------------------
|
| 229 |
# SAVE STATE AFTER EVERY TURN (PERSISTENCE)
|
| 230 |
# -----------------------------------------
|
|
|
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import HTMLResponse
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
+
from fastapi.responses import FileResponse
|
| 11 |
+
from fastapi import BackgroundTasks
|
| 12 |
+
|
| 13 |
from core.conversation_manager import ConversationState
|
| 14 |
from core.conversation_engine import handle_turn
|
| 15 |
|
|
|
|
| 18 |
from core.memory_store import save_state, load_state # session persistence handling
|
| 19 |
from core.conversation_manager import from_dict # to have the retrieved conversation in DICT/JSON converted to ConversationState Object
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
from agents.epistemic_agent import epistemic_response
|
| 22 |
|
| 23 |
+
from core.epistemic.profile_manager import update_global_profile
|
| 24 |
+
from core.epistemic.epistemic_cross import cross_session_analysis
|
| 25 |
+
|
| 26 |
# CREATE APP FIRST
|
| 27 |
app = FastAPI()
|
| 28 |
|
|
|
|
| 228 |
# -----------------------------
|
| 229 |
output = handle_turn(state, message)
|
| 230 |
|
| 231 |
+
|
| 232 |
+
# ---- (EXPERIMENTAL) LOW-LEVEL CROSS-SESSION EPISTEMIC INTELLIGENCE/ANALYSIS ----
|
| 233 |
+
|
| 234 |
+
# -------------------------------
|
| 235 |
+
# UPDATE GLOBAL PROFILE
|
| 236 |
+
# -------------------------------
|
| 237 |
+
state.session_id = session_id # ensure attached
|
| 238 |
+
session_record = update_global_profile(state)
|
| 239 |
+
|
| 240 |
+
# -------------------------------
|
| 241 |
+
# EPISTEMIC CROSS-SESSION
|
| 242 |
+
# -------------------------------
|
| 243 |
+
epistemic_text = cross_session_analysis(session_record)
|
| 244 |
+
|
| 245 |
+
# attach to response
|
| 246 |
+
output["witness_updates"] = {
|
| 247 |
+
"epistemic": epistemic_text
|
| 248 |
+
}
|
| 249 |
+
# ---- (EXPERIMENTAL) LOW-LEVEL CROSS-SESSION EPISTEMIC INTELLIGENCE/ANALYSIS - End ----
|
| 250 |
+
|
| 251 |
# -----------------------------------------
|
| 252 |
# SAVE STATE AFTER EVERY TURN (PERSISTENCE)
|
| 253 |
# -----------------------------------------
|
core/epistemic/epistemic_cross.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# ./core/epistemic/epistemic_cross.py
|
| 3 |
+
|
| 4 |
+
from core.profile_store import load_profile
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def cross_session_analysis(current_session):
|
| 8 |
+
|
| 9 |
+
profile = load_profile()
|
| 10 |
+
|
| 11 |
+
past = profile["sessions"][:-1] # exclude current
|
| 12 |
+
|
| 13 |
+
if not past:
|
| 14 |
+
return "No prior sessions to compare."
|
| 15 |
+
|
| 16 |
+
# -------------------------
|
| 17 |
+
# PATTERN DETECTION
|
| 18 |
+
# -------------------------
|
| 19 |
+
repeated = []
|
| 20 |
+
|
| 21 |
+
for s in past:
|
| 22 |
+
overlap = set(s["themes"]) & set(current_session["themes"])
|
| 23 |
+
if len(overlap) >= 1:
|
| 24 |
+
repeated.extend(list(overlap))
|
| 25 |
+
|
| 26 |
+
repeated = list(set(repeated))
|
| 27 |
+
|
| 28 |
+
# -------------------------
|
| 29 |
+
# DEPTH TREND
|
| 30 |
+
# -------------------------
|
| 31 |
+
prev_depth = past[-1]["depth"]
|
| 32 |
+
curr_depth = current_session["depth"]
|
| 33 |
+
|
| 34 |
+
if curr_depth > prev_depth:
|
| 35 |
+
trend = "Growth detected (increasing depth)"
|
| 36 |
+
elif curr_depth < prev_depth:
|
| 37 |
+
trend = "Regression detected (reduced depth)"
|
| 38 |
+
else:
|
| 39 |
+
trend = "Stable level of engagement"
|
| 40 |
+
|
| 41 |
+
# -------------------------
|
| 42 |
+
# BUILD RESPONSE
|
| 43 |
+
# -------------------------
|
| 44 |
+
response = f"""
|
| 45 |
+
CROSS-SESSION ANALYSIS:
|
| 46 |
+
|
| 47 |
+
PATTERNS:
|
| 48 |
+
{", ".join(repeated) if repeated else "No strong repetition detected"}
|
| 49 |
+
|
| 50 |
+
TRAJECTORY:
|
| 51 |
+
Previous depth: {prev_depth} → Current depth: {curr_depth}
|
| 52 |
+
|
| 53 |
+
EVALUATION:
|
| 54 |
+
{trend}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
return response.strip()
|
core/epistemic/profile_manager.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# ./core/epistemic/profile_manager.py
|
| 3 |
+
|
| 4 |
+
from core.profile_store import load_profile, save_profile
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# -------------------------------
|
| 8 |
+
# BASIC THEME EXTRACTION (LIGHT)
|
| 9 |
+
# -------------------------------
|
| 10 |
+
def extract_themes(text):
|
| 11 |
+
|
| 12 |
+
keywords = {
|
| 13 |
+
"self-doubt": ["doubt", "not sure", "uncertain"],
|
| 14 |
+
"identity": ["who am i", "identity", "self"],
|
| 15 |
+
"fear": ["afraid", "fear", "anxiety"],
|
| 16 |
+
"avoidance": ["avoid", "escape", "ignore"],
|
| 17 |
+
"meaning": ["meaning", "purpose"],
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
detected = set()
|
| 21 |
+
|
| 22 |
+
text = text.lower()
|
| 23 |
+
|
| 24 |
+
for theme, words in keywords.items():
|
| 25 |
+
for w in words:
|
| 26 |
+
if w in text:
|
| 27 |
+
detected.add(theme)
|
| 28 |
+
|
| 29 |
+
return list(detected)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# -------------------------------
|
| 33 |
+
# DEPTH SCORING
|
| 34 |
+
# -------------------------------
|
| 35 |
+
def score_depth(text):
|
| 36 |
+
|
| 37 |
+
text = text.lower()
|
| 38 |
+
|
| 39 |
+
if "i feel" in text or "i am" in text:
|
| 40 |
+
return 4
|
| 41 |
+
elif "why" in text:
|
| 42 |
+
return 3
|
| 43 |
+
elif "what is" in text:
|
| 44 |
+
return 1
|
| 45 |
+
else:
|
| 46 |
+
return 2
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# -------------------------------
|
| 50 |
+
# MAIN FUNCTION
|
| 51 |
+
# -------------------------------
|
| 52 |
+
def update_global_profile(state):
|
| 53 |
+
|
| 54 |
+
profile = load_profile()
|
| 55 |
+
|
| 56 |
+
# ---- collect client text ----
|
| 57 |
+
client_msgs = [
|
| 58 |
+
m["content"]
|
| 59 |
+
for m in state.history
|
| 60 |
+
if m["role"] == "client"
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
full_text = " ".join(client_msgs)
|
| 64 |
+
|
| 65 |
+
themes = extract_themes(full_text)
|
| 66 |
+
depth = score_depth(full_text)
|
| 67 |
+
|
| 68 |
+
session_record = {
|
| 69 |
+
"session_id": state.session_id,
|
| 70 |
+
"themes": themes,
|
| 71 |
+
"depth": depth
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
profile["sessions"].append(session_record)
|
| 75 |
+
|
| 76 |
+
# merge global themes
|
| 77 |
+
profile["themes"] = list(set(profile["themes"] + themes))
|
| 78 |
+
|
| 79 |
+
save_profile(profile)
|
| 80 |
+
|
| 81 |
+
return session_record
|
core/epistemic/profile_store.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# ./core/epistemic/profile_store.py
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
PROFILE_PATH = "/data/profile.json"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def load_profile():
|
| 11 |
+
if not os.path.exists(PROFILE_PATH):
|
| 12 |
+
return {
|
| 13 |
+
"sessions": [],
|
| 14 |
+
"themes": []
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
with open(PROFILE_PATH, "r") as f:
|
| 18 |
+
return json.load(f)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def save_profile(profile):
|
| 22 |
+
os.makedirs(os.path.dirname(PROFILE_PATH), exist_ok=True)
|
| 23 |
+
|
| 24 |
+
with open(PROFILE_PATH, "w") as f:
|
| 25 |
+
json.dump(profile, f, indent=2)
|