Skip to content
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

Fix App Context #66

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions guardrails_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def create_app(
cache_client.initialize(app)

from guardrails_api.blueprints.root import root_bp
from guardrails_api.blueprints.guards import guards_bp, guard_client
from guardrails_api.blueprints.guards import guards_bp

app.register_blueprint(root_bp)
app.register_blueprint(guards_bp)
Expand All @@ -111,11 +111,13 @@ def create_app(

console.print(":green_circle: Active guards and OpenAI compatible endpoints:")

for g in guard_client.get_guards():
g = g.to_dict()
console.print(f"- Guard: [bold white]{g.get('name')}[/bold white] {self_endpoint}/guards/{g.get('name')}/openai/v1")
with app.app_context():
from guardrails_api.blueprints.guards import guard_client
for g in guard_client.get_guards():
g = g.to_dict()
console.print(f"- Guard: [bold white]{g.get('name')}[/bold white] {self_endpoint}/guards/{g.get('name')}/openai/v1")

console.print("")
console.print(Rule("[bold grey]Server Logs[/bold grey]", characters="=", style="white"))

return app
return app
5 changes: 4 additions & 1 deletion guardrails_api/clients/cache_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import threading
from flask_caching import Cache


# TODO: Add option to connect to Redis or MemCached backend with environment variables
class CacheClient:
_instance = None
_lock = threading.Lock()

def __new__(cls):
if cls._instance is None:
cls._instance = super(CacheClient, cls).__new__(cls)
with cls._lock:
cls._instance = super(CacheClient, cls).__new__(cls)
return cls._instance

def initialize(self, app):
Expand Down
5 changes: 4 additions & 1 deletion guardrails_api/clients/postgres_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boto3
import json
import os
import threading
from flask import Flask
from sqlalchemy import text
from typing import Tuple
Expand All @@ -13,10 +14,12 @@ def postgres_is_enabled() -> bool:

class PostgresClient:
_instance = None
_lock = threading.Lock()

def __new__(cls):
if cls._instance is None:
cls._instance = super(PostgresClient, cls).__new__(cls)
with cls._lock:
cls._instance = super(PostgresClient, cls).__new__(cls)
return cls._instance

def fetch_pg_secret(self, secret_arn: str) -> dict:
Expand Down