guardrails-api 0.0.0a0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. guardrails_api-0.0.0a0/LICENSE +44 -0
  2. guardrails_api-0.0.0a0/MANIFEST.in +2 -0
  3. guardrails_api-0.0.0a0/PKG-INFO +127 -0
  4. guardrails_api-0.0.0a0/README.md +50 -0
  5. guardrails_api-0.0.0a0/guardrails_api/__init__.py +1 -0
  6. guardrails_api-0.0.0a0/guardrails_api/app.py +99 -0
  7. guardrails_api-0.0.0a0/guardrails_api/blueprints/__init__.py +0 -0
  8. guardrails_api-0.0.0a0/guardrails_api/blueprints/guards.py +326 -0
  9. guardrails_api-0.0.0a0/guardrails_api/blueprints/root.py +79 -0
  10. guardrails_api-0.0.0a0/guardrails_api/classes/__init__.py +0 -0
  11. guardrails_api-0.0.0a0/guardrails_api/classes/health_check.py +7 -0
  12. guardrails_api-0.0.0a0/guardrails_api/classes/http_error.py +26 -0
  13. guardrails_api-0.0.0a0/guardrails_api/cli/__init__.py +24 -0
  14. guardrails_api-0.0.0a0/guardrails_api/cli/cli.py +4 -0
  15. guardrails_api-0.0.0a0/guardrails_api/cli/start.py +60 -0
  16. guardrails_api-0.0.0a0/guardrails_api/clients/__init__.py +0 -0
  17. guardrails_api-0.0.0a0/guardrails_api/clients/cache_client.py +34 -0
  18. guardrails_api-0.0.0a0/guardrails_api/clients/guard_client.py +35 -0
  19. guardrails_api-0.0.0a0/guardrails_api/clients/memory_guard_client.py +54 -0
  20. guardrails_api-0.0.0a0/guardrails_api/clients/pg_guard_client.py +106 -0
  21. guardrails_api-0.0.0a0/guardrails_api/clients/postgres_client.py +83 -0
  22. guardrails_api-0.0.0a0/guardrails_api/config.py +11 -0
  23. guardrails_api-0.0.0a0/guardrails_api/default.env +8 -0
  24. guardrails_api-0.0.0a0/guardrails_api/models/__init__.py +0 -0
  25. guardrails_api-0.0.0a0/guardrails_api/models/base.py +8 -0
  26. guardrails_api-0.0.0a0/guardrails_api/models/guard_item.py +27 -0
  27. guardrails_api-0.0.0a0/guardrails_api/models/guard_item_audit.py +63 -0
  28. guardrails_api-0.0.0a0/guardrails_api/open-api-spec.json +1694 -0
  29. guardrails_api-0.0.0a0/guardrails_api/open_api_spec.py +17 -0
  30. guardrails_api-0.0.0a0/guardrails_api/otel/__init__.py +28 -0
  31. guardrails_api-0.0.0a0/guardrails_api/otel/constants.py +1 -0
  32. guardrails_api-0.0.0a0/guardrails_api/otel/logs.py +7 -0
  33. guardrails_api-0.0.0a0/guardrails_api/otel/metrics.py +59 -0
  34. guardrails_api-0.0.0a0/guardrails_api/otel/traces.py +72 -0
  35. guardrails_api-0.0.0a0/guardrails_api/py.typed +0 -0
  36. guardrails_api-0.0.0a0/guardrails_api/start.sh +9 -0
  37. guardrails_api-0.0.0a0/guardrails_api/utils/__init__.py +0 -0
  38. guardrails_api-0.0.0a0/guardrails_api/utils/escape_curlys.py +18 -0
  39. guardrails_api-0.0.0a0/guardrails_api/utils/file.py +6 -0
  40. guardrails_api-0.0.0a0/guardrails_api/utils/gather_request_metrics.py +39 -0
  41. guardrails_api-0.0.0a0/guardrails_api/utils/get_llm_callable.py +37 -0
  42. guardrails_api-0.0.0a0/guardrails_api/utils/handle_error.py +27 -0
  43. guardrails_api-0.0.0a0/guardrails_api/utils/logger.py +13 -0
  44. guardrails_api-0.0.0a0/guardrails_api/utils/payload_validator.py +39 -0
  45. guardrails_api-0.0.0a0/guardrails_api/utils/pluck.py +5 -0
  46. guardrails_api-0.0.0a0/guardrails_api/utils/remove_nones.py +20 -0
  47. guardrails_api-0.0.0a0/guardrails_api/utils/try_json_loads.py +9 -0
  48. guardrails_api-0.0.0a0/guardrails_api.egg-info/PKG-INFO +127 -0
  49. guardrails_api-0.0.0a0/guardrails_api.egg-info/SOURCES.txt +62 -0
  50. guardrails_api-0.0.0a0/guardrails_api.egg-info/dependency_links.txt +1 -0
  51. guardrails_api-0.0.0a0/guardrails_api.egg-info/entry_points.txt +2 -0
  52. guardrails_api-0.0.0a0/guardrails_api.egg-info/requires.txt +24 -0
  53. guardrails_api-0.0.0a0/guardrails_api.egg-info/top_level.txt +2 -0
  54. guardrails_api-0.0.0a0/pyproject.toml +58 -0
  55. guardrails_api-0.0.0a0/setup.cfg +7 -0
  56. guardrails_api-0.0.0a0/setup.py +38 -0
  57. guardrails_api-0.0.0a0/tests/__init__.py +0 -0
  58. guardrails_api-0.0.0a0/tests/blueprints/__init__.py +0 -0
  59. guardrails_api-0.0.0a0/tests/blueprints/test_guards.py +547 -0
  60. guardrails_api-0.0.0a0/tests/blueprints/test_root.py +48 -0
  61. guardrails_api-0.0.0a0/tests/clients/__init__.py +0 -0
  62. guardrails_api-0.0.0a0/tests/clients/test_mem_guard_client.py +50 -0
  63. guardrails_api-0.0.0a0/tests/clients/test_pg_guard_client.py +464 -0
@@ -0,0 +1,44 @@
1
+ ## THE FOLLOWING LICENSE HAS BEEN ADOPTED FROM THE ELSSTIC LICENSE ##
2
+
3
+ Acceptance
4
+ By using the software, you agree to all of the terms and conditions below.
5
+
6
+ Copyright License
7
+ The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below.
8
+
9
+ Limitations
10
+ You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
11
+
12
+ You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
13
+
14
+ You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
15
+
16
+ Patents
17
+ The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
18
+
19
+ Notices
20
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
21
+
22
+ If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
23
+
24
+ No Other Rights
25
+ These terms do not imply any licenses other than those expressly granted in these terms.
26
+
27
+ Termination
28
+ If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
29
+
30
+ No Liability
31
+ As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
32
+
33
+ Definitions
34
+ The licensor is the entity offering these terms, and the software is the software the licensor makes available under these terms, including any portion of it.
35
+
36
+ you refers to the individual or entity agreeing to these terms.
37
+
38
+ your company is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. control means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
39
+
40
+ your licenses are all the licenses granted to you for the software under these terms.
41
+
42
+ use means anything you do with the software requiring one of your licenses.
43
+
44
+ trademark means trademarks, service marks, and similar rights.
@@ -0,0 +1,2 @@
1
+ include guardrails_api/*
2
+ exclude */__pycache__/*
@@ -0,0 +1,127 @@
1
+ Metadata-Version: 2.1
2
+ Name: guardrails-api
3
+ Version: 0.0.0a0
4
+ Summary: Guardrails API
5
+ Author-email: Guardrails AI <contact@guardrailsai.com>
6
+ License: ## THE FOLLOWING LICENSE HAS BEEN ADOPTED FROM THE ELSSTIC LICENSE ##
7
+
8
+ Acceptance
9
+ By using the software, you agree to all of the terms and conditions below.
10
+
11
+ Copyright License
12
+ The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below.
13
+
14
+ Limitations
15
+ You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
16
+
17
+ You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
18
+
19
+ You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
20
+
21
+ Patents
22
+ The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
23
+
24
+ Notices
25
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
26
+
27
+ If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
28
+
29
+ No Other Rights
30
+ These terms do not imply any licenses other than those expressly granted in these terms.
31
+
32
+ Termination
33
+ If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
34
+
35
+ No Liability
36
+ As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
37
+
38
+ Definitions
39
+ The licensor is the entity offering these terms, and the software is the software the licensor makes available under these terms, including any portion of it.
40
+
41
+ you refers to the individual or entity agreeing to these terms.
42
+
43
+ your company is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. control means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
44
+
45
+ your licenses are all the licenses granted to you for the software under these terms.
46
+
47
+ use means anything you do with the software requiring one of your licenses.
48
+
49
+ trademark means trademarks, service marks, and similar rights.
50
+ Keywords: Guardrails,Guardrails AI,Guardrails API,Guardrails API
51
+ Requires-Python: >=3.8, <4
52
+ Description-Content-Type: text/markdown
53
+ License-File: LICENSE
54
+ Requires-Dist: guardrails-ai>=0.5.0a2
55
+ Requires-Dist: flask<4,>=3.0.3
56
+ Requires-Dist: Flask-SQLAlchemy<4,>=3.1.1
57
+ Requires-Dist: Flask-Caching<3,>=2.3.0
58
+ Requires-Dist: Werkzeug<4,>=3.0.3
59
+ Requires-Dist: jsonschema<5,>=4.22.0
60
+ Requires-Dist: referencing<1,>=0.35.1
61
+ Requires-Dist: Flask-Cors<5,>=4.0.1
62
+ Requires-Dist: boto3<2,>=1.34.115
63
+ Requires-Dist: gunicorn<23,>=22.0.0
64
+ Requires-Dist: psycopg2-binary<3,>=2.9.9
65
+ Requires-Dist: litellm<2,>=1.39.3
66
+ Requires-Dist: typer<1,>=0.9.4
67
+ Requires-Dist: opentelemetry-api<2,>=1.0.0
68
+ Requires-Dist: opentelemetry-sdk<2,>=1.0.0
69
+ Requires-Dist: opentelemetry-exporter-otlp-proto-grpc<2,>=1.0.0
70
+ Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.0.0
71
+ Requires-Dist: opentelemetry-instrumentation-flask<1,>=0.12b0
72
+ Provides-Extra: dev
73
+ Requires-Dist: ruff; extra == "dev"
74
+ Requires-Dist: pytest; extra == "dev"
75
+ Requires-Dist: coverage; extra == "dev"
76
+ Requires-Dist: pytest-mock; extra == "dev"
77
+
78
+ # guardrails-poc
79
+ Docker compose stub of Guardrails as a Service
80
+
81
+ ## Setting Up
82
+ We strongly encourage you to use a virtual environment when developing in python.
83
+ To set one up for this project run the following:
84
+ ```bash
85
+ python3 -m venv ./.venv
86
+ source ./.venv/bin/activate
87
+ ```
88
+ Your terminal should now show that you are working from within the virtual environment.
89
+ Now you can install the dependencies:
90
+ ```bash
91
+ make install
92
+ ```
93
+
94
+ And start the dev server:
95
+ ```bash
96
+ make start
97
+ ```
98
+
99
+ Once the service has launched, you should be able to navigate to the Swagger documenation for the guardrails-api at http://localhost:8000
100
+
101
+
102
+ ### Local Infrastructure
103
+ By default, the server will start with an in-memory store for Guards. As of June 4th, 2024 this store does not support write operations via the API. In order to utilize all CRUD operations you will need a postgres database running locally and you will need to provide the following environment variables (sane defaults included for demonstration purposes):
104
+
105
+ ```sh
106
+ export PGPORT=5432
107
+ export PGDATABASE=postgres
108
+ export PGHOST=localhost
109
+ export PGUSER=${PGUSER:-postgres}
110
+ export PGPASSWORD=${PGPASSWORD:-changeme}
111
+ ```
112
+
113
+ You can create this database however you wish, but we do have a docker-compose configuration to stand up the database as well as a local opentelemetry stack.
114
+ To use this make sure you have docker installed, then run:
115
+
116
+ `docker compose --profile db up --build`
117
+ to run just the database.
118
+
119
+ `docker compose --profile infra up --build`
120
+ to run the database and opentelemetry infrastructure
121
+
122
+ or
123
+ `docker compose --profile all up --build`
124
+ to run everything including the guardrails-api
125
+
126
+
127
+ The last option is useful when checking that everything will work as planned in a more productionized environment. When developing, it's generally faster to just run the minimum infrastructure you need via docker and run the api on a bare process with the `make start` command.
@@ -0,0 +1,50 @@
1
+ # guardrails-poc
2
+ Docker compose stub of Guardrails as a Service
3
+
4
+ ## Setting Up
5
+ We strongly encourage you to use a virtual environment when developing in python.
6
+ To set one up for this project run the following:
7
+ ```bash
8
+ python3 -m venv ./.venv
9
+ source ./.venv/bin/activate
10
+ ```
11
+ Your terminal should now show that you are working from within the virtual environment.
12
+ Now you can install the dependencies:
13
+ ```bash
14
+ make install
15
+ ```
16
+
17
+ And start the dev server:
18
+ ```bash
19
+ make start
20
+ ```
21
+
22
+ Once the service has launched, you should be able to navigate to the Swagger documenation for the guardrails-api at http://localhost:8000
23
+
24
+
25
+ ### Local Infrastructure
26
+ By default, the server will start with an in-memory store for Guards. As of June 4th, 2024 this store does not support write operations via the API. In order to utilize all CRUD operations you will need a postgres database running locally and you will need to provide the following environment variables (sane defaults included for demonstration purposes):
27
+
28
+ ```sh
29
+ export PGPORT=5432
30
+ export PGDATABASE=postgres
31
+ export PGHOST=localhost
32
+ export PGUSER=${PGUSER:-postgres}
33
+ export PGPASSWORD=${PGPASSWORD:-changeme}
34
+ ```
35
+
36
+ You can create this database however you wish, but we do have a docker-compose configuration to stand up the database as well as a local opentelemetry stack.
37
+ To use this make sure you have docker installed, then run:
38
+
39
+ `docker compose --profile db up --build`
40
+ to run just the database.
41
+
42
+ `docker compose --profile infra up --build`
43
+ to run the database and opentelemetry infrastructure
44
+
45
+ or
46
+ `docker compose --profile all up --build`
47
+ to run everything including the guardrails-api
48
+
49
+
50
+ The last option is useful when checking that everything will work as planned in a more productionized environment. When developing, it's generally faster to just run the minimum infrastructure you need via docker and run the api on a bare process with the `make start` command.
@@ -0,0 +1 @@
1
+ __version__ = "0.0.0a0"
@@ -0,0 +1,99 @@
1
+ import os
2
+ from typing import Optional
3
+ from flask import Flask
4
+ from flask.json.provider import DefaultJSONProvider
5
+ from flask_cors import CORS
6
+ from werkzeug.middleware.proxy_fix import ProxyFix
7
+ from urllib.parse import urlparse
8
+ from guardrails import configure_logging
9
+ from opentelemetry.instrumentation.flask import FlaskInstrumentor
10
+ from guardrails_api.clients.postgres_client import postgres_is_enabled
11
+ from guardrails_api.otel import otel_is_disabled, initialize
12
+ from guardrails_api.clients.cache_client import CacheClient
13
+
14
+
15
+ # TODO: Move this to a separate file
16
+ class OverrideJsonProvider(DefaultJSONProvider):
17
+ def default(self, o):
18
+ if isinstance(o, set):
19
+ return list(o)
20
+ if callable(o):
21
+ return str(o)
22
+ return super().default(o)
23
+
24
+
25
+ class ReverseProxied(object):
26
+ def __init__(self, app):
27
+ self.app = app
28
+
29
+ def __call__(self, environ, start_response):
30
+ self_endpoint = os.environ.get("SELF_ENDPOINT", "http://localhost:8000")
31
+ url = urlparse(self_endpoint)
32
+ environ["wsgi.url_scheme"] = url.scheme
33
+ return self.app(environ, start_response)
34
+
35
+
36
+ def register_config(config: Optional[str] = None):
37
+ default_config_file = os.path.join(os.path.dirname(__file__), "config.py")
38
+
39
+ config_file = config or default_config_file
40
+ config_file_path = os.path.abspath(config_file)
41
+ if os.path.isfile(config_file_path):
42
+ from importlib.machinery import SourceFileLoader
43
+
44
+ # This creates a module named "validators" with the contents of the init file
45
+ # This allow statements like `from validators import StartsWith`
46
+ # But more importantly, it registers all of the validators imported in the init
47
+ SourceFileLoader("config", config_file_path).load_module()
48
+
49
+
50
+ def create_app(env: Optional[str] = None, config: Optional[str] = None, port: Optional[int] = None):
51
+ if os.environ.get("APP_ENVIRONMENT") != "production":
52
+ from dotenv import load_dotenv
53
+
54
+ default_env_file = os.path.join(os.path.dirname(__file__), "default.env")
55
+ env_file = env or default_env_file
56
+ env_file_path = os.path.abspath(env_file)
57
+ load_dotenv(env_file_path)
58
+
59
+ set_port = port or os.environ.get("PORT", 8000)
60
+ host = os.environ.get("HOST", "http://localhost")
61
+ self_endpoint = os.environ.get("SELF_ENDPOINT", f"{host}:{set_port}")
62
+ os.environ["SELF_ENDPOINT"] = self_endpoint
63
+
64
+ register_config(config)
65
+
66
+ app = Flask(__name__)
67
+ app.json = OverrideJsonProvider(app)
68
+
69
+ app.config["APPLICATION_ROOT"] = "/"
70
+ app.config["PREFERRED_URL_SCHEME"] = "https"
71
+ app.wsgi_app = ReverseProxied(app.wsgi_app)
72
+ CORS(app)
73
+
74
+ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1)
75
+
76
+ guardrails_log_level = os.environ.get("GUARDRAILS_LOG_LEVEL", "INFO")
77
+ configure_logging(log_level=guardrails_log_level)
78
+
79
+ if not otel_is_disabled():
80
+ FlaskInstrumentor().instrument_app(app)
81
+ initialize()
82
+
83
+ # if no pg_host is set, don't set up postgres
84
+ if postgres_is_enabled():
85
+ from guardrails_api.clients.postgres_client import PostgresClient
86
+
87
+ pg_client = PostgresClient()
88
+ pg_client.initialize(app)
89
+
90
+ cache_client = CacheClient()
91
+ cache_client.initialize(app)
92
+
93
+ from guardrails_api.blueprints.root import root_bp
94
+ from guardrails_api.blueprints.guards import guards_bp
95
+
96
+ app.register_blueprint(root_bp)
97
+ app.register_blueprint(guards_bp)
98
+
99
+ return app
@@ -0,0 +1,326 @@
1
+ import json
2
+ import os
3
+ from guardrails.hub import * # noqa
4
+ from string import Template
5
+ from typing import Any, Dict, cast
6
+ from flask import Blueprint, Response, request, stream_with_context
7
+ from urllib.parse import unquote_plus
8
+ from guardrails import Guard
9
+ from guardrails.classes import ValidationOutcome
10
+ from opentelemetry.trace import Span
11
+ from guardrails_api_client import Guard as GuardStruct
12
+ from guardrails_api.classes.http_error import HttpError
13
+ from guardrails_api.clients.cache_client import CacheClient
14
+ from guardrails_api.clients.memory_guard_client import MemoryGuardClient
15
+ from guardrails_api.clients.pg_guard_client import PGGuardClient
16
+ from guardrails_api.clients.postgres_client import postgres_is_enabled
17
+ from guardrails_api.utils.handle_error import handle_error
18
+ from guardrails_api.utils.get_llm_callable import get_llm_callable
19
+
20
+
21
+ guards_bp = Blueprint("guards", __name__, url_prefix="/guards")
22
+
23
+
24
+ # if no pg_host is set, use in memory guards
25
+ if postgres_is_enabled():
26
+ guard_client = PGGuardClient()
27
+ else:
28
+ guard_client = MemoryGuardClient()
29
+ # Will be defined at runtime
30
+ import config # noqa
31
+
32
+ exports = config.__dir__()
33
+ for export_name in exports:
34
+ export = getattr(config, export_name)
35
+ is_guard = isinstance(export, Guard)
36
+ if is_guard:
37
+ guard_client.create_guard(export)
38
+
39
+ cache_client = CacheClient()
40
+
41
+
42
+ @guards_bp.route("/", methods=["GET", "POST"])
43
+ @handle_error
44
+ def guards():
45
+ if request.method == "GET":
46
+ guards = guard_client.get_guards()
47
+ return [g.to_dict() for g in guards]
48
+ elif request.method == "POST":
49
+ if not postgres_is_enabled():
50
+ raise HttpError(
51
+ 501,
52
+ "NotImplemented",
53
+ "POST /guards is not implemented for in-memory guards.",
54
+ )
55
+ payload = request.json
56
+ guard = GuardStruct.from_dict(payload)
57
+ new_guard = guard_client.create_guard(guard)
58
+ return new_guard.to_dict()
59
+ else:
60
+ raise HttpError(
61
+ 405,
62
+ "Method Not Allowed",
63
+ "/guards only supports the GET and POST methods. You specified"
64
+ " {request_method}".format(request_method=request.method),
65
+ )
66
+
67
+
68
+ @guards_bp.route("/<guard_name>", methods=["GET", "PUT", "DELETE"])
69
+ @handle_error
70
+ def guard(guard_name: str):
71
+ decoded_guard_name = unquote_plus(guard_name)
72
+ if request.method == "GET":
73
+ as_of_query = request.args.get("asOf")
74
+ guard = guard_client.get_guard(decoded_guard_name, as_of_query)
75
+ if guard is None:
76
+ raise HttpError(
77
+ 404,
78
+ "NotFound",
79
+ "A Guard with the name {guard_name} does not exist!".format(
80
+ guard_name=decoded_guard_name
81
+ ),
82
+ )
83
+ return guard.to_dict()
84
+ elif request.method == "PUT":
85
+ if not postgres_is_enabled():
86
+ raise HttpError(
87
+ 501,
88
+ "NotImplemented",
89
+ "PUT /<guard_name> is not implemented for in-memory guards.",
90
+ )
91
+ payload = request.json
92
+ guard = GuardStruct.from_dict(payload)
93
+ updated_guard = guard_client.upsert_guard(decoded_guard_name, guard)
94
+ return updated_guard.to_dict()
95
+ elif request.method == "DELETE":
96
+ if not postgres_is_enabled():
97
+ raise HttpError(
98
+ 501,
99
+ "NotImplemented",
100
+ "DELETE /<guard_name> is not implemented for in-memory guards.",
101
+ )
102
+ guard = guard_client.delete_guard(decoded_guard_name)
103
+ return guard.to_dict()
104
+ else:
105
+ raise HttpError(
106
+ 405,
107
+ "Method Not Allowed",
108
+ "/guard/<guard_name> only supports the GET, PUT, and DELETE methods."
109
+ " You specified {request_method}".format(request_method=request.method),
110
+ )
111
+
112
+
113
+ def collect_telemetry(
114
+ *,
115
+ guard: Guard,
116
+ validate_span: Span,
117
+ validation_output: ValidationOutcome,
118
+ prompt_params: Dict[str, Any],
119
+ result: ValidationOutcome,
120
+ ):
121
+ # Below is all telemetry collection and
122
+ # should have no impact on what is returned to the user
123
+ prompt = guard.history.last.inputs.prompt
124
+ if prompt:
125
+ prompt = Template(prompt).safe_substitute(**prompt_params)
126
+ validate_span.set_attribute("prompt", prompt)
127
+
128
+ instructions = guard.history.last.inputs.instructions
129
+ if instructions:
130
+ instructions = Template(instructions).safe_substitute(**prompt_params)
131
+ validate_span.set_attribute("instructions", instructions)
132
+
133
+ validate_span.set_attribute("validation_status", guard.history.last.status)
134
+ validate_span.set_attribute("raw_llm_ouput", result.raw_llm_output)
135
+
136
+ # Use the serialization from the class instead of re-writing it
137
+ valid_output: str = (
138
+ json.dumps(validation_output.validated_output)
139
+ if isinstance(validation_output.validated_output, dict)
140
+ else str(validation_output.validated_output)
141
+ )
142
+ validate_span.set_attribute("validated_output", valid_output)
143
+
144
+ validate_span.set_attribute("tokens_consumed", guard.history.last.tokens_consumed)
145
+
146
+ num_of_reasks = (
147
+ guard.history.last.iterations.length - 1
148
+ if guard.history.last.iterations.length > 0
149
+ else 0
150
+ )
151
+ validate_span.set_attribute("num_of_reasks", num_of_reasks)
152
+
153
+
154
+ @guards_bp.route("/<guard_name>/validate", methods=["POST"])
155
+ @handle_error
156
+ def validate(guard_name: str):
157
+ # Do we actually need a child span here?
158
+ # We could probably use the existing span from the request unless we forsee
159
+ # capturing the same attributes on non-GaaS Guard runs.
160
+ if request.method != "POST":
161
+ raise HttpError(
162
+ 405,
163
+ "Method Not Allowed",
164
+ "/guards/<guard_name>/validate only supports the POST method. You specified"
165
+ " {request_method}".format(request_method=request.method),
166
+ )
167
+ payload = request.json
168
+ openai_api_key = request.headers.get(
169
+ "x-openai-api-key", os.environ.get("OPENAI_API_KEY")
170
+ )
171
+ decoded_guard_name = unquote_plus(guard_name)
172
+ guard_struct = guard_client.get_guard(decoded_guard_name)
173
+
174
+ llm_output = payload.pop("llmOutput", None)
175
+ num_reasks = payload.pop("numReasks", None)
176
+ prompt_params = payload.pop("promptParams", {})
177
+ llm_api = payload.pop("llmApi", None)
178
+ args = payload.pop("args", [])
179
+ stream = payload.pop("stream", False)
180
+
181
+ # service_name = os.environ.get("OTEL_SERVICE_NAME", "guardrails-api")
182
+ # otel_tracer = get_tracer(service_name)
183
+
184
+ payload["api_key"] = payload.get("api_key", openai_api_key)
185
+
186
+ # with otel_tracer.start_as_current_span(
187
+ # f"validate-{decoded_guard_name}"
188
+ # ) as validate_span:
189
+ # guard: Guard = guard_struct.to_guard(openai_api_key, otel_tracer)
190
+ guard = guard_struct
191
+ if not isinstance(guard_struct, Guard):
192
+ guard: Guard = Guard.from_dict(guard_struct.to_dict())
193
+
194
+ # validate_span.set_attribute("guardName", decoded_guard_name)
195
+ if llm_api is not None:
196
+ llm_api = get_llm_callable(llm_api)
197
+ if openai_api_key is None:
198
+ raise HttpError(
199
+ status=400,
200
+ message="BadRequest",
201
+ cause=(
202
+ "Cannot perform calls to OpenAI without an api key. Pass"
203
+ " openai_api_key when initializing the Guard or set the"
204
+ " OPENAI_API_KEY environment variable."
205
+ ),
206
+ )
207
+ elif num_reasks and num_reasks > 1:
208
+ raise HttpError(
209
+ status=400,
210
+ message="BadRequest",
211
+ cause=(
212
+ "Cannot perform re-asks without an LLM API. Specify llm_api when"
213
+ " calling guard(...)."
214
+ ),
215
+ )
216
+
217
+ if llm_output is not None:
218
+ if stream:
219
+ raise HttpError(
220
+ status=400,
221
+ message="BadRequest",
222
+ cause="Streaming is not supported for parse calls!",
223
+ )
224
+ result: ValidationOutcome = guard.parse(
225
+ llm_output=llm_output,
226
+ num_reasks=num_reasks,
227
+ prompt_params=prompt_params,
228
+ llm_api=llm_api,
229
+ # api_key=openai_api_key,
230
+ **payload,
231
+ )
232
+ else:
233
+ if stream:
234
+
235
+ def guard_streamer():
236
+ guard_stream = guard(
237
+ # llm_api=llm_api,
238
+ prompt_params=prompt_params,
239
+ num_reasks=num_reasks,
240
+ stream=stream,
241
+ # api_key=openai_api_key,
242
+ *args,
243
+ **payload,
244
+ )
245
+
246
+ for result in guard_stream:
247
+ # TODO: Just make this a ValidationOutcome with history
248
+ validation_output: ValidationOutcome = ValidationOutcome(
249
+ result.validation_passed,
250
+ result.validated_output,
251
+ guard.history,
252
+ result.raw_llm_output,
253
+ )
254
+
255
+ yield validation_output, cast(ValidationOutcome, result)
256
+
257
+ def validate_streamer(guard_iter):
258
+ next_result = None
259
+ # next_validation_output = None
260
+ for validation_output, result in guard_iter:
261
+ next_result = result
262
+ # next_validation_output = validation_output
263
+ fragment = json.dumps(validation_output.to_response())
264
+ yield f"{fragment}\n"
265
+
266
+ final_validation_output: ValidationOutcome = ValidationOutcome(
267
+ validation_passed=next_result.validation_passed,
268
+ validated_output=next_result.validated_output,
269
+ history=guard.history,
270
+ raw_llm_output=next_result.raw_llm_output,
271
+ )
272
+ # I don't know if these are actually making it to OpenSearch
273
+ # because the span may be ended already
274
+ # collect_telemetry(
275
+ # guard=guard,
276
+ # validate_span=validate_span,
277
+ # validation_output=next_validation_output,
278
+ # prompt_params=prompt_params,
279
+ # result=next_result
280
+ # )
281
+ final_output_json = final_validation_output.to_json()
282
+ yield f"{final_output_json}\n"
283
+
284
+ return Response(
285
+ stream_with_context(validate_streamer(guard_streamer())),
286
+ content_type="application/json",
287
+ # content_type="text/event-stream"
288
+ )
289
+
290
+ result: ValidationOutcome = guard(
291
+ llm_api=llm_api,
292
+ prompt_params=prompt_params,
293
+ num_reasks=num_reasks,
294
+ # api_key=openai_api_key,
295
+ *args,
296
+ **payload,
297
+ )
298
+
299
+ # TODO: Just make this a ValidationOutcome with history
300
+ # validation_output = ValidationOutcome(
301
+ # validation_passed = result.validation_passed,
302
+ # validated_output=result.validated_output,
303
+ # history=guard.history,
304
+ # raw_llm_output=result.raw_llm_output,
305
+ # )
306
+
307
+ # collect_telemetry(
308
+ # guard=guard,
309
+ # validate_span=validate_span,
310
+ # validation_output=validation_output,
311
+ # prompt_params=prompt_params,
312
+ # result=result
313
+ # )
314
+ serialized_history = [
315
+ call.to_dict() for call in guard.history
316
+ ]
317
+ cache_key = f"{guard.name}-{result.call_id}"
318
+ cache_client.set(cache_key, serialized_history, 300)
319
+ return result.to_dict()
320
+
321
+ @guards_bp.route("/<guard_name>/history/<call_id>", methods=["GET"])
322
+ @handle_error
323
+ def guard_history(guard_name: str, call_id: str):
324
+ if request.method == "GET":
325
+ cache_key = f"{guard_name}-{call_id}"
326
+ return cache_client.get(cache_key)