Skip to main content

Streamlined Prompt Automation and Recommender Tool (SPART): A Prompt Recommender System for Accurate Content Generation

Project description

SPART: Streamlined Prompt Automation and Recommendation Tool

SPART is a Python package designed to simplify prompt engineering for Large Language Models (LLMs). It provides tools for generating, evaluating, and optimizing prompts automatically, making the process of creating effective prompts more efficient and less manual.

Features

  • Connect to various LLM providers: Use OpenAI, Cohere, HuggingFace, or local LLMs through Ollama.
  • Prompt recommendation: Generate effective prompts based on input-output examples.
  • Prompt evaluation: Test prompts against both semantic and syntactic similarity metrics.
  • Prompt optimization: Improve existing prompts by applying good prompt engineering practices.
  • Multithreaded evaluation: Process multiple inputs concurrently for faster testing.

Installation

To install SPART, run the following command:

pip install spart-prompt

Quick Start

Here's a quick example to get you started with PromptRecommender:

from spart_prompt import ExternalLLMConnector, PromptRecommender
import pandas as pd

# Connect to your LLM provider
llm = ExternalLLMConnector(
    provider="openai",
    model_name="gpt-4o",
    api_key="your_api_key",
    temperature=0.7
)

# Create a recommender
recommender = PromptRecommender(llm)

# Example data 
# (inputs must be the first column and desired_outputs the second column)
examples = pd.DataFrame({
    'inputs': [
        "My name is John and I am 30 years old",
        "Hello I'm Emma, age 25",
        "Name's Sarah, I'm 29",
        "I go by Alice, and I’m 22",
        "It’s Tom here, aged 31",
        "You can call me Linda, I’m 27",
        "Hey, this is Mark and I’m 38"
    ],
    'desired_outputs': [
        "Name: John, Age: 30",
        "Name: Emma, Age: 25",
        "Name: Sarah, Age: 29",
        "Name: Alice, Age: 22",
        "Name: Tom, Age: 31",
        "Name: Linda, Age: 27",
        "Name: Mark, Age: 38"
    ]
})

# Get a recommended prompt
results = recommender.recommend(
    examples=examples, # The input-output examples
    num_examples=1, # Use first example for training, rest for testing
    context="Input contains a short sentence with a person's name and age. Extract both in the format 'Name: X, Age: Y'.", # Extra context for the LLM
    similarity_threshold=0.85, # Threshold to reach before recommending
    max_iterations=3, # If threshold isn't reached then optimization will be attempted 3 times
    semantic_similarity=False, # Don't evaluate based on semantics (outputs -1 if off)
    syntactic_similarity=True # Do evaluate based on syntax
)

print(f"Recommended prompt: {results['recommended_prompt']}")
print(f"Prompt outputs: {results['prompt_outputs']}")
print(f"Semantic similarity: {results['semantic_similarity']}")
print(f"Syntactic similarity: {results['syntactic_similarity']}")

Here's a quick example to get you started with PromptOptimizer:

from spart_prompt import ExternalLLMConnector, PromptOptimizer
import pandas as pd

# Connect to your LLM provider
llm = ExternalLLMConnector(
    provider="openai",
    model_name="gpt-4o",
    api_key="your_api_key",
    temperature=0.7
)

# Create an optimizer
optimizer = PromptOptimizer(llm)

# Example data 
examples = pd.DataFrame({
    'inputs': [
        "My name is John and I am 30 years old",
        "Hello I'm Emma, age 25",
        "Name's Sarah, I'm 29",
        "I go by Alice, and I’m 22",
        "It’s Tom here, aged 31",
        "You can call me Linda, I’m 27",
        "Hey, this is Mark and I’m 38"
    ],
    'desired_outputs': [
        "Name: John, Age: 30",
        "Name: Emma, Age: 25",
        "Name: Sarah, Age: 29",
        "Name: Alice, Age: 22",
        "Name: Tom, Age: 31",
        "Name: Linda, Age: 27",
        "Name: Mark, Age: 38"
    ]
})

# Initial prompt to optimize
prompt = "Extract the person's name and age from the text."

# Optimize the prompt
results = optimizer.optimize_prompt(
    system_prompt=prompt,
    examples=examples,
    num_examples=1,
    threshold=0.9,
    context="Input contains a short sentence with a person's name and age.",
    max_iterations=3,
    semantic_similarity=False,
    syntactic_similarity=True
)

print(f"Optimized prompt: {results['optimized_prompt']}")
print(f"Prompt outputs: {results['prompt_outputs']}")
print(f"Semantic similarity: {results['semantic_similarity']}")
print(f"Syntactic similarity: {results['syntactic_similarity']}")

Classes Overview

LLMConnector (Abstract Base Class)

Base interface for connecting to LLMs with the following implementations:

  • ExternalLLMConnector: Connect to OpenAI, Cohere, or HuggingFace
  • LocalLLMConnector: Connect to local LLMs through Ollama

PromptEvaluator

Evaluates prompts using:

  • Semantic similarity (using vector embeddings)
  • Syntactic similarity (using ROUGE-L score)

PromptRecommender

Generates system prompts based on input-output examples, structured with:

  • Role definition
  • Guidelines
  • Instructions
  • Examples
  • Context
  • Goal

PromptOptimizer

Improves existing prompts by applying prompt engineering best practices - architecture similar to PromptRecommender.

Use Cases

  • Data transformation: Generate prompts to convert data from one format to another.
  • Text summarization: Build prompts that produce consistent summary formats.
  • Structured output generation: Ensure LLM outputs follow specific formats.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the 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

spart_prompt-1.0.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

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

spart_prompt-1.0.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file spart_prompt-1.0.0.tar.gz.

File metadata

  • Download URL: spart_prompt-1.0.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for spart_prompt-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8de2d84a818346f16a90c2b0a850e17a9cf3bb7b6326eb066afe50a496f45c34
MD5 881f8112aafba6785df9b3810d4c5d29
BLAKE2b-256 63a9df0cae8aca6f0c6619d1234ddfb1c3415aaed263f6687ccbb8561ed31107

See more details on using hashes here.

Provenance

The following attestation bundles were made for spart_prompt-1.0.0.tar.gz:

Publisher: test_and_publish.yml on FaizAzizAtK/SPART

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spart_prompt-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: spart_prompt-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for spart_prompt-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c1ee0f1f6c447196ad28e5f3eb145d16b85c440ea29e89b8f81bfcaf18325cd
MD5 cb3761f4984d11d7254d438615d78bab
BLAKE2b-256 aaceb2f516c6bbd485865c66dbf860bbfc5bae0937aee401884321eabea569e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for spart_prompt-1.0.0-py3-none-any.whl:

Publisher: test_and_publish.yml on FaizAzizAtK/SPART

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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