Metadata-Version: 2.5
Name: agent-framework-recallic
Version: 0.1.0
Summary: Recallic as long-term memory for Microsoft Agent Framework agents: self-hosted, no LLM calls for memory, says when it has no record.
Project-URL: Homepage, https://www.recallic.com
Project-URL: Source, https://github.com/Recallic/Recallic/tree/main/integrations/agent-framework
License-Expression: Apache-2.0
License-File: LICENSE
Requires-Python: >=3.11
Requires-Dist: agent-framework-core<2,>=1.26
Requires-Dist: recallic>=0.3.0
Description-Content-Type: text/markdown

# agent-framework-recallic: Recallic memory for Microsoft Agent Framework

Website: [recallic.com](https://www.recallic.com)

Long-term memory for [Microsoft Agent Framework](https://github.com/microsoft/agent-framework) agents, backed by a
self-hosted [Recallic](https://github.com/Recallic/Recallic) server. It is a context provider.

* **No LLM calls for memory.** Storing and searching use Recallic's local models.
* **Across sessions.** Each run, what this user said before that bears on their latest message is recalled, whatever
  session it was said in.
* **Honest.** When nothing answers it, the model is told there is no record instead of being handed near-misses to
  guess from.
* **Your data stays on your server**, separated per user and per agent.

This package is Apache-2.0. The Recallic server is source-available.

## Install

```bash
pip install agent-framework-recallic
```

It works with any Agent Framework chat client. The example below uses OpenAI (`pip install agent-framework-openai`).

Run Recallic ([quickstart](../../docs/quickstart.md)) and create a key with role `service`, which acts for each of
your users (or use a user's own key and leave the user out):

```bash
python -m backend.tools.keys create --user my-agent-app --role service
```

## Usage: `RecallicContextProvider`

```python
import asyncio
import os

from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient
from agent_framework_recallic import RecallicContextProvider

memory = RecallicContextProvider(os.environ["RECALLIC_SERVICE_KEY"], base_url="http://localhost:8000",
                                 mode="balanced")                  # or "cautious" / "helpful"
agent = Agent(client=OpenAIChatClient(), instructions="You are a helpful assistant.",
              context_providers=[memory])


async def main():
    session = agent.create_session()
    session.state["user_id"] = "alice"
    await agent.run("I moved to Pune in March.", session=session)

    later = agent.create_session()                                # days later, a new session
    later.state["user_id"] = "alice"
    print(await agent.run("Which city did I move to?", session=later))   # "Pune."

asyncio.run(main())
```

Before each run the provider recalls on the user's latest message. It adds the memories to the agent's instructions
(your own instructions are kept), or tells the model there is no record. After the run it stores the user's messages.
Recall runs once per run, not once per model call in a tool loop.

**Whose memory.** The provider reads the user for each run, in this order:

1. `session.state["user_id"]`, set by your app on the session.
2. The run's `options={"user": ...}`, e.g. `agent.run(text, options={"user": "alice"})`. Note that Agent Framework
   also passes `user` on to the model provider as the end-user id.
3. The `user_id` given to the constructor.

With none of these, a `service` key falls back to the key's own user, which every run would then share. So with a
`service` key, always name the user. With a user's own key, leave all of them out.

| Parameter | Default | What it does |
|---|---|---|
| `api_key` | required | A Recallic key. `service` acts for each user; a user key acts for itself. |
| `base_url` | `http://localhost:8000` | Your Recallic server. |
| `user_id` | `None` | The user when the session and run name none. |
| `agent_id` | `agent-framework` | Recallic agent id; each agent reads back what it wrote (and what any agent shared). |
| `mode` | server's `recall.mode` | `cautious` reads with a stricter reader and declines more (more "I don't know"); `helpful` answers more counting, ordering and "how long between" questions. |
| `limit` | 5 | Memories given to the model per run. |
| `store_user_messages` | `True` | Store the user's messages after each run (after recalling). |
| `tell_model_on_decline` | `True` | When nothing answers, tell the model not to guess about the user's past. |
| `add_tools` | `False` | Also give the model `recall_memory(query)` and `save_memory(fact)` tools for this run's user. |
| `source_id` | `recallic` | The provider's source id in Agent Framework. |

Notes:

* `memory.tools(user_id)` returns the two tools for one user, if you want to add them to an agent yourself.
* `format_memories(recall_result)` turns any Recallic recall result into the text the provider gives the model.
* Only the user's messages are stored, not the model's replies.
* Needs Recallic server 0.2.5 or newer. Tested with agent-framework-core 1.26.0.

## Tests

Against a running Recallic: `RECALLIC_URL=http://127.0.0.1:8000 RECALLIC_SERVICE_KEY=rx_... pytest tests`.
They run a real `Agent` with a scripted chat client, including its tool loop.

Licence: Apache-2.0.
