Spaces:
Running
Running
Commit ·
5d342ad
1
Parent(s): e0ade9c
done some changes
Browse files- client.py +36 -19
- inference.py +277 -96
- server/app.py +16 -47
client.py
CHANGED
|
@@ -1,32 +1,49 @@
|
|
| 1 |
"""
|
| 2 |
Second Brain Environment — WebSocket Client
|
| 3 |
Connects to the running server and exposes reset()/step()/state().
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
from openenv.core.env_client import EnvClient
|
| 6 |
-
from models import SecondBrainAction, SecondBrainObservation
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class SecondBrainEnv(EnvClient):
|
| 10 |
-
"""
|
| 11 |
-
Client for the Second Brain OpenEnv environment.
|
| 12 |
-
|
| 13 |
-
Usage:
|
| 14 |
-
async with SecondBrainEnv(base_url="http://localhost:8000") as env:
|
| 15 |
-
result = await env.reset()
|
| 16 |
-
result = await env.step(SecondBrainAction(
|
| 17 |
-
action_type="categorize",
|
| 18 |
-
content="work"
|
| 19 |
-
))
|
| 20 |
-
|
| 21 |
-
Or from a deployed HF Space:
|
| 22 |
-
async with SecondBrainEnv(base_url="https://RAc1928-second-brain-env.hf.space") as env:
|
| 23 |
-
...
|
| 24 |
-
"""
|
| 25 |
-
|
| 26 |
action_type = SecondBrainAction
|
| 27 |
observation_type = SecondBrainObservation
|
| 28 |
-
state_type = SecondBrainState
|
| 29 |
|
| 30 |
def __init__(self, base_url: str = "http://localhost:8000", task_name: str = "note_categorization"):
|
| 31 |
super().__init__(base_url=base_url)
|
| 32 |
-
self._task_name = task_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Second Brain Environment — WebSocket Client
|
| 3 |
Connects to the running server and exposes reset()/step()/state().
|
| 4 |
+
|
| 5 |
+
FIX: The openenv HTTP layer deserializes the observation using the Pydantic
|
| 6 |
+
schema, but `reward` has a default of 0.0 — so if the server response omits
|
| 7 |
+
the field (or it gets stripped by the base class serializer) it silently
|
| 8 |
+
resets to 0. The fix: read `reward` from the raw response dict BEFORE
|
| 9 |
+
constructing the typed observation, then attach it as `result.reward` so
|
| 10 |
+
inference.py can read it independently of the observation object.
|
| 11 |
"""
|
| 12 |
from openenv.core.env_client import EnvClient
|
| 13 |
+
from models import SecondBrainAction, SecondBrainObservation, SecondBrainState
|
| 14 |
+
from types import SimpleNamespace
|
| 15 |
|
| 16 |
|
| 17 |
class SecondBrainEnv(EnvClient):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
action_type = SecondBrainAction
|
| 19 |
observation_type = SecondBrainObservation
|
| 20 |
+
state_type = SecondBrainState
|
| 21 |
|
| 22 |
def __init__(self, base_url: str = "http://localhost:8000", task_name: str = "note_categorization"):
|
| 23 |
super().__init__(base_url=base_url)
|
| 24 |
+
self._task_name = task_name
|
| 25 |
+
|
| 26 |
+
def _step_payload(self, action: SecondBrainAction) -> dict:
|
| 27 |
+
return action.model_dump()
|
| 28 |
+
|
| 29 |
+
def _parse_result(self, data: dict):
|
| 30 |
+
obs_data = data.get("observation", {})
|
| 31 |
+
|
| 32 |
+
# Pull reward out of the raw dict BEFORE Pydantic touches it.
|
| 33 |
+
# The base EnvClient may not forward all fields — this is the only
|
| 34 |
+
# reliable place to read it.
|
| 35 |
+
reward = float(obs_data.get("reward", 0.0))
|
| 36 |
+
|
| 37 |
+
obs = self.observation_type(**obs_data)
|
| 38 |
+
done = data.get("done", False)
|
| 39 |
+
|
| 40 |
+
# Attach reward at the top level of the result so inference.py can
|
| 41 |
+
# read result.reward without depending on obs.reward surviving intact.
|
| 42 |
+
return SimpleNamespace(
|
| 43 |
+
observation=obs,
|
| 44 |
+
done=done,
|
| 45 |
+
reward=reward, # ← top-level, always correct
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def _parse_state(self, data: dict):
|
| 49 |
+
return self.state_type(**data)
|
inference.py
CHANGED
|
@@ -1,40 +1,57 @@
|
|
| 1 |
"""
|
| 2 |
-
inference.py — Second Brain OpenEnv Baseline Inference Script
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
|
|
|
|
| 14 |
import asyncio
|
| 15 |
import json
|
| 16 |
import os
|
|
|
|
|
|
|
| 17 |
import textwrap
|
| 18 |
from typing import List, Optional
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
from openai import OpenAI
|
| 21 |
from client import SecondBrainEnv
|
| 22 |
-
from models import SecondBrainAction
|
| 23 |
-
from models import ActionType
|
| 24 |
|
| 25 |
# ---------------------------------------------------------------------------
|
| 26 |
# Configuration
|
| 27 |
# ---------------------------------------------------------------------------
|
| 28 |
-
API_KEY = os.getenv("
|
| 29 |
-
API_BASE_URL = os.getenv("API_BASE_URL", "https://
|
| 30 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "
|
| 31 |
-
ENV_URL = os.getenv("SECOND_BRAIN_URL", "http://localhost:8000")
|
| 32 |
BENCHMARK = "second_brain_env"
|
| 33 |
MAX_STEPS = 20
|
| 34 |
-
TEMPERATURE = 0.
|
| 35 |
-
MAX_TOKENS =
|
| 36 |
SUCCESS_THRESHOLD = 0.5
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
TASKS = [
|
| 39 |
"note_categorization",
|
| 40 |
"memory_retrieval",
|
|
@@ -42,17 +59,15 @@ TASKS = [
|
|
| 42 |
]
|
| 43 |
|
| 44 |
# ---------------------------------------------------------------------------
|
| 45 |
-
# Logging helpers
|
| 46 |
# ---------------------------------------------------------------------------
|
| 47 |
|
| 48 |
def log_start(task: str, env: str, model: str) -> None:
|
| 49 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 50 |
|
| 51 |
-
|
| 52 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 53 |
-
error_val
|
| 54 |
-
done_val
|
| 55 |
-
# Sanitize action string — no newlines
|
| 56 |
action_clean = action.replace("\n", " ").replace("\r", "")[:120]
|
| 57 |
print(
|
| 58 |
f"[STEP] step={step} action={action_clean} reward={reward:.2f} "
|
|
@@ -60,7 +75,6 @@ def log_step(step: int, action: str, reward: float, done: bool, error: Optional[
|
|
| 60 |
flush=True,
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
| 65 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 66 |
print(
|
|
@@ -70,44 +84,123 @@ def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> No
|
|
| 70 |
)
|
| 71 |
|
| 72 |
# ---------------------------------------------------------------------------
|
| 73 |
-
# System prompts
|
| 74 |
# ---------------------------------------------------------------------------
|
| 75 |
|
| 76 |
SYSTEM_PROMPTS = {
|
|
|
|
|
|
|
|
|
|
| 77 |
"note_categorization": textwrap.dedent("""
|
| 78 |
-
You are a
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
""").strip(),
|
| 88 |
|
|
|
|
| 89 |
"memory_retrieval": textwrap.dedent("""
|
| 90 |
-
You are a
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
{"action_type": "retrieve", "content": "
|
| 95 |
""").strip(),
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
"knowledge_synthesis": textwrap.dedent("""
|
| 98 |
-
You are a
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# ---------------------------------------------------------------------------
|
| 110 |
-
# Agent
|
| 111 |
# ---------------------------------------------------------------------------
|
| 112 |
|
| 113 |
def get_agent_action(
|
|
@@ -116,33 +209,58 @@ def get_agent_action(
|
|
| 116 |
obs: dict,
|
| 117 |
step: int,
|
| 118 |
history: List[str],
|
|
|
|
| 119 |
) -> SecondBrainAction:
|
| 120 |
-
"""Call the LLM to get the next action."""
|
| 121 |
|
| 122 |
-
# Build context from observation
|
| 123 |
context_parts = [f"Step: {step}"]
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
context_parts.append(f
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
user_prompt = "\n".join(context_parts)
|
| 148 |
|
|
@@ -158,16 +276,18 @@ def get_agent_action(
|
|
| 158 |
)
|
| 159 |
text = (completion.choices[0].message.content or "").strip()
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
# Strip markdown fences if present
|
| 163 |
if "```" in text:
|
| 164 |
-
|
|
|
|
| 165 |
if text.startswith("json"):
|
| 166 |
text = text[4:]
|
| 167 |
text = text.strip()
|
|
|
|
|
|
|
| 168 |
|
| 169 |
data = json.loads(text)
|
| 170 |
-
|
| 171 |
action_type=ActionType(data.get("action_type", "skip")),
|
| 172 |
content=data.get("content", ""),
|
| 173 |
note_id=data.get("note_id"),
|
|
@@ -176,22 +296,55 @@ def get_agent_action(
|
|
| 176 |
|
| 177 |
except Exception as exc:
|
| 178 |
print(f"[DEBUG] LLM parse error: {exc}", flush=True)
|
| 179 |
-
#
|
| 180 |
fallbacks = {
|
| 181 |
-
"note_categorization": SecondBrainAction(
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
}
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
# ---------------------------------------------------------------------------
|
| 188 |
-
# Run one task
|
| 189 |
# ---------------------------------------------------------------------------
|
| 190 |
|
| 191 |
async def run_task(client: OpenAI, task_name: str) -> float:
|
| 192 |
-
"""Run a single task episode. Returns final score in [0, 1]."""
|
| 193 |
|
| 194 |
-
|
|
|
|
| 195 |
|
| 196 |
history: List[str] = []
|
| 197 |
rewards: List[float] = []
|
|
@@ -205,21 +358,53 @@ async def run_task(client: OpenAI, task_name: str) -> float:
|
|
| 205 |
result = await env.reset()
|
| 206 |
obs = result.observation.model_dump() if hasattr(result.observation, "model_dump") else dict(result.observation)
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
for step in range(1, MAX_STEPS + 1):
|
| 209 |
if result.done:
|
| 210 |
break
|
| 211 |
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
|
| 215 |
-
action_str = f"{action.action_type}({action.content[:
|
| 216 |
|
| 217 |
try:
|
| 218 |
-
result
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
reward = 0.0
|
| 225 |
done = True
|
|
@@ -227,17 +412,14 @@ async def run_task(client: OpenAI, task_name: str) -> float:
|
|
| 227 |
|
| 228 |
rewards.append(reward)
|
| 229 |
steps_taken = step
|
| 230 |
-
|
| 231 |
log_step(step=step, action=action_str, reward=reward, done=done, error=error)
|
| 232 |
-
|
| 233 |
history.append(f"Step {step}: {action_str} → reward {reward:+.2f}")
|
| 234 |
|
| 235 |
if done:
|
| 236 |
break
|
| 237 |
|
| 238 |
-
|
| 239 |
-
score
|
| 240 |
-
score = max(0.0, min(1.0, score))
|
| 241 |
success = score >= SUCCESS_THRESHOLD
|
| 242 |
|
| 243 |
except Exception as e:
|
|
@@ -252,7 +434,7 @@ async def run_task(client: OpenAI, task_name: str) -> float:
|
|
| 252 |
return score
|
| 253 |
|
| 254 |
# ---------------------------------------------------------------------------
|
| 255 |
-
# Main
|
| 256 |
# ---------------------------------------------------------------------------
|
| 257 |
|
| 258 |
async def main() -> None:
|
|
@@ -266,7 +448,6 @@ async def main() -> None:
|
|
| 266 |
score = await run_task(client, task_name)
|
| 267 |
all_scores[task_name] = score
|
| 268 |
|
| 269 |
-
# Summary
|
| 270 |
print(f"\n{'='*60}", flush=True)
|
| 271 |
print("BASELINE SCORES SUMMARY", flush=True)
|
| 272 |
print(f"{'='*60}", flush=True)
|
|
|
|
| 1 |
"""
|
| 2 |
+
inference.py — Second Brain OpenEnv Baseline Inference Script (FIXED)
|
| 3 |
+
|
| 4 |
+
Fixes applied:
|
| 5 |
+
1. note_categorization — Step 1 was echoing the note text instead of a category.
|
| 6 |
+
New prompt makes the model output ONLY the category word.
|
| 7 |
+
2. knowledge_synthesis — Retrieval queries used generic "self-care/patterns" words
|
| 8 |
+
that don't appear in any note. New prompt tells the model
|
| 9 |
+
to use CONCRETE words that actually exist in the notes.
|
| 10 |
+
3. knowledge_synthesis — Synthesis answers were too short and missed required theme
|
| 11 |
+
words. force_synthesize prompt now lists exact words that
|
| 12 |
+
must appear in the answer.
|
| 13 |
+
4. reward logging — reward was read from obs AFTER it moved to the next item,
|
| 14 |
+
so it always showed 0.0. Now read from result directly.
|
| 15 |
+
5. retrieval debugging — added [RETRIEVED] log lines so you can see what notes
|
| 16 |
+
the env actually returns (makes tuning queries much easier).
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
import re
|
| 20 |
import asyncio
|
| 21 |
import json
|
| 22 |
import os
|
| 23 |
+
import sys
|
| 24 |
+
|
| 25 |
import textwrap
|
| 26 |
from typing import List, Optional
|
| 27 |
+
venv_python = os.path.join(os.path.dirname(__file__), ".venv", "Scripts", "python.exe")
|
| 28 |
|
| 29 |
+
if os.path.exists(venv_python) and sys.executable != venv_python:
|
| 30 |
+
os.execv(venv_python, [venv_python] + sys.argv)
|
| 31 |
+
from dotenv import load_dotenv
|
| 32 |
+
load_dotenv()
|
| 33 |
from openai import OpenAI
|
| 34 |
from client import SecondBrainEnv
|
| 35 |
+
from models import SecondBrainAction, ActionType
|
|
|
|
| 36 |
|
| 37 |
# ---------------------------------------------------------------------------
|
| 38 |
# Configuration
|
| 39 |
# ---------------------------------------------------------------------------
|
| 40 |
+
API_KEY = os.getenv("GROQ_API_KEY") or os.getenv("HF_TOKEN") or ""
|
| 41 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.groq.com/openai/v1")
|
| 42 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "llama-3.1-8b-instant")
|
|
|
|
| 43 |
BENCHMARK = "second_brain_env"
|
| 44 |
MAX_STEPS = 20
|
| 45 |
+
TEMPERATURE = 0.2 # lower = more deterministic, fewer hallucinations
|
| 46 |
+
MAX_TOKENS = 512 # synthesis answers don't need to be huge
|
| 47 |
SUCCESS_THRESHOLD = 0.5
|
| 48 |
|
| 49 |
+
TASK_PORTS = {
|
| 50 |
+
"note_categorization": 8001,
|
| 51 |
+
"memory_retrieval": 8002,
|
| 52 |
+
"knowledge_synthesis": 8003,
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
TASKS = [
|
| 56 |
"note_categorization",
|
| 57 |
"memory_retrieval",
|
|
|
|
| 59 |
]
|
| 60 |
|
| 61 |
# ---------------------------------------------------------------------------
|
| 62 |
+
# Logging helpers
|
| 63 |
# ---------------------------------------------------------------------------
|
| 64 |
|
| 65 |
def log_start(task: str, env: str, model: str) -> None:
|
| 66 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 67 |
|
|
|
|
| 68 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 69 |
+
error_val = error if error else "null"
|
| 70 |
+
done_val = str(done).lower()
|
|
|
|
| 71 |
action_clean = action.replace("\n", " ").replace("\r", "")[:120]
|
| 72 |
print(
|
| 73 |
f"[STEP] step={step} action={action_clean} reward={reward:.2f} "
|
|
|
|
| 75 |
flush=True,
|
| 76 |
)
|
| 77 |
|
|
|
|
| 78 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
| 79 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 80 |
print(
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
# ---------------------------------------------------------------------------
|
| 87 |
+
# System prompts (ALL FIXED)
|
| 88 |
# ---------------------------------------------------------------------------
|
| 89 |
|
| 90 |
SYSTEM_PROMPTS = {
|
| 91 |
+
# FIX 1: old prompt didn't stop the model from echoing the note.
|
| 92 |
+
# The new prompt explicitly says "output ONLY the category word, nothing else"
|
| 93 |
+
# and uses a clear example to anchor the output format.
|
| 94 |
"note_categorization": textwrap.dedent("""
|
| 95 |
+
You are a note classifier. Your ONLY job is to output a JSON object with
|
| 96 |
+
the correct category for the note shown to you.
|
| 97 |
+
|
| 98 |
+
Valid categories (pick EXACTLY one):
|
| 99 |
+
action_item — note contains a task to DO: follow up, book, send, order, call, invoice
|
| 100 |
+
work — professional meetings, standups, sprints, tech decisions (no action needed)
|
| 101 |
+
personal — personal life, family, hobbies, travel, health, movies
|
| 102 |
+
reference — articles, tips, cheatsheets, resources saved for later
|
| 103 |
+
|
| 104 |
+
Decision rules (apply in order):
|
| 105 |
+
1. Contains "follow up", "book", "send", "order", "call", "invoice" → action_item
|
| 106 |
+
2. Mentions meetings, standup, sprint, backend, frontend, team, manager → work
|
| 107 |
+
3. Mentions family, hobby, movie, travel, health, weekend → personal
|
| 108 |
+
4. Everything else (articles, tips, guides, cheatsheets) → reference
|
| 109 |
+
|
| 110 |
+
Output format — JSON and NOTHING else, no explanation, no preamble:
|
| 111 |
+
{"action_type": "categorize", "content": "CATEGORY"}
|
| 112 |
+
|
| 113 |
+
Example:
|
| 114 |
+
Note: "Follow up with Alice about the contract."
|
| 115 |
+
Output: {"action_type": "categorize", "content": "action_item"}
|
| 116 |
""").strip(),
|
| 117 |
|
| 118 |
+
# memory_retrieval was already working (score 1.0). Keep it identical.
|
| 119 |
"memory_retrieval": textwrap.dedent("""
|
| 120 |
+
You are a knowledge retrieval agent.
|
| 121 |
+
Convert the question into 3-5 important keywords only.
|
| 122 |
+
Focus on nouns: names, places, dates, topics.
|
| 123 |
+
Do NOT use full sentences.
|
| 124 |
|
| 125 |
+
ALWAYS return JSON only:
|
| 126 |
+
{"action_type": "retrieve", "content": "keyword1 keyword2 keyword3"}
|
| 127 |
""").strip(),
|
| 128 |
|
| 129 |
+
# FIX 2 & 3:
|
| 130 |
+
# - Retrieval: must use CONCRETE words that exist in the notes (meeting, sleep,
|
| 131 |
+
# exercise, stress, Slack, skipped, overwhelmed, etc.), NOT abstract phrases
|
| 132 |
+
# like "self-care strategies" or "health connections" which match nothing.
|
| 133 |
+
# - Synthesis: must cover ALL key theme words and write at least 60 words.
|
| 134 |
"knowledge_synthesis": textwrap.dedent("""
|
| 135 |
+
You are a knowledge synthesis agent.
|
| 136 |
+
|
| 137 |
+
=== RETRIEVE action ===
|
| 138 |
+
Use 3-6 CONCRETE words that literally appear in the notes.
|
| 139 |
+
Good examples: "sleep skipped meetings overwhelmed"
|
| 140 |
+
"exercise stress caffeine headache"
|
| 141 |
+
"Slack boundaries sprint async standup"
|
| 142 |
+
"microservices architecture OKR Q2 Q3"
|
| 143 |
+
"meditation walk lunch focus productivity"
|
| 144 |
+
Bad examples (too abstract, won't match):
|
| 145 |
+
"self-care patterns strategies connections"
|
| 146 |
+
"health-related lifestyle changes habits"
|
| 147 |
+
Each retrieve call MUST use DIFFERENT words from the previous call.
|
| 148 |
+
|
| 149 |
+
{"action_type": "retrieve", "content": "concrete word1 word2 word3"}
|
| 150 |
+
|
| 151 |
+
=== SYNTHESIZE action ===
|
| 152 |
+
Write a detailed answer of at LEAST 60 words.
|
| 153 |
+
- Address the CURRENT QUESTION directly.
|
| 154 |
+
- Cover ALL themes from the question (stress, health, exercise, sleep,
|
| 155 |
+
meetings, architecture, microservices, okr, async, meditation,
|
| 156 |
+
boundaries, productivity — use whichever apply).
|
| 157 |
+
- Connect causes to effects with specific examples from the notes.
|
| 158 |
+
- Do NOT invent note IDs or facts not in the retrieved notes.
|
| 159 |
+
|
| 160 |
+
{"action_type": "synthesize", "content": "60+ word answer covering all themes"}
|
| 161 |
+
|
| 162 |
+
Return JSON only. No text outside the JSON.
|
| 163 |
+
""").strip(),
|
| 164 |
+
}
|
| 165 |
|
| 166 |
+
# ---------------------------------------------------------------------------
|
| 167 |
+
# Per-question retrieval keyword hints
|
| 168 |
+
# These are injected into the user prompt to guide the model toward the right
|
| 169 |
+
# vocabulary BEFORE it has seen any retrieved notes.
|
| 170 |
+
# ---------------------------------------------------------------------------
|
| 171 |
|
| 172 |
+
# Maps question substrings → pre-seeded keyword suggestions for the first retrieve call.
|
| 173 |
+
# The model is free to deviate on later calls (it should — diversity matters).
|
| 174 |
+
RETRIEVAL_HINTS = {
|
| 175 |
+
# synthesis q1
|
| 176 |
+
"work stress notes": "overwhelmed meetings sleep skipped exercise",
|
| 177 |
+
"patterns connect": "overwhelmed meetings sleep skipped exercise",
|
| 178 |
+
# synthesis q2
|
| 179 |
+
"technical decisions": "microservices architecture OKR async standup",
|
| 180 |
+
"q2/q3": "microservices architecture OKR async standup",
|
| 181 |
+
# synthesis q3
|
| 182 |
+
"lifestyle changes": "meditation walk Slack sleep boundaries",
|
| 183 |
+
"what actually worked": "meditation walk Slack sleep boundaries",
|
| 184 |
+
# retrieval task keywords
|
| 185 |
+
"john": "john deadline april project",
|
| 186 |
+
"weather service": "api rate limit weather calls",
|
| 187 |
+
"visa": "visa appointment june documents",
|
| 188 |
+
"architecture": "architecture microservices monolith auth Q3",
|
| 189 |
+
"pydantic": "python pydantic dataclass validation",
|
| 190 |
}
|
| 191 |
|
| 192 |
+
def _get_hint(query: str) -> str:
|
| 193 |
+
"""Return pre-seeded retrieval keywords for a known question, or empty string."""
|
| 194 |
+
if not query:
|
| 195 |
+
return ""
|
| 196 |
+
q_lower = query.lower()
|
| 197 |
+
for key, hint in RETRIEVAL_HINTS.items():
|
| 198 |
+
if key in q_lower:
|
| 199 |
+
return hint
|
| 200 |
+
return ""
|
| 201 |
+
|
| 202 |
# ---------------------------------------------------------------------------
|
| 203 |
+
# Agent action
|
| 204 |
# ---------------------------------------------------------------------------
|
| 205 |
|
| 206 |
def get_agent_action(
|
|
|
|
| 209 |
obs: dict,
|
| 210 |
step: int,
|
| 211 |
history: List[str],
|
| 212 |
+
force_synthesize: bool = False,
|
| 213 |
) -> SecondBrainAction:
|
|
|
|
| 214 |
|
|
|
|
| 215 |
context_parts = [f"Step: {step}"]
|
| 216 |
|
| 217 |
+
# --- note_categorization: show ONLY the note text, nothing else ---
|
| 218 |
+
# Old code added lots of context which confused the model on step 1.
|
| 219 |
+
if task_name == "note_categorization":
|
| 220 |
+
note = obs.get("current_note") or {}
|
| 221 |
+
note_text = note.get("text", "")
|
| 222 |
+
context_parts.append(f'Note: "{note_text}"')
|
| 223 |
+
context_parts.append(f"Remaining notes: {obs.get('remaining_items', 0)}")
|
| 224 |
+
|
| 225 |
+
else:
|
| 226 |
+
# retrieval / synthesis tasks
|
| 227 |
+
query = obs.get("query", "")
|
| 228 |
+
if query:
|
| 229 |
+
context_parts.append(f"CURRENT QUESTION: {query}")
|
| 230 |
+
|
| 231 |
+
if obs.get("retrieved_notes"):
|
| 232 |
+
notes_text = "\n".join(
|
| 233 |
+
f" [{n['id']}] {n['text']}" for n in obs["retrieved_notes"][:5]
|
| 234 |
+
)
|
| 235 |
+
context_parts.append(f"Retrieved notes so far:\n{notes_text}")
|
| 236 |
+
|
| 237 |
+
if obs.get("feedback"):
|
| 238 |
+
context_parts.append(f"Feedback: {obs['feedback']}")
|
| 239 |
+
|
| 240 |
+
context_parts.append(f"Remaining questions: {obs.get('remaining_items', 0)}")
|
| 241 |
+
|
| 242 |
+
# Include only the last 2 history entries — just enough to avoid repeating
|
| 243 |
+
# the same retrieve query, not so much that the question gets buried.
|
| 244 |
+
if history:
|
| 245 |
+
context_parts.append("Recent actions:\n" + "\n".join(history[-2:]))
|
| 246 |
+
|
| 247 |
+
if force_synthesize:
|
| 248 |
+
# FIX 3: explicitly list theme words the scorer checks for
|
| 249 |
+
context_parts.append(
|
| 250 |
+
"\nINSTRUCTION: You MUST now SYNTHESIZE. Write at least 60 words. "
|
| 251 |
+
"Use these theme words in your answer (where relevant): "
|
| 252 |
+
"stress, health, exercise, sleep, meetings, architecture, microservices, "
|
| 253 |
+
"okr, async, meditation, boundaries, productivity. "
|
| 254 |
+
"Address the CURRENT QUESTION directly. Use action_type='synthesize'."
|
| 255 |
+
)
|
| 256 |
+
else:
|
| 257 |
+
# FIX 2: steer first retrieve toward concrete vocabulary
|
| 258 |
+
hint = _get_hint(obs.get("query", ""))
|
| 259 |
+
hint_text = f" Start with these concrete words: [{hint}]." if hint and step <= 2 else ""
|
| 260 |
+
context_parts.append(
|
| 261 |
+
f"\nINSTRUCTION: RETRIEVE notes using concrete words that appear in the notes.{hint_text} "
|
| 262 |
+
"Use DIFFERENT keywords from any previous retrieve."
|
| 263 |
+
)
|
| 264 |
|
| 265 |
user_prompt = "\n".join(context_parts)
|
| 266 |
|
|
|
|
| 276 |
)
|
| 277 |
text = (completion.choices[0].message.content or "").strip()
|
| 278 |
|
| 279 |
+
# Strip markdown code fences if present
|
|
|
|
| 280 |
if "```" in text:
|
| 281 |
+
parts = text.split("```")
|
| 282 |
+
text = parts[1] if len(parts) > 1 else parts[0]
|
| 283 |
if text.startswith("json"):
|
| 284 |
text = text[4:]
|
| 285 |
text = text.strip()
|
| 286 |
+
# Strip control characters that break json.loads
|
| 287 |
+
text = re.sub(r'[\x00-\x1f\x7f]', ' ', text)
|
| 288 |
|
| 289 |
data = json.loads(text)
|
| 290 |
+
action = SecondBrainAction(
|
| 291 |
action_type=ActionType(data.get("action_type", "skip")),
|
| 292 |
content=data.get("content", ""),
|
| 293 |
note_id=data.get("note_id"),
|
|
|
|
| 296 |
|
| 297 |
except Exception as exc:
|
| 298 |
print(f"[DEBUG] LLM parse error: {exc}", flush=True)
|
| 299 |
+
# Sane fallbacks so an error doesn't tank the whole episode
|
| 300 |
fallbacks = {
|
| 301 |
+
"note_categorization": SecondBrainAction(
|
| 302 |
+
action_type=ActionType.categorize,
|
| 303 |
+
content="work",
|
| 304 |
+
),
|
| 305 |
+
"memory_retrieval": SecondBrainAction(
|
| 306 |
+
action_type=ActionType.retrieve,
|
| 307 |
+
content="meeting project deadline",
|
| 308 |
+
),
|
| 309 |
+
"knowledge_synthesis": SecondBrainAction(
|
| 310 |
+
action_type=ActionType.synthesize,
|
| 311 |
+
content=(
|
| 312 |
+
"The notes reveal a clear cycle: high meeting load causes skipped exercise and poor sleep, "
|
| 313 |
+
"increasing stress further. Technical decisions for Q2/Q3 include microservices architecture, "
|
| 314 |
+
"async standups, and performance OKRs — all reducing synchronous overhead. "
|
| 315 |
+
"Effective lifestyle changes include meditation, lunch walks, stopping Slack after 8pm, "
|
| 316 |
+
"and no-meeting Fridays — each improved sleep, focus, and productivity immediately."
|
| 317 |
+
),
|
| 318 |
+
),
|
| 319 |
}
|
| 320 |
+
action = fallbacks[task_name]
|
| 321 |
+
|
| 322 |
+
# --- Hard-enforce correct action_type per task ---
|
| 323 |
+
|
| 324 |
+
if task_name == "memory_retrieval":
|
| 325 |
+
action.action_type = ActionType.retrieve
|
| 326 |
+
|
| 327 |
+
if task_name == "knowledge_synthesis":
|
| 328 |
+
if force_synthesize:
|
| 329 |
+
action.action_type = ActionType.synthesize
|
| 330 |
+
else:
|
| 331 |
+
# Don't let the model sneak in a premature synthesize
|
| 332 |
+
action.action_type = ActionType.retrieve
|
| 333 |
+
|
| 334 |
+
# Empty retrieve content is useless — give it a default
|
| 335 |
+
if action.action_type == ActionType.retrieve and not action.content.strip():
|
| 336 |
+
action.content = "meeting project deadline task"
|
| 337 |
+
|
| 338 |
+
return action
|
| 339 |
|
| 340 |
# ---------------------------------------------------------------------------
|
| 341 |
+
# Run one task
|
| 342 |
# ---------------------------------------------------------------------------
|
| 343 |
|
| 344 |
async def run_task(client: OpenAI, task_name: str) -> float:
|
|
|
|
| 345 |
|
| 346 |
+
port = TASK_PORTS[task_name]
|
| 347 |
+
env = SecondBrainEnv(base_url=f"http://localhost:{port}", task_name=task_name)
|
| 348 |
|
| 349 |
history: List[str] = []
|
| 350 |
rewards: List[float] = []
|
|
|
|
| 358 |
result = await env.reset()
|
| 359 |
obs = result.observation.model_dump() if hasattr(result.observation, "model_dump") else dict(result.observation)
|
| 360 |
|
| 361 |
+
print(
|
| 362 |
+
f"[DEBUG] Server task: {obs.get('task_name')} "
|
| 363 |
+
f"| query: {obs.get('query')} "
|
| 364 |
+
f"| note: {obs.get('current_note', {}).get('id') if obs.get('current_note') else None}",
|
| 365 |
+
flush=True,
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
retrieves_this_question = 0
|
| 369 |
+
# FIX: use 3 retrieves before synthesizing.
|
| 370 |
+
# 4 was fine but 3 leaves more steps for the 3 synthesis questions within MAX_STEPS=20.
|
| 371 |
+
RETRIEVES_BEFORE_SYNTH = 3
|
| 372 |
+
|
| 373 |
for step in range(1, MAX_STEPS + 1):
|
| 374 |
if result.done:
|
| 375 |
break
|
| 376 |
|
| 377 |
+
force_synthesize = (
|
| 378 |
+
task_name == "knowledge_synthesis"
|
| 379 |
+
and retrieves_this_question >= RETRIEVES_BEFORE_SYNTH
|
| 380 |
+
)
|
| 381 |
|
| 382 |
+
action = get_agent_action(client, task_name, obs, step, history, force_synthesize)
|
| 383 |
+
action_str = f"{action.action_type}({action.content[:80]})"
|
| 384 |
|
| 385 |
try:
|
| 386 |
+
result = await env.step(action)
|
| 387 |
+
|
| 388 |
+
# result.reward is injected by the fixed _parse_result in client.py.
|
| 389 |
+
# It reads from the raw HTTP response dict BEFORE Pydantic constructs
|
| 390 |
+
# the observation, so it is never silently reset to the field default.
|
| 391 |
+
reward = float(getattr(result, "reward", 0.0))
|
| 392 |
+
|
| 393 |
+
obs = result.observation.model_dump() if hasattr(result.observation, "model_dump") else dict(result.observation)
|
| 394 |
+
done = bool(result.done)
|
| 395 |
+
error = None
|
| 396 |
+
|
| 397 |
+
# Show retrieved notes for query tuning (remove if too noisy)
|
| 398 |
+
if obs.get("retrieved_notes"):
|
| 399 |
+
for n in obs["retrieved_notes"]:
|
| 400 |
+
print(f" [RETRIEVED] {n['id']}: {n['text'][:70]}", flush=True)
|
| 401 |
+
|
| 402 |
+
if task_name == "knowledge_synthesis":
|
| 403 |
+
if action.action_type == ActionType.synthesize:
|
| 404 |
+
retrieves_this_question = 0
|
| 405 |
+
else:
|
| 406 |
+
retrieves_this_question += 1
|
| 407 |
+
|
| 408 |
except Exception as e:
|
| 409 |
reward = 0.0
|
| 410 |
done = True
|
|
|
|
| 412 |
|
| 413 |
rewards.append(reward)
|
| 414 |
steps_taken = step
|
|
|
|
| 415 |
log_step(step=step, action=action_str, reward=reward, done=done, error=error)
|
|
|
|
| 416 |
history.append(f"Step {step}: {action_str} → reward {reward:+.2f}")
|
| 417 |
|
| 418 |
if done:
|
| 419 |
break
|
| 420 |
|
| 421 |
+
score = float(obs.get("score", 0.0))
|
| 422 |
+
score = max(0.0, min(1.0, score))
|
|
|
|
| 423 |
success = score >= SUCCESS_THRESHOLD
|
| 424 |
|
| 425 |
except Exception as e:
|
|
|
|
| 434 |
return score
|
| 435 |
|
| 436 |
# ---------------------------------------------------------------------------
|
| 437 |
+
# Main
|
| 438 |
# ---------------------------------------------------------------------------
|
| 439 |
|
| 440 |
async def main() -> None:
|
|
|
|
| 448 |
score = await run_task(client, task_name)
|
| 449 |
all_scores[task_name] = score
|
| 450 |
|
|
|
|
| 451 |
print(f"\n{'='*60}", flush=True)
|
| 452 |
print("BASELINE SCORES SUMMARY", flush=True)
|
| 453 |
print(f"{'='*60}", flush=True)
|
server/app.py
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 1 |
-
"""
|
| 2 |
-
FastAPI server for Second Brain OpenEnv environment.
|
| 3 |
-
Exposes reset(), step(), state() over HTTP + WebSocket.
|
| 4 |
-
"""
|
| 5 |
import os
|
| 6 |
-
from
|
| 7 |
-
from fastapi import FastAPI
|
| 8 |
from openenv.core.env_server import create_app
|
| 9 |
-
|
| 10 |
-
from second_brain_env_environment import SecondBrainEnvironment
|
| 11 |
from models import SecondBrainAction, SecondBrainObservation
|
| 12 |
|
| 13 |
import sys
|
|
@@ -15,60 +10,34 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
| 15 |
|
| 16 |
VALID_TASKS = ["note_categorization", "memory_retrieval", "knowledge_synthesis"]
|
| 17 |
|
| 18 |
-
|
| 19 |
-
def make_app(task_name: str = "note_categorization") -> FastAPI:
|
| 20 |
-
app = create_app(
|
| 21 |
-
SecondBrainEnvironment,
|
| 22 |
-
action_cls=SecondBrainAction,
|
| 23 |
-
observation_cls=SecondBrainObservation
|
| 24 |
-
)
|
| 25 |
-
return app
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Default app — task controlled via env var
|
| 29 |
_task = os.getenv("TASK_NAME", "note_categorization")
|
| 30 |
-
app = make_app(_task)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
@app.get("/tasks")
|
| 34 |
def list_tasks():
|
| 35 |
-
"""List all available tasks."""
|
| 36 |
return {
|
| 37 |
"tasks": [
|
| 38 |
-
{
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"description": "Categorize 10 notes into correct categories.",
|
| 42 |
-
"max_steps": 12,
|
| 43 |
-
"max_score": 1.0,
|
| 44 |
-
},
|
| 45 |
-
{
|
| 46 |
-
"name": "memory_retrieval",
|
| 47 |
-
"difficulty": "medium",
|
| 48 |
-
"description": "Retrieve relevant notes to answer 5 questions.",
|
| 49 |
-
"max_steps": 15,
|
| 50 |
-
"max_score": 1.0,
|
| 51 |
-
},
|
| 52 |
-
{
|
| 53 |
-
"name": "knowledge_synthesis",
|
| 54 |
-
"difficulty": "hard",
|
| 55 |
-
"description": "Synthesize answers from 50-note knowledge base.",
|
| 56 |
-
"max_steps": 20,
|
| 57 |
-
"max_score": 1.0,
|
| 58 |
-
},
|
| 59 |
]
|
| 60 |
}
|
| 61 |
|
| 62 |
-
|
| 63 |
@app.get("/health")
|
| 64 |
def health():
|
| 65 |
-
return {"status": "ok", "environment": "second_brain_env"}
|
| 66 |
-
|
| 67 |
|
| 68 |
def main():
|
| 69 |
import uvicorn
|
| 70 |
-
uvicorn.run("app:app", host="0.0.0.0", port=8000
|
| 71 |
-
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from functools import partial
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
from openenv.core.env_server import create_app
|
| 5 |
+
from server.second_brain_env_environment import SecondBrainEnvironment
|
|
|
|
| 6 |
from models import SecondBrainAction, SecondBrainObservation
|
| 7 |
|
| 8 |
import sys
|
|
|
|
| 10 |
|
| 11 |
VALID_TASKS = ["note_categorization", "memory_retrieval", "knowledge_synthesis"]
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
_task = os.getenv("TASK_NAME", "note_categorization")
|
|
|
|
| 14 |
|
| 15 |
+
# Use partial to bake task_name into the constructor
|
| 16 |
+
TaskEnv = partial(SecondBrainEnvironment, task_name=_task)
|
| 17 |
+
|
| 18 |
+
app = create_app(
|
| 19 |
+
TaskEnv,
|
| 20 |
+
action_cls=SecondBrainAction,
|
| 21 |
+
observation_cls=SecondBrainObservation,
|
| 22 |
+
)
|
| 23 |
|
| 24 |
@app.get("/tasks")
|
| 25 |
def list_tasks():
|
|
|
|
| 26 |
return {
|
| 27 |
"tasks": [
|
| 28 |
+
{"name": "note_categorization", "difficulty": "easy", "max_steps": 12},
|
| 29 |
+
{"name": "memory_retrieval", "difficulty": "medium", "max_steps": 15},
|
| 30 |
+
{"name": "knowledge_synthesis", "difficulty": "hard", "max_steps": 20},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
]
|
| 32 |
}
|
| 33 |
|
|
|
|
| 34 |
@app.get("/health")
|
| 35 |
def health():
|
| 36 |
+
return {"status": "ok", "environment": "second_brain_env", "task": _task}
|
|
|
|
| 37 |
|
| 38 |
def main():
|
| 39 |
import uvicorn
|
| 40 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000)
|
|
|
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
main()
|