From 55fe73d00166360725236242d382578c9184cb27 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 9 Jan 2026 15:38:56 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(backend)=20use=20langfuse=20to=20moni?= =?UTF-8?q?tor=20AI=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to monitor AI actions. For this we choose to use langfuse. As this usage is optional, we load langfuse sdk only if settings are configured. Also, the openai client from langfuse is a dropin replacement of openai client, so we only have to change how openai is imported. --- CHANGELOG.md | 1 + docs/env.md | 3 +++ src/backend/core/services/ai_services.py | 8 ++++++-- src/backend/impress/settings.py | 10 ++++++++++ src/backend/pyproject.toml | 1 + 5 files changed, 21 insertions(+), 2 deletions(-) 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",