Skip to main content
Welcome to the official reference documentation for the Moorcheh Python SDK. This guide provides everything you need to integrate Moorcheh’s powerful semantic search and generative AI capabilities into your Python applications.

Quick Start

Installation

Install the SDK using pip:
Installation
pip install moorcheh-sdk

Authentication

Get your API key from console.moorcheh.ai/api-keys. Set your API key as an environment variable:
export MOORCHEH_API_KEY="your_api_key_here"
Or pass it directly to the client (not recommended for production).

Example Workflow

Here is a complete example showing how to create a namespace, upload documents, search, and generate an answer.
import time
from moorcheh_sdk import MoorchehClient

# Use the context manager for automatic cleanup
with MoorchehClient() as client:
    namespace = "sdk-quickstart-ns"
    
    # 1. Create a text namespace
    print(f"Creating namespace: {namespace}")
    client.namespaces.create(namespace_name=namespace, type="text")

    # 2. Upload documents
    docs = [
        {"id": "qs-doc-1", "text": "Moorcheh is a semantic search engine designed for speed and accuracy."},
        {"id": "qs-doc-2", "text": "The Python SDK allows developers to easily integrate Moorcheh's features."}
    ]
    print("Uploading documents...")
    client.documents.upload(namespace_name=namespace, documents=docs)
    
    # Wait briefly for indexing (asynchronous process)
    time.sleep(5)

    # 3. Perform a semantic search
    print("Searching...")
    search_results = client.similarity_search.query(
        namespaces=[namespace], 
        query="What is the SDK?"
    )
    for result in search_results.get('results', []):
        print(f"Found: {result['text']} (Score: {result['score']})")

    # 4. Generate an AI answer
    print("Generating answer...")
    gen_ai_response = client.answer.generate(
        namespace=namespace,
        query="In one sentence, what is the purpose of the Python SDK?"
    )
    print(f"AI Answer: {gen_ai_response['answer']}")

    # 5. Clean up
    print("Deleting namespace...")
    client.namespaces.delete(namespace)
If you are using Moorcheh v1.2.2 or earlier, you can check the legacy MoorchehClient.

Key Features

Full API Coverage

Manage namespaces, ingest data, perform searches, and generate AI answers.

Simplified Authentication

Automatic handling of API keys via environment variables.

Robust Error Handling

Custom exceptions for specific API errors, allowing for graceful failure and recovery.

Context Management

The client is designed to be used as a context manager (with statement) for efficient handling of network resources.

Next Steps