Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Simplifed langfuse auth check #177

Merged
merged 5 commits into from
Oct 22, 2024

Conversation

lifeizhou-ap
Copy link
Collaborator

@lifeizhou-ap lifeizhou-ap commented Oct 21, 2024

What
Currently the env.langfuse.local is used for 2 purposes:

  1. Start the langfuse server on localhost
  2. The host, public key, private key is used in goose application as a langfuse client to send tracing data.

For item 2, it would be better to specify the values on the goose client side (user) instead of packaging into the exchange package, either via loading an environment file that user specifies the path or loading the environment variable that user sets.

What
In exchange, rendering the environment variables for langfuse host, public key, private key, if any of them does not exists, fallback to the default variable for localhost langfuse

Copy link
Collaborator

@ahau-square ahau-square left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! thanks for cleaning this up

start_path = start_path.parent
return None
DEFAULT_LOCAL_LANGFUSE_HOST = "http://localhost:3000"
DEFAULT_LOCAL_LANGFUSE_PUBLIC_KEY = "publickey-local"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe add a comment here that the public/private key defaults come from the .env.langfuse.local file. As these are what langfuse is locally initialized with as defined there. These values are not actually default from Langfuse itself.



@cache
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! this is a lot cleaner

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come we're memoizing this? seems like a premature optimization?

the env check is cheap enough it shouldn't matter. now goose can't get new env variables when set externally (i.e. next tracing event) because there is no cache invalidation

Copy link
Collaborator Author

@lifeizhou-ap lifeizhou-ap Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically I did not change the existing behaviour. The purpose of the PR is mainly to remove the assumption of .env.langfuse.local file is part of exchange package, and rendering the local Langfuse credential from environment variables. (or it can be rendered from a environment file provided by goose application)

At the moment, I still leave the @cache method here as it is overkill to call the auth_check every time a function with observe_wrapper decorator is called.

By looking into the implementation, currently we rely on the auth_check to decide whether we want to trace the api call instead of deciding by whether the user passes --tracing option. I think the improvement for this part could be:

  • if goose session starts with --tracing, the application should raise with error message when error occurs with tracing
  • If goose session starts without --tracing, then we don't need to perform auth_check

from langfuse.decorators import langfuse_context
import sys
from io import StringIO
from pathlib import Path
from functools import wraps # Add this import
from functools import cache, wraps # Add this import
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comment unnecessary

def test_function_is_wrapped(mock_langfuse_context):
mock_observe = MagicMock(side_effect=lambda *args, **kwargs: lambda fn: fn)
mock_langfuse_context.observe = mock_observe
with patch("exchange.langfuse_wrapper.auth_check") as mocked_auth_check:
Copy link
Collaborator

@lamchau lamchau Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we go with this approach? i deliberately wrote the tests as a decorator to reduce how much nesting the testing code has

CURRENT_DIR = Path(__file__).parent
PACKAGE_ROOT = find_package_root(CURRENT_DIR)

LANGFUSE_ENV_FILE = os.path.join(PACKAGE_ROOT, ".env.langfuse.local") if PACKAGE_ROOT else None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didn't mind this approach, if feel what we could've improved here is deferred the loading so it wasn't during module init. moving everything to a environment variable just pollutes how we manage configs more

personally i feel approach here is to configure them in $XDG_CONFIG_HOME to centralize it.

@lamchau
Copy link
Collaborator

lamchau commented Oct 21, 2024

we shouldn't be introducing more environment variables rather we should be doing the opposite. if we really decide to keep going down this path we should at least namespace it (e.g. GOOSE_...)

shell environments are a global shared state.

anyhoo lgtm, just something we need to consider

Copy link
Collaborator

@lamchau lamchau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed this via standup - comments are non-blocking

@@ -9,8 +9,3 @@ LANGFUSE_INIT_USER_PASSWORD=localpwd
LANGFUSE_INIT_ORG_ID=local-id
LANGFUSE_INIT_ORG_NAME=local-org
LANGFUSE_INIT_PROJECT_ID=goose

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for posterity: long-term we'll consider moving this to a app level config

@lifeizhou-ap lifeizhou-ap merged commit 7749b8a into main Oct 22, 2024
4 checks passed
@lifeizhou-ap lifeizhou-ap deleted the lifei/simplified-langfuse-auth-check branch October 22, 2024 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants