tinycodinglm / agentic /aiagent-0030.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
2.23 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do agents implement working, episodic (vector DB), semantic and procedural memory? Show ChromaDB storage with required ids and LM Studio embeddings. Include code examples.

Assistant

Agent memory layers:

  1. Working: current context window - summarize/slide when full
  2. Episodic: past turns in a vector DB, retrieved by similarity
  3. Semantic: facts as triples/rules
  4. Procedural: reusable skills/scripts

ChromaDB add() requires unique ids (see Chroma docs).

python
import chromadb
from openai import OpenAI

client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
MODEL = "text-embedding-nomic-embed-text-v1.5"  # example; use loaded id

class AgentMemory:
    def __init__(self, name: str = "agent_memory"):
        self.db = chromadb.Client()
        self.collection = self.db.get_or_create_collection(name=name)

    def _embed(self, text: str) -> list[float]:
        resp = client.embeddings.create(model=MODEL, input=[text.replace("\n", " ")])
        return resp.data[0].embedding

    def store(self, doc_id: str, content: str, metadata: dict | None = None) -> None:
        self.collection.add(
            ids=[doc_id],
            documents=[content],
            embeddings=[self._embed(content)],
            metadatas=[metadata or {}],
        )

    def recall(self, query: str, n: int = 3) -> list[str]:
        res = self.collection.query(query_embeddings=[self._embed(query)], n_results=n)
        docs = res.get("documents") or [[]]
        return docs[0]