Package for generating Prompt Stability Score (PSS). PSS estimates the stability of outcomes resulting from variations in language model prompt specifications.
Project description
promptstability
Package for generating Prompt Stability Scores (PSS). See paper here outlining technique for investigating the stability of outcomes resulting from variations in language model prompt specifications.
Requirements
- Python 3.8 to 3.10 (Python 3.11 and above are not supported due to dependency limitations)
- Other dependencies are installed automatically via
pip
Installation
TestPypi installation
Install this library using pip
:
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple promptstability
Pypi installation (pending upload)
Install this library using pip
:
pip install promptstability
Usage
OpenAI example
from openai import OpenAI
import pandas as pd
from promptstability.core import get_openai_api_key
from promptstability.core import PromptStabilityAnalysis
from promptstability.core import load_example_data
import os
print("OPENAI_API_KEY:", os.environ.get('OPENAI_API_KEY'))
#This script mimics a user run-through of package use
# Load data
df = load_example_data()
print(df.head())
# Take a subsample
example_data = list(df['body'].values)
# Initialize OpenAI client
client = OpenAI(api_key=get_openai_api_key()) #Will get an error if no API key set as environment variable
# Enter in terminal: export OPENAI_API_KEY='your-api-key-here'
# OR (not advised) hard code it with:
os.environ['OPENAI_API_KEY'] = 'your-api-key-here'
# Initialize OpenAI client
client = OpenAI(api_key=get_openai_api_key())
# Define the annotation function
def annotate(text, prompt, temperature=0.1):
try:
response = client.chat.completions.create(
model='gpt-4o-mini',
temperature=temperature,
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": text}
]
)
except Exception as e:
print(f"Caught exception: {e}")
raise e
return ''.join(choice.message.content for choice in response.choices)
psa = PromptStabilityAnalysis(annotation_function=annotate, data=example_data)
# Construct the prompt
original_text = 'The following are some news articles about the economy.'
prompt_postfix = '[Respond 0 for positive news, or 1 for negative news. Guess if you do not know. Respond nothing else.]'
# Run intra_pss (aka within-prompt PSS)
ka_scores, annotated_data = psa.intra_pss(original_text, prompt_postfix, iterations=20, plot=True, save_path='news_within.png', save_csv="news_within.csv")
# Run inter_pss (aka between-prompt PSS)
# Set temperatures (in practice, you would set more temperatures than this)
temperatures = [0.1, 5.0]
# Get KA scores across different temperature paraphrasings
ka_scores, annotated_data = psa.inter_pss(original_text, prompt_postfix, nr_variations=10, temperatures=temperatures, iterations = 1, print_prompts=True, plot=True, save_path='news_between.png', save_csv = 'news_between.csv')
Ollama annotation function example
import ollama
MODEL = 'llama3'
def annotate(text, prompt, temperature=0.1):
response = ollama.chat(model=MODEL, messages=[
{"role": "system", "content": f"'{prompt}'"},
{"role": "user", "content": f"'{text}'"}
])
return response['message']['content']
Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd promptstability
python -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file promptstability-0.1.0.tar.gz
.
File metadata
- Download URL: promptstability-0.1.0.tar.gz
- Upload date:
- Size: 39.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0e0fd91509f8558d886de214dbb2328ab631e7de428e55800ae08fe4df3bdd2 |
|
MD5 | b87a89d633b4a859558a5281585c2649 |
|
BLAKE2b-256 | 9f7bb9fefef6e733508fbcd65055096f6b95ca492f8b2b13ad05dde0aea55fd6 |
File details
Details for the file promptstability-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: promptstability-0.1.0-py3-none-any.whl
- Upload date:
- Size: 38.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29bd865b8704b18bbd3a4ae83a3eed0fed023fc33f572cc9017bfc62a67a2531 |
|
MD5 | 173c9ec9b46fb68448e96cba5ab2bc0b |
|
BLAKE2b-256 | f396bfd9f3e655b27fb5fc573c3727ac9913e9593dd218d297aa4235a120d35d |