Skip to main content

Shard is an open-source LLM tuning package for Python, which can turn any causal LLM into a specific character.

Project description

Shard

Make your own character LLM.

A Python package for fine-tuning large language models to behave like specific characters using LoRA.

Installation

  1. Install it with pip:
pip install shard-llm

Features

  • Fine-tune LLMs to mimic specific characters
  • Parameter-efficient training with LoRA
  • Support for 4-bit and 8-bit quantization
  • Easy conversion to Ollama-compatible formats
  • Simple API for dataset creation and response generation

Quick Start

from shard_ai import CharacterTuner, ResponseGenerator

# Initialize the tuner
tuner = CharacterTuner(
    model_name="meta-llama/Llama-3.1-8B-Instruct",
    output_dir="./sherlock-llama-lora"
)

# Create a character dataset
examples = [
    {
        "user": "What do you think about this case?",
        "assistant": "The facts, as presented, suggest a crime of passion rather than premeditation. Elementary deduction, really."
    },
    {
        "user": "Can you help me find my missing watch?",
        "assistant": "Observe the slight indentation on your right wrist, indicating you've worn the watch consistently until very recently. Have you checked the pocket of the jacket you wore during yesterday's garden excursion?"
    }
]

dataset_path = tuner.create_character_dataset(
    character_name="Sherlock Holmes",
    character_description="is a brilliant detective with exceptional deductive reasoning skills. You speak in a formal, precise manner, often making keen observations about details others miss.",
    examples=examples,
    output_file="sherlock_dataset.jsonl"
)

# Fine-tune the model
model, tokenizer = tuner.fine_tune(
    dataset_path=dataset_path,
    batch_size=2,
    num_epochs=3
)

# Generate responses with the fine-tuned model
generator = ResponseGenerator("./sherlock-llama-lora")
# Integrated generator for testing
response = generator.generate_response("Tell me about a case you solved recently")
print(response)

Converting to Ollama

from shard_ai import ModelConverter

# Merge LoRA weights with base model
merged_model_path = ModelConverter.merge_lora_weights(
    base_model_name="meta-llama/Llama-3.1-8B-Instruct",
    lora_model_path="./sherlock-llama-lora",
    output_dir="./sherlock-llama-merged"
)

# Create Ollama modelfile
modelfile_path = ModelConverter.create_ollama_modelfile(
    model_path=merged_model_path,
    model_name="sherlock-llama",
    system_prompt="You are Sherlock Holmes, a brilliant detective with exceptional deductive reasoning skills."
)

# Optional: Convert to GGUF format
gguf_path = ModelConverter.convert_to_gguf(
    model_path=merged_model_path,
    output_path="./sherlock-llama.gguf",
    quantization="f16"
)

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

shard_llm-1.0.4.tar.gz (169.3 kB view details)

Uploaded Source

Built Distribution

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

shard_llm-1.0.4-py3-none-any.whl (170.2 kB view details)

Uploaded Python 3

File details

Details for the file shard_llm-1.0.4.tar.gz.

File metadata

  • Download URL: shard_llm-1.0.4.tar.gz
  • Upload date:
  • Size: 169.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Windows/10

File hashes

Hashes for shard_llm-1.0.4.tar.gz
Algorithm Hash digest
SHA256 c4399e5d2a570c17667e5aa093cfcf857291bdc45f6fe5886be42913a337eb4a
MD5 b6841c787df308acb22f8dd722bcd9fa
BLAKE2b-256 3e6f1476b220d3241b0803afd3ee01205fab8a076b8b07c3033e2faf40ff4a2a

See more details on using hashes here.

File details

Details for the file shard_llm-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: shard_llm-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 170.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Windows/10

File hashes

Hashes for shard_llm-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 381b3f0797d90bd2e086ca2df4494fc55c527f93e6e694e13f8a684fdba559f1
MD5 0da7dac5d9baebb9bf1a96d37e623476
BLAKE2b-256 e82a01c654aa13277d06f53610ae8c5373e7dc7edfe99cc70a6fbf479bcf8c7e

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