A package to validate prompts using an external API.
Project description
Midfield Documentation
Introduction
Midfield is a Python package designed to validate prompts using an external API. It ensures the quality and reliability of text outputs from large language models (LLMs) by providing a suite of pre-built and custom validators.
Features
- Easy integration with any Python application.
- Validators are associated with the API key and managed through the Midfield website.
- User-friendly interface to validate prompts.
Installation
You can install the midfield package via pip:
pip install midfield
Usage
Step 1: Obtain an API Key
To use Midfield, you need an API key. Sign up at midfield.ai and obtain your API key. During the signup process, you will associate the necessary checks with your API key.
Step 2: Initialize the Package
from midfield import Midfield
midfield = Midfield(api_key='your-api-key')
Step 3: Validate the Prompt
Use the validate method to validate your prompt. The associated checks will be retrieved automatically based on your API key.
prompt = "Your prompt text here"
try:
result = midfield.validate(prompt)
print(result)
except Exception as e:
print(f"Validation failed: {e}")
Example Responses
Invalid API Key
If the API key provided is invalid, you will get the following error:
Validation failed: Error: Provided API KEY is invalid
Validation Error
If the prompt does not pass the validation checks associated with the API key, you will get an error message similar to the following:
Validation failed: Error: Validation failed for field with errors: Result must match \(?\d{3}\)?-? *\d{3}-? *-?\d{4}
Successful Validation
If the prompt passes all the validation checks, you will get a success message:
{
"message": "Prompt validated successfully!"
}
Detailed Documentation
For more detailed documentation, visit midfield.ai/docs.
Examples
Basic Example
from midfield import Midfield
midfield = Midfield(api_key='your-api-key')
prompt = "Call me at 123-456-7890."
try:
result = midfield.validate(prompt)
print(result)
except Exception as e:
print(f"Validation failed: {e}")
API Reference
The midfield package provides the following main components:
Midfield: The main class to initialize with your API key and use for validating prompts.
Midfield Class
Midfield(api_key: str): Initializes the Midfield instance with the provided API key.validate(prompt: str): Validates the given prompt. The checks associated with the API key will be used automatically.
Available Validators
Midfield supports a wide range of validators. Here is a detailed list of available validators and their parameters:
-
regex_match
- Parameters:
regex(str) - Description: Validates that the input matches the specified regular expression.
- Parameters:
-
toxic_language
- Parameters:
validation_method(str, optional),threshold(float, optional) - Description: Checks for toxic language in the input.
- Parameters:
-
mentions_drugs
- Parameters: None
- Description: Checks if the input mentions drugs.
-
competitor_check
- Parameters:
competitors(list of str) - Description: Checks if the input mentions any competitors from the specified list.
- Parameters:
-
valid_length
- Parameters:
min(int, optional),max(int, optional) - Description: Validates that the input length is within the specified range.
- Parameters:
-
valid_url
- Parameters: None
- Description: Checks if the input is a valid URL.
-
valid_json
- Parameters: None
- Description: Checks if the input is a valid JSON string.
-
valid_python
- Parameters: None
- Description: Checks if the input is valid Python code.
-
valid_sql
- Parameters: None
- Description: Checks if the input is valid SQL.
-
web_sanitization
- Parameters: None
- Description: Sanitizes the input to prevent web vulnerabilities.
-
valid_address
- Parameters: None
- Description: Checks if the input is a valid address.
-
unusual_prompt
- Parameters: None
- Description: Checks for unusual prompts.
-
sql_column_presence
- Parameters:
columns(list of str) - Description: Ensures the presence of specified columns in SQL.
- Parameters:
-
responsiveness_check
- Parameters: None
- Description: Checks for responsiveness.
-
regex_match
- Parameters:
regex(str) - Description: Validates that the input matches the specified regular expression.
- Parameters:
-
reading_time
- Parameters: None
- Description: Estimates reading time for the input.
-
quotes_price
- Parameters: None
- Description: Checks for quoted prices in the input.
-
one_line
- Parameters: None
- Description: Ensures the input is a single line of text.
-
lower_case
- Parameters: None
- Description: Ensures the input is in lower case.
-
has_url
- Parameters: None
- Description: Checks if the input contains a URL.
-
exclude_sql_predicates
- Parameters: None
- Description: Ensures the input does not contain SQL predicates.
-
endpoint_is_reachable
- Parameters: None
- Description: Checks if a given endpoint is reachable.
-
similar_to_document
- Parameters: None
- Description: Checks if the input is similar to a given document.
-
saliency_check
- Parameters: None
- Description: Checks for salient information in the input.
-
relevancy_evaluator
- Parameters: None
- Description: Evaluates the relevancy of the input.
-
provenance_llm
- Parameters: None
- Description: Checks for provenance in LLM-generated text.
-
profanity_free
- Parameters: None
- Description: Ensures the input is free from profanity.
-
logic_check
- Parameters: None
- Description: Checks the logical consistency of the input.
-
gibberish_text
- Parameters: None
- Description: Checks if the input is gibberish.
-
extracted_summary_sentences_match
- Parameters: None
- Description: Validates that extracted summary sentences match the input.
-
detect_pii
- Parameters: None
- Description: Detects personally identifiable information (PII) in the input.
Conclusion
The midfield package simplifies the process of validating prompts. By following the steps and examples provided, you can easily integrate it into your Python applications to ensure the quality and reliability of your LLM outputs.
For more information and detailed documentation, visit midfield.ai/docs. If you have any questions or need further assistance, feel free to reach out to our support team.
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 midfield-0.1.0.tar.gz.
File metadata
- Download URL: midfield-0.1.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4a15c27bf194667ffdef574c1f870568e2744b4007b654fad8b72bf8017ea5f
|
|
| MD5 |
2795acda7c8ef588dd852a1905fcacc4
|
|
| BLAKE2b-256 |
1422bcbbc43bffe55eea8a0a570a111d248ab4d428ecb05b8c732d0cc50bc760
|
File details
Details for the file midfield-0.1.0-py3-none-any.whl.
File metadata
- Download URL: midfield-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27167d2a360b54ee5575416ad55eb66ae807d7adf4cf25a1bab63e591cf5c864
|
|
| MD5 |
28524fc280a9d0f685f815734d531d4e
|
|
| BLAKE2b-256 |
2d663489d45c8da6d6768af2b9b93fdc5e158fff8c5d3c4a5a0d53f806b33d7f
|