Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (91 loc) · 4.1 KB
/
Copy pathmain.py
File metadata and controls
117 lines (91 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Cortex REST API — a multi-tenant, self-pruning memory service.
Memory is isolated per `namespace` (e.g. a user id) and shared across agents
within that namespace. Engines + persistence are managed by cortex.registry.
Run: uvicorn main:app --host 0.0.0.0 --port 8000
"""
from typing import Optional
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from cortex.config import cfg
from cortex.registry import get_engine
from cortex import qwen_client as q
app = FastAPI(title="Cortex", version="1.0.0",
description="Transparent, self-pruning memory layer for AI agents.")
app.add_middleware(CORSMiddleware, allow_origins=cfg.cors_origins or ["*"],
allow_methods=["*"], allow_headers=["*"])
_NS = Field("default", max_length=64)
_AGENT = Field("agent", max_length=64)
class RememberIn(BaseModel):
namespace: str = _NS
agent: str = _AGENT
content: str = Field(..., min_length=1, max_length=cfg.max_content_len)
kind: str = Field("fact", pattern="^(fact|preference|event)$")
salience: float = Field(0.6, ge=0, le=1)
source: str = Field("", max_length=200)
class RecallIn(BaseModel):
namespace: str = _NS
agent: str = _AGENT
query: str = Field(..., min_length=1, max_length=cfg.max_content_len)
k: int = Field(5, ge=1, le=50)
class ChatIn(BaseModel):
namespace: str = _NS
agent: str = _AGENT
message: str = Field(..., min_length=1, max_length=cfg.max_content_len)
system: str = Field(
"You are a helpful assistant. Use the user's remembered context when "
"relevant; never invent it.", max_length=2000)
@app.get("/health")
def health():
return {"status": "ok", "version": app.version,
"qwen_key": q.has_key(), "store_dir": cfg.store_dir}
@app.post("/remember")
def remember(inp: RememberIn):
try:
eng = get_engine(inp.namespace, inp.agent)
m = eng.remember(inp.content, inp.kind, inp.salience, source=inp.source or inp.agent)
return {"memory": m.to_public()}
except Exception as e:
raise HTTPException(500, f"remember failed: {e}")
@app.post("/recall")
def recall(inp: RecallIn):
try:
eng = get_engine(inp.namespace, inp.agent)
return {"memories": [m.to_public() for m in eng.retrieve(inp.query, inp.k)]}
except Exception as e:
raise HTTPException(500, f"recall failed: {e}")
@app.post("/chat")
def chat(inp: ChatIn):
if not q.has_key():
raise HTTPException(503, "No DASHSCOPE_API_KEY set — /chat needs a live "
"model. Use /remember + /recall for engine ops.")
eng = get_engine(inp.namespace, inp.agent)
retrieved = eng.retrieve(inp.message)
mem_block = "\n".join(f"- {m.content}" for m in retrieved) or "(nothing yet)"
system = f"{inp.system}\n\nKnown about the user:\n{mem_block}"
try:
resp = q.chat([{"role": "system", "content": system},
{"role": "user", "content": inp.message}])
answer = resp.choices[0].message.content
except Exception as e:
raise HTTPException(502, f"model error: {e}")
stored = eng.consider(inp.message, answer)
return {"answer": answer,
"retrieved": [m.to_public() for m in retrieved],
"stored": [m.to_public() for m in stored]}
@app.get("/memories")
def memories(namespace: str = Query("default", max_length=64)):
eng = get_engine(namespace)
return {"namespace": namespace, "memories": [m.to_public() for m in eng.memories]}
@app.get("/audit")
def audit(namespace: str = Query("default", max_length=64),
memory_id: Optional[str] = None):
eng = get_engine(namespace)
return {"namespace": namespace, "audit": eng.export_audit(memory_id)}
@app.delete("/memories/{memory_id}")
def delete_memory(memory_id: str, namespace: str = Query("default", max_length=64)):
eng = get_engine(namespace)
if eng._by_id(memory_id) is None:
raise HTTPException(404, "memory not found")
eng.supersede(eng._by_id(memory_id), reason="deleted via API")
return {"deleted": memory_id}