diff --git a/CHANGELOG.md b/CHANGELOG.md index 3574246d..d07f6750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to - ✅(export) add PDF regression tests #1762 - 📝(docs) Add language configuration documentation #1757 - 🔒(helm) Set default security context #1750 +- ✨(backend) use langfuse to monitor AI actions ### Fixed diff --git a/docs/env.md b/docs/env.md index 43c1bade..6576f332 100644 --- a/docs/env.md +++ b/docs/env.md @@ -64,6 +64,9 @@ These are the environment variables you can set for the `impress-backend` contai | FRONTEND_HOMEPAGE_FEATURE_ENABLED | Frontend feature flag to display the homepage | false | | FRONTEND_THEME | Frontend theme to use | | | LANGUAGE_CODE | Default language | en-us | +| LANGFUSE_SECRET_KEY | The Langfuse secret key used by the sdk | None | +| LANGFUSE_PUBLIC_KEY | The Langfuse public key used by the sdk | None | +| LANGFUSE_BASE_URL | The Langfuse base url used by the sdk | None | | LASUITE_MARKETING_BACKEND | Backend used when SIGNUP_NEW_USER_TO_MARKETING_EMAIL is True. See https://github.com/suitenumerique/django-lasuite/blob/main/documentation/how-to-use-marketing-backend.md | lasuite.marketing.backends.dummy.DummyBackend | | LASUITE_MARKETING_PARAMETERS | The parameters to configure LASUITE_MARKETING_BACKEND. See https://github.com/suitenumerique/django-lasuite/blob/main/documentation/how-to-use-marketing-backend.md | {} | | LOGGING_LEVEL_LOGGERS_APP | Application logging level. options are "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL" | INFO | diff --git a/src/backend/core/services/ai_services.py b/src/backend/core/services/ai_services.py index 4b110584..7b4a560c 100644 --- a/src/backend/core/services/ai_services.py +++ b/src/backend/core/services/ai_services.py @@ -3,10 +3,14 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured -from openai import OpenAI - from core import enums +if settings.LANGFUSE_PUBLIC_KEY: + from langfuse.openai import OpenAI +else: + from openai import OpenAI + + AI_ACTIONS = { "prompt": ( "Answer the prompt using markdown formatting for structure and emphasis. " diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index 33228b05..c6e29efb 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -699,6 +699,16 @@ class Base(Configuration): "day": 200, } + LANGFUSE_SECRET_KEY = SecretFileValue( + None, environ_name="LANGFUSE_SECRET_KEY", environ_prefix=None + ) + LANGFUSE_PUBLIC_KEY = values.Value( + None, environ_name="LANGFUSE_PUBLIC_KEY", environ_prefix=None + ) + LANGFUSE_BASE_URL = values.Value( + None, environ_name="LANGFUSE_BASE_URL", environ_prefix=None + ) + # Y provider microservice Y_PROVIDER_API_KEY = SecretFileValue( environ_name="Y_PROVIDER_API_KEY", diff --git a/src/backend/pyproject.toml b/src/backend/pyproject.toml index 1b7e829b..f58d055b 100644 --- a/src/backend/pyproject.toml +++ b/src/backend/pyproject.toml @@ -48,6 +48,7 @@ dependencies = [ "factory_boy==3.3.3", "gunicorn==23.0.0", "jsonschema==4.25.1", + "langfuse==3.11.2", "lxml==6.0.2", "markdown==3.10", "mozilla-django-oidc==5.0.2",