Promptsage helps you combine all the components of your prompt into a single LLM-ready payload
Project description
Promptsage
Promptsage is an LLM prompt builder with AI safety guardrails. Apply fine-grained access control over the context, and prevent unwanted behavior with filters. Compatible with all major datastores, LLMs and gateways.
Quick Install
With pip:
pip install promptsage
Examples
from promptsage import text_prompt, load_examples
from promptsage.sources import PDF, LangchainDocuments
from promptsage.filters import PromptInject, Anonymize, TrimTokens
prompt = text_prompt(
"Summarize the following case file and provide references from the datastore"
examples=load_examples("./icl_examples.json")
sources=[
PDF('case_file.pdf'),
LangchainDocuments(fetch_from_store())
],
filters=[
PromptInject(provider="local"),
Anonymize(provider="local"),
TrimTokens(from="examples", max=10000)
]
)
# Use prompt with the openai SDK
from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="gpt-4",
messages=prompt.to_openai_messages(),
)
# Or, use the prompt with langchain
from langchain_openai import ChatOpenAI
model = ChatOpenAI()
chain = prompt.to_langchain_messages() | model
chain.invoke()
Access Control
By adding a user_id field to text_prompt, promptsage can apply access control to each individual Source
At the moment, authorization is performed via user_id matching, with support for groups(LDAP, SSO) and ACL/RBAC in the roadmap
From our unit tests:
sources = [
EchoSource("User 1 knows that the password is XXX", "user1"),
EchoSource("User 2 knows that the password is YYY", "user2"),
EchoSource("User 3 knows that the password is ZZZ", "user3"),
]
with pytest.raises(UnauthorizedError):
text_prompt(
"What do I know?"
sources=sources,
user_id="user2",
)
Instead of raising an exception, you may also exclude unauthorized sources:
text_prompt(
"What do I know?"
sources=sources,
user_id="user2",
access_control_policy=AccessControlPolicy.skip_unauthorized,
)
Datastores
While not implemented yet, when a Source fetches data from an external datastore the prompt's user_id can be used to fetch only the data applicable to the user, per the datastore configuration.
Basic Concepts
A typical LLM invocation is comprised of three parts:
- The
user prompt, instructions for the LLM - The
context, all accompanying data necessary for the LLM to complete generation - The
examples, for in-context learning
promptsage introduces the following contexts
- A
Sourceis a chunk of text within the context - A
Filteris applied over the entire prompt to catch undesired inputs - A
Promptis a sanitized prompt ready to be used with the target LLM
Current Features
- Compatible with openai SDK and langchain formats
- Access control only applies with user_id matching
- Filters: Prompt Injection
Roadmap
- llama_index support
- Acess Control: LDAP & SSO integration
- Vectorstores: Langchain, Pinecone, Milvus
- Filters:
- PII anonymization
- Templates:
- Prompt compression
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file promptsage-0.0.1.tar.gz.
File metadata
- Download URL: promptsage-0.0.1.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.9.6 Darwin/23.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
717901dcaa85c300801e7d2c8c3abc0f926a9b458291e6a3ea498f9afa32702d
|
|
| MD5 |
c5f76200b413b24a541f003ab68c37f1
|
|
| BLAKE2b-256 |
cbfd7b4fe978db3b8926bfbe142d2ace065daac4382e3e8d82d72b18e02358bb
|
File details
Details for the file promptsage-0.0.1-py3-none-any.whl.
File metadata
- Download URL: promptsage-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.9.6 Darwin/23.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18ed316d711b2c8f240a4be4ce7c19c85610c6e825cf0726fee26bd256a29dae
|
|
| MD5 |
13d5c2e44ce7fb8d3f5b717405632255
|
|
| BLAKE2b-256 |
f265e1916f8fd005846471b2460437d5d4d3de6433d6ddbbda8c62f9bbc181e0
|