Skip to main content
The search method performs a hybrid semantic search across stored memories and returns the most relevant results ranked by score. Call it before generating a response to give your agent relevant context from prior conversations.

Method signature

cog.search(
    query: str,
    owner_id: str | None = None,
    agent_id: str | None = None,
    session_id: str | None = None,
    limit: int | None = None,
    cross_session: bool | None = None,
) -> List[CognisSearchResult]
At least one of owner_id, agent_id, or session_id must be provided.

Parameters

ParameterTypeRequiredDescription
querystrYesNatural language search query.
owner_idstrConditionalFilter by owner or user identifier.
agent_idstrConditionalFilter by agent identifier.
session_idstrConditionalFilter by session identifier.
limitintNoMaximum number of results to return.
cross_sessionboolNoSearch across all sessions for the given owner.

Response

Returns a List[CognisSearchResult]. Each result has the following fields:
FieldTypeDescription
idstrMemory record ID.
contentstrThe memory content.
scorefloat | NoneSimilarity score. Higher values indicate stronger relevance.
owner_idstr | NoneOwner identifier.
agent_idstr | NoneAgent identifier.
session_idstr | NoneSession identifier.
metadataDict | NoneAdditional metadata attached to the memory.
created_atstr | NoneCreation timestamp.

Basic usage

from lyzr import Cognis

cog = Cognis(api_key="sk-your-api-key")

results = cog.search(query="What is the user's name?", owner_id="user_alice")
for result in results:
    print(f"{result.content}  (score: {result.score})")
Narrow results by combining scope identifiers.
# Within a specific session
results = cog.search(
    query="project details",
    owner_id="user_alice",
    session_id="project_session",
)

# Across a specific agent's memories
results = cog.search(
    query="user preferences",
    owner_id="user_alice",
    agent_id="support_bot",
)
Search across all sessions for an owner by setting cross_session=True.
results = cog.search(
    query="What programming languages does the user know?",
    owner_id="user_alice",
    cross_session=True,
)

Limiting results

results = cog.search(
    query="hobbies",
    owner_id="user_alice",
    limit=5,
)

Async usage

import asyncio
from lyzr import Cognis

async def main():
    async with Cognis(api_key="sk-your-api-key") as cog:
        results = await cog.asearch(
            query="What is the user's name?",
            owner_id="user_alice",
            limit=10,
        )
        for result in results:
            print(result.content)

asyncio.run(main())
Write queries as natural language questions for best results. For example, "What is the user's favorite color?" retrieves more relevant results than a bare keyword like "favorite color".