Setup the Guardrails server locally
This guide is condensed to get you up and running quickly.To see the full guide on using the Guardrails Server, see the usage doc.To learn more about the Guardrails Server, see the concepts doc.
RegexMatch validator:
config.py that defines the Guard. We’ll use a regex that matches title case strings:
Best practices
Running the Guardrails server like this is useful for rapid development and testing out guardrails, but it is not suitable for production use. The built-in dev server is not optimized for stability, performance, or security. For production, serve the FastAPI application with an ASGI server like Uvicorn (directly, or via Gunicorn’s Uvicorn worker class). Furthermore, the dev server isn’t as portable or deployable broadly as many cloud ecosystems would require. Our solution for this is to build a Docker container for the Guardrails server, using Uvicorn (or Gunicorn with the Uvicorn worker class) to serve the FastAPI app.Hosting Guardrails with Docker
The Guardrails server can be packaged into a Docker container for production deployments. Once containerized, you can host it in any Docker-compatible hosting service, such as Kubernetes, AWS Fargate, Google Cloud Run, etc.Building and running the Docker container
You can build the Docker container as you would any other Docker application. We need a dockerfile to get started. You can download the file from our Github repo. Save it in the top level of your project directory as a file calledDockerfile.
Understanding the Guardrails server Dockerfile
Below we walk through the most significant lines in case you need to customize the server to fit your needs.pyproject.toml or poetry.lock depending on how you manage your dependencies.
.dockerignore file to prevent unwanted files and folders from copying.
guardrails_api.app:create_app() in your startup command.
Using OpenAI with the Guardrails server
Just like the open source library, the Guardrails server comes with first class support for OpenAI, and other LLM APIs. By setting your Guardrails Docker Image up to talk to OpenAI, you can pass around guarded OpenAI SDK compatible endpoints to your team, ensuring that all uses of the API are validated and secure. As with any other use case with OpenAI, you must authenticate with an API key in order to make requests to its services. You have two options for providing an OpenAI API key to the Guardrails server:- Specify the API key at build or deploy time as an environment variable on the container. This approach allows you to avoid including your key in requests and abstract it away into the container runtime. Note, though, that if and when your key changes, you would need to redeploy the server with the updated key.
-
Specify the API key at runtime by including it as a named header
x-openai-api-key. This approach allows you to utilize different API keys with different scopes if necessary as well as performing key rotation without the need to restart the server.
--env flag:
.env files. Should you wish to use this option instead, you can do so in the docker run command:
Using other LLMs with the Guardrails server
The same approaches above can be used to set other environment variables including other OpenAI env vars likeOPENAI_API_BASE, as well as for other libraries or LLMs like ANTHROPIC_API_KEY, TOGETHERAI_API_KEY, etc.