Skip to main content

Official Python client library for NeuraLex API

Project description

NeuraLex Python Client

Official Python client library for the NeuraLex API.

Installation

pip install neuralex

Quick Start

from neuralex import NeuraLexClient

# Initialize client with your API key
client = NeuraLexClient(api_key="nlx_your_api_key")

# Generate embeddings
response = client.embed("Hello, world!")

# Access the embedding vector
embedding = response.payload[0].embedding
print(f"Dimensions: {len(embedding)}")
print(f"First 5 values: {embedding[:5]}")

Features

  • ✅ Simple and intuitive API
  • ✅ Synchronous and asynchronous support
  • ✅ Type hints and Pydantic models
  • ✅ Automatic error handling
  • ✅ Configurable semantic/term-based balance
  • ✅ Batch embedding support (up to 100 inputs)
  • ✅ BYOE (Bring Your Own Embedding) mode support

Usage

Basic Usage

from neuralex import NeuraLexClient

client = NeuraLexClient(api_key="nlx_your_api_key")

# Single text
response = client.embed("Machine learning is fascinating")
embedding = response.payload[0].embedding

Batch Embeddings

# Multiple texts
texts = [
    "First document",
    "Second document",
    "Third document"
]
response = client.embed(texts)

for item in response.payload:
    print(f"Text: {item.text}")
    print(f"Embedding dimensions: {len(item.embedding)}")
    print(f"Tokens used: {item.usage.total_tokens}")

Adjusting Semantic Weight

The semantic_weight parameter controls the balance between term-based and semantic embeddings:

  • 0.0 = Pure term-based (exact keyword matching)
  • 1.0 = Pure semantic (meaning-based)
  • 0.5 = Balanced (default)
# More term-focused (better for keyword search)
response = client.embed(
    "Python programming",
    semantic_weight=0.3
)

# More semantic-focused (better for meaning-based search)
response = client.embed(
    "Python programming",
    semantic_weight=0.8
)

Using Context Manager

# Automatically handles connection cleanup
with NeuraLexClient(api_key="nlx_your_api_key") as client:
    response = client.embed("Hello, world!")
    print(response.payload[0].embedding[:5])

Async Usage

import asyncio
from neuralex import AsyncNeuraLexClient

async def main():
    async with AsyncNeuraLexClient(api_key="nlx_your_api_key") as client:
        response = await client.embed(["Text 1", "Text 2", "Text 3"])
        for item in response.payload:
            print(f"{item.text}: {len(item.embedding)} dimensions")

asyncio.run(main())

BYOE (Bring Your Own Embedding) Mode

When the embed service is configured with BYOE=true, you can provide your own pre-computed embeddings instead of generating them server-side:

from neuralex import NeuraLexClient, EmbeddingInputData

client = NeuraLexClient(api_key="nlx_your_api_key")

# Create inputs with optional pre-computed embeddings
inputs = [
    # Provide your own embedding (must match server dimensions, typically 1024)
    EmbeddingInputData(text="hello world", embedding=[0.1] * 1024),
    # Or let the server compute the embedding
    EmbeddingInputData(text="server-computed text"),
]

response = client.embed(inputs)

Note: BYOE mode must be enabled on the server (BYOE=true). If BYOE is disabled, providing embeddings will result in an error.

Error Handling

from neuralex import NeuraLexClient, AuthenticationError, RateLimitError, APIError

client = NeuraLexClient(api_key="nlx_your_api_key")

try:
    response = client.embed("Hello, world!")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limit exceeded, please wait")
except APIError as e:
    print(f"API error: {e.message} (status: {e.status_code})")
except Exception as e:
    print(f"Unexpected error: {e}")

API Reference

NeuraLexClient

Main client class for synchronous API calls.

Methods:

  • embed(inputs, model="public", language="english", semantic_weight=0.5) - Generate embeddings

Parameters:

  • inputs (str | List[str] | EmbeddingInputData | List[EmbeddingInputData]): Text or list of texts/EmbeddingInputData to embed (max 100)
  • model (str, optional): Model name (default: "public")
  • language (str, optional): Language for lexeme extraction (default: "english")
  • semantic_weight (float, optional): Balance between term (0.0) and semantic (1.0) (default: 0.5)

Returns: EmbeddingResponse object

AsyncNeuraLexClient

Async client class for asynchronous API calls. Same interface as NeuraLexClient but all methods are async.

Models

EmbeddingInputData

  • text (str): Text to embed (required)
  • embedding (List[float] | None): Pre-computed embedding vector (optional, for BYOE mode)

EmbeddingResponse

  • payload (List[EmbeddingData]): List of embedding results
  • model (str): Model name used
  • total_usage (Usage): Total token usage across all inputs

EmbeddingData

  • text (str): Original input text
  • embedding (List[float]): Vector embedding
  • usage (Usage): Token usage for this input

Usage

  • total_tokens (int): Total tokens processed

Getting an API Key

  1. Sign up at app.neuralex.ca
  2. Generate a new API key
  3. Keep your API key secure and never commit it to version control

License

MIT License - see LICENSE file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neuralex-0.2.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neuralex-0.2.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file neuralex-0.2.0.tar.gz.

File metadata

  • Download URL: neuralex-0.2.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for neuralex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ae1423aa60a3ca5bc9e0c0e2392835911c597f08541f3c90ed892e3789a48d2f
MD5 fb4e32640a5e3a1f9403b5f9b7c7a107
BLAKE2b-256 9bdd03a2166ab8bd4ff558c02bed4dfe24939091712e7a09bb90cf01a8f25f1a

See more details on using hashes here.

File details

Details for the file neuralex-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: neuralex-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for neuralex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 afff147eece88cd4832609f200aa63d435227eb3e4b1988fd7bf36f6451d115e
MD5 25d0b08e35495f2e24472a24501b0455
BLAKE2b-256 d0c64beb860f7a627964c03aa7735f6046c37480d74bd5fb40bd86c16bb37fc7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page