> ## Documentation Index
> Fetch the complete documentation index at: https://docs.negentro.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a Companion with Negentro & PiyAPI

> Step-by-step guide to giving an AI assistant persistent long-term memory across sessions.

## System Architecture

A persistent AI companion requires three synchronized subsystems: (1) Fact Extraction from user conversations, (2) Hybrid Vector + BM25 Storage with bitemporal timestamps, and (3) Context Window Injection during generation.

### End-to-End Companion Loop

```python theme={null}
from piyapi_memory import PiyAPIClient

client = PiyAPIClient(api_key="sk_live_your_api_key_here")

def chat_turn(user_id: str, prompt: str) -> str:
    # Step 1: Recall relevant facts using hybrid RRF scoring
    context_records = client.search.query(
        query=prompt,
        filter={"metadata.user_id": user_id},
        limit=4
    )
    
    # Step 2: Format context injection
    memory_context = "\n".join([f"- {r['content']}" for r in context_records['results']])
    
    # Step 3: Run LLM inference with augmented context
    system_prompt = f"""You are a helpful personal companion.
User Background & Memories:
{memory_context}"""
    
    # Step 4: After replying, mine new facts for persistence
    client.memories.create(
        content=prompt,
        tags=["conversation"],
        metadata={"user_id": user_id}
    )
    return "Response generated with active context."
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.