Knowledge Search

Search the knowledge base and list stored content

Create a Python file

knowledge_search.py
import asyncio
import os

from agno.client import AgentOSClient


HEADERS = {"Authorization": f"Bearer {os.environ['OS_SECURITY_KEY']}"}

async def main():
    client = AgentOSClient(base_url="http://localhost:7778")

    print("=" * 60)
    print("Knowledge Search")
    print("=" * 60)

    # Get knowledge configuration
    print("\n1. Getting knowledge config...")
    try:
        config = await client.get_knowledge_config(headers=HEADERS)
        print(f"   Available readers: {config.readers if hasattr(config, 'readers') else 'N/A'}")
        print(f"   Available chunkers: {config.chunkers if hasattr(config, 'chunkers') else 'N/A'}")
    except Exception as e:
        print(f"   Knowledge not configured: {e}")
        return

    # List existing content
    print("\n2. Listing content...")
    try:
        content = await client.list_knowledge_content(headers=HEADERS)
        print(f"   Found {len(content.data)} content items")
        for item in content.data[:5]:
            print(f"   - {item.id}: {item.name if hasattr(item, 'name') else 'Unnamed'}")
    except Exception as e:
        print(f"   Error listing content: {e}")

    # Search knowledge base
    print("\n3. Searching knowledge base...")
    try:
        results = await client.search_knowledge(
            query="What is Agno?",
            limit=5,
            headers=HEADERS,
        )
        print(f"   Found {len(results.data)} results")
        for result in results.data:
            content_preview = str(result.content)[:100] if hasattr(result, "content") else "N/A"
            print(f"   - Reranking score: {result.reranking_score if result.reranking_score is not None else 'N/A'}")
            print(f"     Content: {content_preview}...")
    except Exception as e:
        print(f"   Error searching: {e}")


if __name__ == "__main__":
    asyncio.run(main())

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U "agno[os]" openai

Set the server model key

Set this key in the terminal that starts the example server. The client sends requests to AgentOS and does not call OpenAI directly.

export OPENAI_API_KEY="your_openai_api_key_here"

Start an AgentOS Server

Set OS_SECURITY_KEY in the client terminal to the same value used by the server. The code sends it as a bearer credential on each request.

export OS_SECURITY_KEY="your_os_security_key_here"

Start the client example server on port 7778. It registers the knowledge base used by this client.

Upload content

Upload at least one content item before searching the knowledge base.

Run the Client

python knowledge_search.py