-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add local langfuse tracing option (#106)
- Loading branch information
1 parent
30a1efe
commit 56d88a8
Showing
21 changed files
with
387 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lint.select = ["E", "W", "F", "N"] | ||
line-length = 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Langfuse Wrapper | ||
|
||
This package provides a wrapper for [Langfuse](https://langfuse.com/). The wrapper serves to initialize Langfuse appropriately if the Langfuse server is running locally and otherwise to skip applying the Langfuse observe descorators. | ||
|
||
**Note: This Langfuse integration is experimental and we don't currently have integration tests for it.** | ||
|
||
|
||
## Usage | ||
|
||
### Start your local Langfuse server | ||
|
||
Run `setup_langfuse.sh` to start your local Langfuse server. It requires Docker. | ||
|
||
Read more about local Langfuse deployments [here](https://langfuse.com/docs/deployment/local). | ||
|
||
### Exchange and Goose integration | ||
|
||
Import `from langfuse_wrapper.langfuse_wrapper import observe_wrapper` and use the `observe_wrapper()` decorator on functions you wish to enable tracing for. `observe_wrapper` functions the same way as Langfuse's observe decorator. | ||
|
||
Read more about Langfuse's decorator-based tracing [here](https://langfuse.com/docs/sdk/python/decorators). | ||
|
||
In Goose, initialization requires certain environment variables to be present: | ||
|
||
- `LANGFUSE_PUBLIC_KEY`: Your Langfuse public key | ||
- `LANGFUSE_SECRET_KEY`: Your Langfuse secret key | ||
- `LANGFUSE_BASE_URL`: The base URL of your Langfuse instance | ||
|
||
By default your local deployment and Goose will use the values in `env/.env.langfuse.local`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# These variables are default initialization variables for the Langfuse server | ||
LANGFUSE_INIT_PROJECT_NAME=goose-local | ||
LANGFUSE_INIT_PROJECT_PUBLIC_KEY=publickey-local | ||
LANGFUSE_INIT_PROJECT_SECRET_KEY=secretkey-local | ||
[email protected] | ||
LANGFUSE_INIT_USER_NAME=localdev | ||
LANGFUSE_INIT_USER_PASSWORD=localpwd | ||
|
||
LANGFUSE_INIT_ORG_ID=local-id | ||
LANGFUSE_INIT_ORG_NAME=local-org | ||
LANGFUSE_INIT_PROJECT_ID=goose | ||
|
||
# These variables are used by Goose | ||
LANGFUSE_PUBLIC_KEY=publickey-local | ||
LANGFUSE_SECRET_KEY=secretkey-local | ||
LANGFUSE_HOST=http://localhost:3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[project] | ||
name = "langfuse-wrapper" | ||
version = "0.1.0" | ||
description = "A wrapper for Langfuse integration" | ||
readme = "README.md" | ||
requires-python = ">=3.10" | ||
author = [{ name = "Block", email = "[email protected]" }] | ||
packages = [{ include = "langfuse_wrapper", from = "src" }] | ||
|
||
dependencies = [ | ||
"langfuse>=2.38.2", | ||
"python-dotenv>=1.0.1" | ||
] | ||
|
||
[tool.hatch.build.targets.wheel] | ||
packages = ["src/langfuse_wrapper"] | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[tool.uv] | ||
dev-dependencies = ["pytest>=8.3.2"] | ||
|
||
[tool.pytest.ini_options] | ||
markers = [ | ||
"integration: marks tests that need to authenticate (deselect with '-m \"not integration\"')", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/bin/bash | ||
|
||
# setup_langfuse.sh | ||
# | ||
# This script sets up and runs Langfuse locally for development and testing purposes. | ||
# | ||
# Key functionalities: | ||
# 1. Downloads the latest docker-compose.yaml from the Langfuse repository | ||
# 2. Starts Langfuse using Docker Compose with default initialization variables | ||
# 3. Waits for the service to be available | ||
# 4. Launches a browser to open the local Langfuse UI | ||
# 5. Prints login credentials from the environment file | ||
# | ||
# Usage: | ||
# ./setup_langfuse.sh | ||
# | ||
# Requirements: | ||
# - Docker | ||
# - curl | ||
# - A .env.langfuse.local file in the env directory | ||
# | ||
# Note: This script is intended for local development use only. | ||
|
||
set -e | ||
|
||
SCRIPT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")") | ||
LANGFUSE_DOCKER_COMPOSE_URL="https://raw.githubusercontent.com/langfuse/langfuse/main/docker-compose.yml" | ||
LANGFUSE_DOCKER_COMPOSE_FILE="docker-compose.yaml" | ||
LANGFUSE_ENV_FILE="$SCRIPT_DIR/../env/.env.langfuse.local" | ||
|
||
check_dependencies() { | ||
local dependencies=("curl" "docker") | ||
local missing_dependencies=() | ||
|
||
for cmd in "${dependencies[@]}"; do | ||
if ! command -v "$cmd" &> /dev/null; then | ||
missing_dependencies+=("$cmd") | ||
fi | ||
done | ||
|
||
if [ ${#missing_dependencies[@]} -ne 0 ]; then | ||
echo "Missing dependencies: ${missing_dependencies[*]}" | ||
exit 1 | ||
fi | ||
} | ||
|
||
download_docker_compose() { | ||
if ! curl --fail --location --output "$SCRIPT_DIR/docker-compose.yaml" "$LANGFUSE_DOCKER_COMPOSE_URL"; then | ||
echo "Failed to download docker-compose file from $LANGFUSE_DOCKER_COMPOSE_URL" | ||
exit 1 | ||
fi | ||
} | ||
|
||
start_docker_compose() { | ||
docker compose --env-file "$LANGFUSE_ENV_FILE" -f "$LANGFUSE_DOCKER_COMPOSE_FILE" up --detach | ||
} | ||
|
||
wait_for_service() { | ||
echo "Waiting for Langfuse to start..." | ||
local retries=10 | ||
local count=0 | ||
until curl --silent http://localhost:3000 > /dev/null; do | ||
((count++)) | ||
if [ "$count" -ge "$retries" ]; then | ||
echo "Max retries reached. Langfuse did not start in time." | ||
exit 1 | ||
fi | ||
sleep 1 | ||
done | ||
echo "Langfuse is now available!" | ||
} | ||
|
||
launch_browser() { | ||
if [[ "$OSTYPE" == "linux-gnu"* ]]; then | ||
xdg-open "http://localhost:3000" | ||
elif [[ "$OSTYPE" == "darwin"* ]]; then | ||
open "http://localhost:3000" | ||
else | ||
echo "Please open http://localhost:3000 to view Langfuse traces." | ||
fi | ||
} | ||
|
||
print_login_variables() { | ||
if [ -f "$LANGFUSE_ENV_FILE" ]; then | ||
echo "If not already logged in use the following credentials to log in:" | ||
grep -E "LANGFUSE_INIT_USER_EMAIL|LANGFUSE_INIT_USER_PASSWORD" "$LANGFUSE_ENV_FILE" | ||
else | ||
echo "Langfuse environment file with local credentials not found." | ||
fi | ||
} | ||
|
||
check_dependencies | ||
pushd "$SCRIPT_DIR" > /dev/null | ||
download_docker_compose | ||
start_docker_compose | ||
wait_for_service | ||
print_login_variables | ||
launch_browser | ||
popd > /dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from langfuse_wrapper.langfuse_wrapper import observe_wrapper # noqa | ||
|
||
module_name = "langfuse-wrapper" |
Oops, something went wrong.