Skip to content

Validator Versioning #1140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/concepts/remote_validation_inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"\n",
":::note\n",
"Remote validation inferencing is only available in Guardrails versions 0.5.0 and above.\n",
":::\n",
"\n",
"::note\n",
"To enable/disable, you can run the cli command `guardrails configure` or modify your `~/.guardrailsrc`. For example, to disable remote inference use: `use_remote_inferencing=false`.\n",
":::"
]
},
Expand Down Expand Up @@ -147,7 +151,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -161,9 +165,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
64 changes: 62 additions & 2 deletions docs/how_to_guides/continuous_integration_continuous_deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ terraform apply -var="aws_region=us-east-1" -var="backend_memory=2048" -var="bac
Once the deployment has succeeded you should see some output values (which will be required if you wish to set up CI).


## Step4: Deploying Guardrails API
## Step 4: Deploying Guardrails API
### Manual

Firstly, create or use your existing guardrails token and export it to your current shell `export GUARDRAILS_TOKEN="..."`
Expand Down Expand Up @@ -444,7 +444,67 @@ By setting the above environment variable `GUARDRAILS_BASE_URL` the SDK will be

## Quick Start Repository Template

We've conveniently packaged all the artifacts from this document in a github repository that can be used as a template for your own verification and deployment [here](https://github.com/guardrails-ai/continuous_integration_and_deployment_aws_template).
We've conveniently packaged all the artifacts from this document in a github repository that can be used as a template for your own verification and deployment [here](https://github.com/guardrails-ai/continuous_integration_and_deployment_aws_template).

## Diagram

```mermaid
graph TD
%% Internet and IGW
Internet((Internet)) --> IGW[Internet Gateway]
IGW --> RouteTable[Public Route Table]

%% VPC Container
VPC[VPC<br/>10.0.0.0/16] --> IGW

%% IAM Permissions Group
subgraph ECSIAMPermissions["ECS IAM Permissions"]
ExecutionRole[ECS Execution Role]
TaskRole[ECS Task Role]
end

%% Public Subnet Group
subgraph PublicSubnets["Public Subnets x3"]
NLB[Network Load Balancer]
TG[Target Group<br/>TCP:80]
SG[Security Group<br/>Ingress: 8000<br/>Egress: All]
ECSCluster[ECS Cluster]

%% ECS Components
ECSService[ECS Service]
TaskDef[Task Definition<br/>CPU: 1024<br/>Memory: 2048]
Container[Container<br/>Port: 8000]

%% Internal Connections
NLB --> |Port 80| TG
TG --> ECSService
ECSCluster --> ECSService
SG --> ECSService
ECSService --> TaskDef
TaskDef --> Container
end

%% IAM Connections
ECSIAMPermissions --> TaskDef

%% Route Table Connection
RouteTable --> PublicSubnets

%% External Service Connections
CloudWatchLogs[CloudWatch Log Group] --> Container
ECR[ECR Repository] --> |Image| Container

%% Internet Access
Internet --> NLB

%% Styling
classDef aws fill:#FF9900,stroke:#232F3E,stroke-width:2px,color:black
classDef subnet fill:#FFD700,stroke:#232F3E,stroke-width:2px,color:black
classDef iam fill:#FF6B6B,stroke:#232F3E,stroke-width:2px,color:black
class VPC,IGW,NATGateway,RouteTable,NLB,TG,ECSCluster,ECSService,TaskDef,Container,SG,CloudWatchLogs,ECR aws
class PublicSubnets subnet
class ECSIAMPermissions,ExecutionRole,TaskRole iam
```

## Terraform

Expand Down
133 changes: 133 additions & 0 deletions docs/migration_guides/0-6-alpha-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Migrating to 0.6.0-alpha

## Summary
This guide will help you migrate your codebase from 0.5.X to 0.6.0-alpha.

## Installation
```bash
pip install --pre guardrails-ai
```

## List of backwards incompatible changes

1. All validators will require authentication with a guardrails token. This will also apply to all versions >=0.4.x of the guardrails-ai package.
1. prompt, msg_history, instructions, reask_messages, reask_instructions, and will be removed from the `Guard.__call__` function and will instead be supported by a single messages argument, and a reask_messages argument for reasks.
1. Custom callables now need to expect `messages`, as opposed to `prompt` to come in as a keyword argument.
1. The default on_fail action for validators will change from noop to exception.
1. In cases where we try and generate structured data, Guardrails will no longer automatically attempt to try and coerce the LLM into giving correctly formatted information.
1. Guardrails will no longer automatically set a tool selection in the OpenAI callable when initialized using pydantic for initial prompts or reasks
1. Guard.from_string is being removed in favor of Guard()
1. Guard.from_pydantic is renamed to Guard.for_pydantic
1. Guard.from_rail is renamed to Guard.for_rail
1. Guard.from_rail_string is renamed to guard.for_rail_string
1. The guardrails server will change from using Flask to FastAPI. We recommend serving uvicorn runners via a gunicorn WSGI.
1. OpenAI, Cohere and Anthropic **callables are being removed in favor of support through passing no callable and setting the appropriate api key and model argument.


## How to migrate

### Messages support for reask and RAILS
`Guard.__call` and rails now fully support `reask_messages` as an argument.

### For `Guard.__call`
```py
response = guard(
messages=[{
"role":"user",
"content":"tell me a joke"
}],
reask_messages=[{
"role":"system"
"content":"your previous joke failed validation can you tell me another joke?"
}]
)
```

#### For Rail
```
<rail version="0.1">
<messages>
<message role="system">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
</message>
<message role="user">
${question}
</message>
</messages>
<reask_messages>
<message="system">
You were asked ${question} and it was not correct can you try again?
</message>
</reask_messages>
</rail>
```

## Improvements

### Per message validation and fix support
Message validation is now more granular and executed on a per message content basis.
This allows for on_fix behavior to be fully supported.

## Backwards-incompatible changes
### Validator onFail default behavior is now exception
Previously the default behavior for validation failure was noop. This meant developers were required to set it on_fail to exception or check validation_failed
to know if validation failed. This was unintuitive for new users and led to confusion around if validators were working or not. This new behavior will require
exception handling be added or configurations manually to be set to noop if desired.

### Simplified schema injection behavior
Previously prompt and instruction suffixes and formatting hints were sometimes automatically injected
into prompt and instructions if guardrails detected xml or a structured schema being used for output.
This caused confusion and unexpected behavior when arguments to llms were being mutated without a developer asking for it.
Developers will now need to intentionally include Guardrails template variables such as `${gr.complete_xml_suffix_v2}`

### Guardrails Server migration from Flask to FastAPI
In 0.5.X guard.__call and guard.validate async streaming received support for on_fail="fix" merge and parallelized async validation.
We have updated the server to use FastAPI which is build on ASGI to be able to fully take advantage of these improvements.
config.py now fully supports the definition of AsyncGuards and streaming=true as a request argument.
We recommend the combination of `gunicorn` and `uvicorn.workers.UvicornWorker`s

### Streamlined prompt, instructions and msg_history arguments into messages
`Guard.__call` prompt, instruction, reask_prompt and reask_instruction arguments have been streamlined into messages and reask_messages
Instructions should be specified with the role system and prompts with the role user. Any of the out of the box supported llms that require only one text prompt will automatically have the messages converted to one unless a custom callable is being used.
```
# version < 0.6.0
guard(
instructions="you are a funny assistant",
prompt="tell me a joke"
)
# version >= 0.6.0
guard(
messages=[
{"role":"system", "content":"you are a funny assistant"},
{"role":"user", "content":"tell me a joke"}
]
)
```

### Removal of guardrails OpenAI, Cohere, Anthropic Callables
These callables are being removed in favor of support through passing no callable and setting the appropriate api key and model argument.

### Prompt no longer a required positional argument on custom callables
Custom callables will no longer throw an error if the prompt arg is missing in their declaration and guardrails will no longer pass prompt as the first argument. They need to be updated to the messages kwarg to get text input. If a custom callables underlying llm only accepts a single string a helper exists that can compose messages into one otherwise some code to adapt them will be required.

```py
from guardrails import messages_to_prompt_string

class CustomCallableCallable(PromptCallableBase):
def llm_api(
self,
*args,
**kwargs,
) -> str:
messages = kwargs.pop("messages", [])
prompt = messages_to_prompt_string(messages)

llm_string_output = some_llm_call_requiring_prompt(
prompt,
*args,
**kwargs,
)
return llm_string_output
```
2 changes: 1 addition & 1 deletion docusaurus/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const config = {
markdown: {
mermaid: true
},
// themes: ['@docusaurus/theme-mermaid', '@docusaurus/theme-classic'],
themes: ['@docusaurus/theme-mermaid'],
// Even if you don't use internalization, you can use this field to set useful
// metadata like html lang. For example, if your site is Chinese, you may want
// to replace "en" with "zh-Hans".
Expand Down
5 changes: 3 additions & 2 deletions guardrails/cli/hub/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re

from guardrails.cli.hub.hub import hub_command
from guardrails.cli.hub.utils import get_site_packages_location
from guardrails.hub_telemetry.hub_tracing import trace
from .console import console

Expand All @@ -11,7 +10,9 @@
@trace(name="guardrails-cli/hub/list")
def list():
"""List all installed validators."""
site_packages = get_site_packages_location()
from guardrails.hub.validator_package_service import ValidatorPackageService

site_packages = ValidatorPackageService.get_site_packages_location()
hub_init_file = os.path.join(site_packages, "guardrails", "hub", "__init__.py")

installed_validators = []
Expand Down
58 changes: 20 additions & 38 deletions guardrails/cli/hub/uninstall.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import shutil
import sys
from typing import List, Literal

Expand All @@ -10,9 +9,7 @@
from guardrails.cli.server.hub_client import get_validator_manifest
from guardrails_hub_types import Manifest

from guardrails.cli.hub.utils import get_site_packages_location
from guardrails.cli.hub.utils import get_org_and_package_dirs
from guardrails.cli.hub.utils import get_hub_directory
from guardrails.cli.hub.utils import pip_process
from guardrails.hub_telemetry.hub_tracing import trace

from .console import console
Expand All @@ -32,46 +29,28 @@ def remove_line(file_path: str, line_content: str):


def remove_from_hub_inits(manifest: Manifest, site_packages: str):
org_package = get_org_and_package_dirs(manifest)
from guardrails.hub.validator_package_service import ValidatorPackageService

exports: List[str] = manifest.exports or []
sorted_exports = sorted(exports, reverse=True)
module_name = manifest.module_name
relative_path = ".".join([*org_package, module_name])
import_line = (
f"from guardrails.hub.{relative_path} import {', '.join(sorted_exports)}"

validator_id = manifest.id
import_path = ValidatorPackageService.get_import_path_from_validator_id(
validator_id
)
import_line = f"from {import_path} import {', '.join(sorted_exports)}"

# Remove import line from main __init__.py
hub_init_location = os.path.join(site_packages, "guardrails", "hub", "__init__.py")
remove_line(hub_init_location, import_line)

# Remove import line from namespace __init__.py
namespace = org_package[0]
namespace_init_location = os.path.join(
site_packages, "guardrails", "hub", namespace, "__init__.py"
)
lines = remove_line(namespace_init_location, import_line)

# remove namespace pkg if namespace __init__.py is empty
if (len(lines) == 0) and (namespace != "hub"):
logger.info(f"Removing namespace package {namespace} as it is now empty")
try:
shutil.rmtree(os.path.join(site_packages, "guardrails", "hub", namespace))
except Exception as e:
logger.error(f"Error removing namespace package {namespace}")
logger.error(e)
sys.exit(1)


def uninstall_hub_module(manifest: Manifest, site_packages: str):
uninstall_directory = get_hub_directory(manifest, site_packages)
logger.info(f"Removing directory {uninstall_directory}")
try:
shutil.rmtree(uninstall_directory)
except Exception as e:
logger.error("Error removing directory")
logger.error(e)
sys.exit(1)

def uninstall_hub_module(manifest: Manifest):
from guardrails.hub.validator_package_service import ValidatorPackageService

validator_id = manifest.id
package_name = ValidatorPackageService.get_normalized_package_name(validator_id)
pip_process("uninstall", package_name, flags=["-y"], quiet=True)


@hub_command.command()
Expand All @@ -82,6 +61,9 @@ def uninstall(
),
):
"""Uninstall a validator from the Hub."""
from guardrails.hub.validator_package_service import ValidatorPackageService

print(f"ValidatorPackageService: {ValidatorPackageService}")
if not package_uri.startswith("hub://"):
logger.error("Invalid URI!")
sys.exit(1)
Expand All @@ -98,11 +80,11 @@ def uninstall(
# Prep
with console.status("Fetching manifest", spinner="bouncingBar"):
module_manifest = get_validator_manifest(module_name)
site_packages = get_site_packages_location()
site_packages = ValidatorPackageService.get_site_packages_location()

# Uninstall
with console.status("Removing module", spinner="bouncingBar"):
uninstall_hub_module(module_manifest, site_packages)
uninstall_hub_module(module_manifest)

# Cleanup
with console.status("Cleaning up", spinner="bouncingBar"):
Expand Down
Loading
Loading