diff --git a/packages/exchange/src/exchange/langfuse/langfuse.py b/packages/exchange/src/exchange/langfuse/langfuse.py index 5f92d0c64..a4f475879 100644 --- a/packages/exchange/src/exchange/langfuse/langfuse.py +++ b/packages/exchange/src/exchange/langfuse/langfuse.py @@ -19,11 +19,13 @@ LANGFUSE_LOCAL_ENV_FILE_NAME = ".env.langfuse.local" LANGFUSE_LOCAL_INIT_ENV_FILE = os.path.join(SCRIPT_DIR, LANGFUSE_LOCAL_ENV_FILE_NAME) + def _run_command(command): """Run a shell command.""" result = subprocess.run(command, shell=True, check=True, text=True) return result + def _update_or_clone_repo(): """Update or clone the Langfuse repository.""" if os.path.isdir(os.path.join(LANGFUSE_CLONE_DIR, ".git")): @@ -31,6 +33,7 @@ def _update_or_clone_repo(): else: _run_command(f"git clone {LANGFUSE_REPO_URL} {LANGFUSE_CLONE_DIR}") + def _copy_env_file(): """Copy the environment file to the Langfuse clone directory.""" if os.path.isfile(LANGFUSE_LOCAL_INIT_ENV_FILE): @@ -39,10 +42,12 @@ def _copy_env_file(): logger.error("Environment file not found. Exiting.") exit(1) + def _start_docker_compose(): """Start Docker containers for Langfuse service and Postgres db.""" _run_command(f"cd {LANGFUSE_CLONE_DIR} && docker compose --env-file ./{LANGFUSE_LOCAL_ENV_FILE_NAME} up --detach") + def _wait_for_service(): """Wait for the Langfuse service to be available on localhost:3000.""" while True: @@ -52,11 +57,12 @@ def _wait_for_service(): except OSError: time.sleep(1) + def _launch_browser(): """Launch the default web browser to open the Langfuse service URL.""" system = platform.system().lower() url = "http://localhost:3000" - + if "linux" in system: subprocess.run(["xdg-open", url]) elif "darwin" in system: # macOS @@ -64,6 +70,7 @@ def _launch_browser(): else: logger.info("Please open http://localhost:3000 to view Langfuse traces.") + def _print_login_variables(): """Read and print the default email and password from the environment file.""" if os.path.isfile(LANGFUSE_LOCAL_INIT_ENV_FILE): @@ -79,6 +86,7 @@ def _print_login_variables(): else: logger.warning("Langfuse environment file with local credentials required for local login not found.") + def setup_langfuse(): """Main function to set up and run the Langfuse service.""" try: @@ -96,6 +104,7 @@ def setup_langfuse(): except Exception as e: logger.warning(f"Trouble finding Langfuse or Langfuse credentials: {e}") + def observe_wrapper(*args, **kwargs) -> Callable: """ A decorator that wraps a function with Langfuse context observation if credentials are available. @@ -119,4 +128,5 @@ def _wrapper(fn: Callable) -> Callable: return _wrapper -setup_langfuse() \ No newline at end of file + +setup_langfuse()