Skip to main content

A shared library for common utilities.

Project description

Nitro

A shared library for common utilities, including LLM management.

Installation

pip install nitrotools

For LLM functionality with LangChain and OpenAI support:

pip install nitrotools[llm]

Usage

Core Utilities

from nitro.core import hello

print(hello())

LLM Management

Nitro provides a configurable LLM factory for easy integration with multiple providers.

Setup

  1. Copy the sample config and customize it:
cp /path/to/site-packages/nitro/llm_config.yaml.sample llm_config.yaml
# Or create llm_config.yaml in your project root (or set NITRO_CONFIG_PATH env var)

Example llm_config.yaml:

servers:
  llamacpp:
    - endpoint: "http://your-llamacpp-server:11435"
      interface: "langchain"
      models:
        - name: "qwen"
          model_name: "gpt-oss-20b-Q6_K.gguf"
          temperature: 0.7
          max_tokens: 1000
  openrouter:
    endpoint: "https://openrouter.ai/api/v1"
    api_key: "${OPENROUTER_API_KEY}"
    interface: "openai_compatible"
    headers:
      HTTP-Referer: "${OPENROUTER_HTTP_REFERER}"
      X-Title: "${OPENROUTER_X_TITLE}"
    models:
      - name: "grok-fast-free"
        model: "x-ai/grok-4-fast:free"
        temperature: 0.3
        max_tokens: 1000
      - name: "nemotron-nano-free"
        model: "nvidia/nemotron-nano-9b-v2:free"
        temperature: 0.1
        max_tokens: 1500

purposes:
  general: "llamacpp:qwen"
  coding: "llamacpp:qwen"
  reasoning: "openrouter:grok-fast-free"
  analysis: "openrouter:nemotron-nano-free"
  assistant: "openrouter:grok-fast-free"
  1. Set environment variables in .env:
OPENROUTER_API_KEY=your_key_here
OPENROUTER_HTTP_REFERER=https://your-site.com
OPENROUTER_X_TITLE=Your App
LLAMACPP_BASE_URL=http://localhost:11435

Basic Usage

from nitro import get_llm

# Get LLM for a purpose
llm = get_llm("coding")

# Generate text
response = llm.generate([
    {"role": "user", "content": "Write a Python function to reverse a string."}
])
print(response)

# Full chat response
full_response = llm.chat([
    {"role": "user", "content": "Explain recursion."}
])
print(full_response)  # Raw response object

Advanced Usage

from nitro import LLMFactory

factory = LLMFactory()

# Health check
status = factory.health_check()
print(status)  # {'general': '✅ llamacpp:qwen', ...}

# Direct factory usage
llm = factory.get_llm("assistant")

JSON Generation

Nitro supports structured JSON generation with automatic repair for malformed responses. This is useful for generating structured data like quizzes, profiles, or any custom JSON format.

from nitro import get_llm

# Get an LLM instance
llm = get_llm("reasoning")

# Generate a quiz in JSON format
quiz_prompt = """Create a quiz of 3 questions on current Indian affairs.
Each question should have:
- question: the question text
- options: array of 4 possible answers
- correct_option: index (0-3) of the correct answer

Return as a JSON array of question objects."""

quiz_data = llm.generate_json(quiz_prompt)
print(quiz_data)
# Output: [{'question': '...', 'options': [...], 'correct_option': 0}, ...]

# Generate with JSON schema guidance
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "skills": {"type": "array", "items": {"type": "string"}}
    },
    "required": ["name", "age"]
}

profile = llm.generate_json("Generate a software engineer profile", json_schema=schema)
print(profile)
# Output: {'name': 'John Doe', 'age': 30, 'skills': ['Python', 'JavaScript']}

# Custom role (optional)
todo_data = llm.generate_json("Generate 3 todo items with priorities", role="user")
print(todo_data)

Note: JSON generation requires the json-repair package, which is included in pip install nitrotools[llm]. The method automatically repairs any malformed JSON from the LLM response.

Testing with Real LLMs

To run integration tests with real endpoints:

  1. Set credentials in .env as above.
  2. Run tests: pytest tests/test_llm.py::TestLLMIntegration -v

Tests will skip if credentials are not provided.

Contributing

See CONTRIBUTING.md for detailed contribution guidelines.

License

MIT

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

nitrotools-0.2.0.tar.gz (84.8 kB view details)

Uploaded Source

Built Distribution

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

nitrotools-0.2.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nitrotools-0.2.0.tar.gz
  • Upload date:
  • Size: 84.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for nitrotools-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e829820038939147c3e92610eafb2b687753684a50300f07656fbbb2b661f9bd
MD5 44219c6b053027ee6a294136cd16b2da
BLAKE2b-256 0a8d404485495feb0cd1c654dc0e59396531a812dd8e72634836aac079066397

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nitrotools-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for nitrotools-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78271450bfdff2269090e46cc5211467ed403e8a359f59a80a656889fb7276b6
MD5 aed9c17fead63f44bd8a7c2168ce72cb
BLAKE2b-256 924a40d751f3367166cb89057256206a1ba93f9b0dd0666254bf9e94d3dc1e81

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