Replace repository with DuckLM runtime
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class MemoryDecision(BaseModel):
|
||||
should_store: bool
|
||||
memory_type: str
|
||||
summary: str
|
||||
importance: float
|
||||
metadata: dict[str, str] = {}
|
||||
|
||||
|
||||
class MemoryPolicy:
|
||||
async def classify(self, summary: str, task_id: str) -> MemoryDecision:
|
||||
return MemoryDecision(
|
||||
should_store=False,
|
||||
memory_type="event",
|
||||
summary=summary,
|
||||
importance=0.0,
|
||||
metadata={"task_id": task_id, "source": "stub_policy"},
|
||||
)
|
||||
@@ -0,0 +1,70 @@
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
class EmbeddingsUnavailableError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class VectorMemory:
|
||||
def __init__(
|
||||
self,
|
||||
qdrant_url: str,
|
||||
collection_name: str = "duck_memory",
|
||||
embeddings_base_url: str | None = "http://127.0.0.1:8081/v1",
|
||||
):
|
||||
self.qdrant_url = qdrant_url.rstrip("/")
|
||||
self.collection_name = collection_name
|
||||
self.embeddings_base_url = embeddings_base_url.rstrip("/") if embeddings_base_url else None
|
||||
|
||||
async def add_memory(self, text: str, metadata: dict[str, Any] | None = None) -> str:
|
||||
vector = await self._embed(text)
|
||||
point_id = str(uuid4())
|
||||
async with httpx.AsyncClient(timeout=20.0, trust_env=False) as client:
|
||||
await client.put(
|
||||
f"{self.qdrant_url}/collections/{self.collection_name}",
|
||||
json={"vectors": {"size": len(vector), "distance": "Cosine"}},
|
||||
)
|
||||
response = await client.put(
|
||||
f"{self.qdrant_url}/collections/{self.collection_name}/points",
|
||||
json={
|
||||
"points": [
|
||||
{
|
||||
"id": point_id,
|
||||
"vector": vector,
|
||||
"payload": {"text": text, **(metadata or {})},
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return point_id
|
||||
|
||||
async def search_memory(self, query: str, limit: int = 5) -> list[dict[str, Any]]:
|
||||
vector = await self._embed(query)
|
||||
async with httpx.AsyncClient(timeout=20.0, trust_env=False) as client:
|
||||
response = await client.post(
|
||||
f"{self.qdrant_url}/collections/{self.collection_name}/points/search",
|
||||
json={"vector": vector, "limit": limit, "with_payload": True},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json().get("result", [])
|
||||
|
||||
async def _embed(self, text: str) -> list[float]:
|
||||
if not self.embeddings_base_url:
|
||||
raise EmbeddingsUnavailableError(
|
||||
"Embeddings endpoint is not configured; vector memory is explicit stub."
|
||||
)
|
||||
async with httpx.AsyncClient(timeout=20.0, trust_env=False) as client:
|
||||
response = await client.post(
|
||||
f"{self.embeddings_base_url}/embeddings",
|
||||
json={"model": "local-main", "input": text},
|
||||
)
|
||||
if response.status_code >= 400:
|
||||
raise EmbeddingsUnavailableError(
|
||||
f"Embeddings endpoint unavailable: HTTP {response.status_code}"
|
||||
)
|
||||
data = response.json()["data"][0]["embedding"]
|
||||
return [float(value) for value in data]
|
||||
Reference in New Issue
Block a user