Skip to main content

Welcome to Moorcheh Python SDK

The Moorcheh Python SDK provides a simple and intuitive interface to integrate Moorcheh’s powerful semantic search and generative AI capabilities into your Python applications. With full API coverage, automatic authentication handling, and robust error management, the SDK makes it easy to build intelligent applications.

Installation

Install the SDK using pip:
pip install moorcheh-sdk

Quick Start

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):
from moorcheh_sdk import MoorchehClient

with MoorchehClient(api_key="your-api-key-here") as client:
    # Your code here
    pass

Basic Example

Here’s 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.

SDK Structure

The SDK is organized into logical modules that mirror the API structure:

Basic Workflow

1

Initialize Client

Create a MoorchehClient instance, preferably using a context manager
2

Create a Namespace

Create a namespace to organize your data (text or vector type)
3

Upload Data

Upload text documents or vector embeddings to your namespace
4

Search

Perform semantic search to retrieve relevant data
5

Generate Answers

Use AI models to generate contextual answers from your data

Client Initialization

The MoorchehClient is the main entry point to the SDK. Learn more about client initialization and configuration.

Next Steps