From 9559f1d684f5114d24ac4495ddb2b1290704a39e Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 30 Aug 2023 09:49:38 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(langfuse.py):=20add=20temporar?= =?UTF-8?q?y=20implementation=20of=20LangfuseInstance=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The LangfuseInstance class is a temporary implementation that will be replaced by a plugin system in the future. This class provides methods for creating, getting, updating, and tearing down an instance of Langfuse. It uses the Langfuse library and credentials from the settings module to create the instance. If no credentials are found, the instance is set to None. The update method resets the instance and creates a new one. The teardown method flushes the instance and sets it to None. --- .../langflow/services/plugins/langfuse.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/backend/langflow/services/plugins/langfuse.py diff --git a/src/backend/langflow/services/plugins/langfuse.py b/src/backend/langflow/services/plugins/langfuse.py new file mode 100644 index 000000000..5031084b4 --- /dev/null +++ b/src/backend/langflow/services/plugins/langfuse.py @@ -0,0 +1,44 @@ +from langflow.utils.logger import logger + +### Temporary implementation +# This will be replaced by a plugin system once merged into 0.5.0 + + +class LangfuseInstance: + _instance = None + + @classmethod + def get(cls): + logger.debug("Getting Langfuse instance") + if cls._instance is None: + cls.create() + return cls._instance + + @classmethod + def create(cls): + logger.debug("Creating Langfuse instance") + from langflow.settings import settings + from langfuse import Langfuse + + if settings.LANGFUSE_PUBLIC_KEY and settings.LANGFUSE_SECRET_KEY: + logger.debug("Langfuse credentials found") + cls._instance = Langfuse( + public_key=settings.LANGFUSE_PUBLIC_KEY, + secret_key=settings.LANGFUSE_SECRET_KEY, + ) + else: + logger.debug("No Langfuse credentials found") + cls._instance = None + + @classmethod + def update(cls): + logger.debug("Updating Langfuse instance") + cls._instance = None + cls.create() + + @classmethod + def teardown(cls): + logger.debug("Tearing down Langfuse instance") + if cls._instance is not None: + cls._instance.flush() + cls._instance = None