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.3.tar.gz (16.4 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.3-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: neuroprompt-0.1.3.tar.gz
  • Upload date:
  • Size: 16.4 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.3.tar.gz
Algorithm Hash digest
SHA256 b9a7293c9cc6e847e3d69defa55645152714a34457bb4581828972ca44e59613
MD5 60bacaad3b7ed149687af59b13e88f0e
BLAKE2b-256 e26263f3b2f3e3da1103528f4add5bc56aa28622593a871d41f904bc737b7e02

See more details on using hashes here.

File details

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

File metadata

  • Download URL: neuroprompt-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b6afb1cce74e77ef22c1335d8b95bb0603a6da83af09472498daf68f3eb413cc
MD5 8fa131a8488cea78c1f40d719ec91676
BLAKE2b-256 4ca709d5b5f25ceb92ec795080a2f81dc560b04d8f4421033eaa083d265ab440

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