Smritea

Quickstart

Install an SDK, store a memory and search it back in five minutes.

Ask AI about this page

This tutorial stores one memory and searches it back. You need an app ID and an API key from the dashboard. When you finish, your script prints the memory text with a relevance score.

Get your credentials

Create an app in the dashboard and copy the app ID and the API key. The key appears once. See Create an app and API key.

If your app is in the India region, also read Regions. You must point the SDK at the India host.

Set two environment variables

The examples read the app ID and the API key from the environment.

export SMRITEA_APP_ID="app_..."
export SMRITEA_API_KEY="smrt_..."

Install the package

pip install smritea-sdk

For Java, replace LATEST with the current release from Maven Central.

Store a memory and search it

Save this code in a file. It stores one sentence for a user named Alice. Then it asks a question and prints the matches.

import os

from smritea import MemoryScope, SmriteaClient, SmriteaError

client = SmriteaClient(
    api_key=os.environ["SMRITEA_API_KEY"],
    app_id=os.environ["SMRITEA_APP_ID"],
)
alice = MemoryScope(actor_id="alice", actor_type="user")

try:
    client.add("Alice is vegetarian and loves hiking", scope=alice)
    for r in client.search("What does Alice eat?", scope=alice):
        if r.memory:
            print(f"{r.score:.2f}  {r.memory.content}")
except SmriteaError as e:
    print(e.http_status, e.code, e.request_id)

Run it

Run the file with your usual runner. For Python, use python quickstart.py.

The script prints one line for each matching memory. Each line holds a relevance score and the memory text:

<score>  <memory text>

The memory text depends on your app's storage engine. A Deep Search app rewrites the sentence into separate facts. A Quick Search app stores the sentence as you sent it. See How Smritea stores memory.

If the script prints an error line instead, the three values are the HTTP status, the error code and the request ID. Look the code up in Errors and codes.

What you built

You stored a memory for the user alice and found it again with a question. The scope you passed to both calls kept the search on Alice's memories. To go further, read Memory scope and Search methods and rerankers.

Use search results in a model prompt

A large language model is the text model that answers your user. Search returns memory text, and your code puts that text in the prompt for your model. Smritea does not call the model for you.

  1. Search with the user's scope and a limit.
  2. Read memory.content from each result.
  3. Put those lines in the system message of your model call.
  4. If the search fails, send the system message without facts.

The function below does all four steps and returns the system message. It reuses the client and the scope from the quickstart. The Go and Java versions take them as parameters. Replace the base text with your own instructions.

from smritea import SmriteaError

BASE = "You are a helpful assistant."


def build_system_prompt(client, scope, question):
    try:
        results = client.search(question, scope=scope, limit=5)
    except SmriteaError:
        return BASE
    if not results:
        return BASE
    facts = "\n".join(f"- {r.memory.content}" for r in results if r.memory)
    return f"{BASE}\nKnown facts about the user:\n{facts}"

The C# block also catches network errors, because the SDK wraps only HTTP errors.

Pass the returned string as the system message of your model call, using your model provider's own SDK. Each result has a score, and you should compare scores only within one search call. Search methods and rerankers explains why. To decide which search errors to retry, read Handle errors and retries.

Last updated on