Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
ahau-square committed Oct 7, 2024
1 parent 2729982 commit 2c1f7c4
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/exchange/src/exchange/langfuse/langfuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@
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")):
_run_command(f"git -C {LANGFUSE_CLONE_DIR} pull --rebase")
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):
Expand All @@ -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:
Expand All @@ -52,18 +57,20 @@ 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
subprocess.run(["open", url])
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):
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -119,4 +128,5 @@ def _wrapper(fn: Callable) -> Callable:

return _wrapper

setup_langfuse()

setup_langfuse()

0 comments on commit 2c1f7c4

Please sign in to comment.