fix: prevent langfuse initialization (#4057)

This commit is contained in:
Ítalo Johnny 2024-10-07 17:38:28 -03:00 committed by GitHub
commit f7e58f96b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -30,22 +30,19 @@ class LangFuseTracer(BaseTracer):
self.flow_id = trace_name.split(" - ")[-1]
self.last_span: StatefulSpanClient | None = None
self.spans: dict = {}
self._ready: bool = self.setup_langfuse()
config = self._get_config()
self._ready: bool = self.setup_langfuse(config) if config else False
@property
def ready(self):
return self._ready
def setup_langfuse(self) -> bool:
def setup_langfuse(self, config) -> bool:
try:
from langfuse import Langfuse
from langfuse.callback.langchain import LangchainCallbackHandler
config = self._get_config()
if not all(config.values()):
msg = "Missing Langfuse configuration"
raise ValueError(msg)
self._client = Langfuse(**config)
self.trace = self._client.trace(id=str(self.trace_id), name=self.flow_id)
@ -134,8 +131,10 @@ class LangFuseTracer(BaseTracer):
return None
return None # self._callback
def _get_config(self):
def _get_config(self) -> dict:
secret_key = os.getenv("LANGFUSE_SECRET_KEY", None)
public_key = os.getenv("LANGFUSE_PUBLIC_KEY", None)
host = os.getenv("LANGFUSE_HOST", None)
return {"secret_key": secret_key, "public_key": public_key, "host": host}
if secret_key and public_key and host:
return {"secret_key": secret_key, "public_key": public_key, "host": host}
return {}