-
Notifications
You must be signed in to change notification settings - Fork 36
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
Lifei/branch from ajgray #238
Draft
lifeizhou-ap
wants to merge
10
commits into
main
Choose a base branch
from
lifei/branch-from-ajgray
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
296f664
create base class / wrapper method
ajgray-stripe 7656095
update imports to use the more general wrapper
ajgray-stripe 0581cfd
plugin loading, move langfuse to an observer
ajgray-stripe 268b775
add/modify tests
ajgray-stripe aaca598
docs
ajgray-stripe 59e5716
linting, formatting
ajgray-stripe ca22a27
ruff fix
michaelneale c9a7c77
ruff fix
michaelneale ea019ec
added some suggestions
lifeizhou-ap 098480b
rename to session id
lifeizhou-ap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,20 @@ | ||
from functools import wraps | ||
from typing import Callable | ||
|
||
from exchange.observers.base import ObserverManager | ||
|
||
|
||
def observe_wrapper(*args, **kwargs) -> Callable: # noqa: ANN002, ANN003 | ||
"""Decorator to wrap a function with all registered observer plugins, dynamically fetched.""" | ||
|
||
def wrapper(func: Callable) -> Callable: | ||
@wraps(func) | ||
def dynamic_wrapped(*func_args, **func_kwargs) -> Callable: # noqa: ANN002, ANN003 | ||
wrapped = func | ||
for observer in ObserverManager.get_instance()._observers: | ||
wrapped = observer.observe_wrapper(*args, **kwargs)(wrapped) | ||
return wrapped(*func_args, **func_kwargs) | ||
|
||
return dynamic_wrapped | ||
|
||
return wrapper |
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,43 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Callable, Type | ||
|
||
|
||
class Observer(ABC): | ||
@abstractmethod | ||
def initialize(self) -> None: | ||
pass | ||
|
||
@abstractmethod | ||
def observe_wrapper(*args, **kwargs) -> Callable: # noqa: ANN002, ANN003 | ||
pass | ||
|
||
@abstractmethod | ||
def finalize(self) -> None: | ||
pass | ||
|
||
|
||
class ObserverManager: | ||
_instance = None | ||
_observers: list[Observer] = [] | ||
|
||
@classmethod | ||
def get_instance(cls: Type["ObserverManager"]) -> "ObserverManager": | ||
if cls._instance is None: | ||
cls._instance = cls() | ||
return cls._instance | ||
|
||
def initialize(self, tracing: bool, observers: list[Observer]) -> None: | ||
from exchange.observers.langfuse import LangfuseObserver | ||
|
||
self._observers = observers | ||
for observer in self._observers: | ||
# LangfuseObserver has special behavior when tracing is _dis_abled. | ||
# Consider refactoring to make this less special-casey if that's common. | ||
if isinstance(observer, LangfuseObserver) and not tracing: | ||
observer.initialize_with_disabled_tracing() | ||
elif tracing: | ||
observer.initialize() | ||
|
||
def finalize(self) -> None: | ||
for observer in self._observers: | ||
observer.finalize() |
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,98 @@ | ||
""" | ||
Langfuse Observer | ||
|
||
This observer provides integration with Langfuse, a tool for monitoring and tracing LLM applications. | ||
|
||
Usage: | ||
Include "langfuse" in your profile's list of observers to enable Langfuse integration. | ||
It automatically checks for Langfuse credentials in the .env.langfuse file and for a running Langfuse server. | ||
If these are found, it will set up the necessary client and context for tracing. | ||
|
||
Note: | ||
Run setup_langfuse.sh which automates the steps for running local Langfuse. | ||
""" | ||
|
||
import logging | ||
import os | ||
import sys | ||
from functools import cache, wraps | ||
from io import StringIO | ||
from typing import Callable | ||
|
||
from langfuse.decorators import langfuse_context | ||
|
||
from exchange.observers.base import Observer | ||
|
||
## These are the default configurations for local Langfuse server | ||
## Please refer to .env.langfuse.local file for local langfuse server setup configurations | ||
DEFAULT_LOCAL_LANGFUSE_HOST = "http://localhost:3000" | ||
DEFAULT_LOCAL_LANGFUSE_PUBLIC_KEY = "publickey-local" | ||
DEFAULT_LOCAL_LANGFUSE_SECRET_KEY = "secretkey-local" | ||
|
||
|
||
@cache | ||
def auth_check() -> bool: | ||
# Temporarily redirect stdout and stderr to suppress print statements from Langfuse | ||
temp_stderr = StringIO() | ||
sys.stderr = temp_stderr | ||
|
||
# Set environment variables if not specified | ||
os.environ.setdefault("LANGFUSE_PUBLIC_KEY", DEFAULT_LOCAL_LANGFUSE_PUBLIC_KEY) | ||
os.environ.setdefault("LANGFUSE_SECRET_KEY", DEFAULT_LOCAL_LANGFUSE_SECRET_KEY) | ||
os.environ.setdefault("LANGFUSE_HOST", DEFAULT_LOCAL_LANGFUSE_HOST) | ||
|
||
auth_val = langfuse_context.auth_check() | ||
|
||
# Restore stderr | ||
sys.stderr = sys.__stderr__ | ||
return auth_val | ||
|
||
|
||
class LangfuseObserver(Observer): | ||
def initialize(self) -> None: | ||
langfuse_auth = auth_check() | ||
if langfuse_auth: | ||
print("Local Langfuse initialized. View your traces at http://localhost:3000") | ||
else: | ||
raise RuntimeError( | ||
"You passed --tracing, but a Langfuse object was not found in the current context. " | ||
"Please initialize the local Langfuse server and restart Goose." | ||
) | ||
|
||
langfuse_context.configure(enabled=True) | ||
self.tracing = True | ||
|
||
def initialize_with_disabled_tracing(self) -> None: | ||
logging.getLogger("langfuse").setLevel(logging.ERROR) | ||
langfuse_context.configure(enabled=False) | ||
self.tracing = False | ||
|
||
def session_id_wrapper(self, func: Callable, session_id) -> Callable: | ||
@wraps(func) # This will preserve the metadata of 'func' | ||
def wrapper(*args, **kwargs): | ||
langfuse_context.update_current_trace(session_id=session_id) | ||
return func(*args, **kwargs) | ||
return wrapper | ||
|
||
def observe_wrapper(self, *args, **kwargs) -> Callable: # noqa: ANN002, ANN003 | ||
def _wrapper(fn: Callable) -> Callable: | ||
if self.tracing and auth_check(): | ||
|
||
@wraps(fn) | ||
def wrapped_fn(*fargs, **fkwargs) -> Callable: # noqa: ANN002, ANN003 | ||
# group all traces under the same session | ||
if "session_id" in kwargs: | ||
session_id_function = kwargs.pop("session_id") | ||
session_id_value = session_id_function(fargs[0]) | ||
modified_fn = self.session_id_wrapper(fn, session_id_value) | ||
return langfuse_context.observe(*args, **kwargs)(modified_fn)(*fargs, **fkwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrap the update_current_trace call inside the observer wrapper |
||
else: | ||
return langfuse_context.observe(*args, **kwargs)(fn)(*fargs, **fkwargs) | ||
return wrapped_fn | ||
else: | ||
return fn | ||
|
||
return _wrapper | ||
|
||
def finalize(self) -> None: | ||
langfuse_context.flush() |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
used the session_id param in decorator instead of checking the function name "reply". (This is not related to the issue)