Skip to main content

A smart prompt compression and optimization tool for LLMs

Project description

NeuroPrompt

A smart prompt compression and optimization tool for Large Language Models that automatically adapts to different types of content and provides comprehensive quality evaluation.

Installation

To install NeuroPrompt, follow these steps:

  1. Set up your OpenAI API key. NeuroPrompt relies on OpenAI services, so make sure you have access to the OpenAI API.
export OPENAI_API_KEY=<your_openai_key>
  1. Install NeuroPrompt via pip:
pip install neuroprompt

You can find more information about OpenAI API keys here.

Features

  • Smart Prompt Compression: Compresses prompts while retaining the core information, optimizing token usage.
  • Content-Aware Parameter Optimization: Automatically adapts to different types of content like code, technical lists, and text.
  • Comprehensive Response Quality Evaluation: Evaluates the quality of compressed responses using standard metrics.
  • Cost Optimization for OpenAI API Calls: Helps reduce the cost of API calls by minimizing token counts.
  • Automatic Token Counting and Cost Estimation: Estimates the number of tokens and calculates associated costs.

Quick Start

To start using NeuroPrompt, follow the example below to implement a basic prompt compression decorator.

from neuroprompt import NeuroPromptCompress
from openai import OpenAI

@NeuroPromptCompress()
def chat_completion(messages, model="gpt-4o", temperature=0.7):
    client = OpenAI()
    return client.chat.completions.create(
        messages=messages,
        model=model,
        temperature=temperature
    )

# Example usage
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Your prompt here..."}
]

response = chat_completion(messages=messages)
print(response)

Documentation

Basic Usage

NeuroPrompt provides two main decorators that help you utilize prompt compression and quality evaluation features:

  1. NeuroPromptCompress: Use this for basic prompt compression without any extra evaluation. It helps to optimize token usage and cost without checking response quality.
  2. NeuroPromptCompressWithEval: This decorator compresses prompts while also evaluating the compressed response's quality. It provides detailed insights into metrics such as relevance, coherence, and accuracy.

Advanced Features

Quality Metrics

The quality of compressed responses is measured using various standard metrics:

  • ROUGE Scores: Measures the overlap of key elements between original and compressed responses.
  • BLEU Score: Evaluates the similarity of the compressed response to the original text based on word overlap.
  • Semantic Similarity: Uses embeddings to calculate the semantic consistency of the compressed response.
  • Information Coverage: Assesses whether critical information is retained in the compressed output.
  • Expert Evaluation (using GPT-4o): Applies GPT-4o to evaluate the quality of the response in terms of accuracy, completeness, and coherence.

Examples

Compressing a Chat with Evaluation

To use NeuroPrompt with evaluation metrics:

from neuroprompt import NeuroPromptCompressWithEval
from openai import OpenAI

@NeuroPromptCompressWithEval()
def chat_completion(messages, model="gpt-4o", temperature=0.7):
    client = OpenAI()
    return client.chat.completions.create(
        messages=messages,
        model=model,
        temperature=temperature
    )

# Example usage with evaluation
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Explain quantum computing in simple terms."}
]

response = chat_completion(messages=messages)
print(response)

The above code will compress the prompt and provide comprehensive quality metrics, ensuring that the response remains relevant and complete.

Cost Optimization

One of NeuroPrompt's key features is reducing token usage, which directly impacts the cost of using models like GPT-4. By reducing prompt size, NeuroPrompt helps make OpenAI API usage more affordable.

License

Copyright © 2024 Tejas Chopra.

All rights reserved.

This is proprietary software. Unauthorized copying, modification, distribution, or use of this software, in whole or in part, is strictly prohibited.

Third-Party Components

This software uses LLMLingua under the MIT license. See the LICENSE file for full terms.

For more details about MIT licensing, visit the Open Source Initiative.

Support and Contributions

We welcome contributions to NeuroPrompt! If you have suggestions or find bugs, please feel free to email chopratejas@gmail.com

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

neuroprompt-0.1.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

neuroprompt-0.1.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file neuroprompt-0.1.1.tar.gz.

File metadata

  • Download URL: neuroprompt-0.1.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for neuroprompt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4f53617b2752b47e221618ee7e45a920c929826fe9d48408a4fd72c400be9254
MD5 15aa001c15254575cb452693a419e311
BLAKE2b-256 4e1d2b2f490ed7db5dbd5880d97ae3769c27b2a5b1db7bb9153958db6b1d7f28

See more details on using hashes here.

File details

Details for the file neuroprompt-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: neuroprompt-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for neuroprompt-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e22a36faf96962fd8d6d9d181a05c78f986884837acec4d3e1376010b3d7afea
MD5 24798878628a27048e8d159d2e714427
BLAKE2b-256 df4f661ebfdbbfe0390d2a95ee599fbc8b73b6d301cd5c9e0d3a6df9ba56b1b8

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