Skip to content

Commit 6d62ed4

Browse files
enh: use langchain's callback for langfuse instead of litellm's
Signed-off-by: thiswillbeyourgithub <[email protected]>
1 parent 71c64ca commit 6d62ed4

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ wdoc --path $link --task summarize --filetype "online_pdf"
106106
* **Statically typed**: Runtime type checking. Opt out with an environment flag: `WDOC_TYPECHECKING="disabled / warn / crash" wdoc` (by default: `warn`). Thanks to [beartype](https://beartype.readthedocs.io/en/latest/) it shouldn't even slow down the code!
107107
* **LLM (and embeddings) caching**: speed things up, as well as index storing and loading (handy for large collections).
108108
* **Good PDF parsing** PDF parsers are notoriously unreliable, so 15 (!) different loaders are used, and the best according to a parsing scorer is kept. Including table support via [openparse](https://github.com/Filimoa/open-parse/) (no GPU needed by default) or via [UnstructuredPDFLoader](https://python.langchain.com/docs/integrations/document_loaders/unstructured_pdfloader/).
109-
* **Langfuse support**: If you set the appropriate langfuse environment variables they will be used. See [this guide to learn more](https://langfuse.com/docs/integrations/litellm/tracing) (Note: disabled if using private_mode)
109+
* **Langfuse support**: If you set the appropriate langfuse environment variables they will be used. See [this guide](https://langfuse.com/docs/integrations/langchain/tracing) or [this one](https://langfuse.com/docs/integrations/litellm/tracing) to learn more (Note: this is disabled if using private_mode to avoid any leaks).
110110
* **Document filtering**: based on regex for document content or metadata.
111111
* **Fast**: Parallel document loading, parsing, embeddings, querying, etc.
112112
* **Shell autocompletion** using [python-fire](https://github.com/google/python-fire/blob/master/docs/using-cli.md#completion-flag)

wdoc/utils/llm.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@
2424

2525
TESTING_LLM = "testing/testing"
2626

27+
if (
28+
"LANGFUSE_PUBLIC_KEY" in os.environ and
29+
"LANGFUSE_SECRET_KEY" in os.environ and
30+
"LANGFUSE_HOST" in os.environ
31+
) and not is_private:
32+
red("Activating langfuse callbacks")
33+
try:
34+
# # litellm's callbacks seem more flawed than langchain's
35+
# import langfuse
36+
# litellm.success_callback = ["langfuse"]
37+
# litellm.failure_callback = ["langfuse"]
38+
39+
from langfuse.callback import CallbackHandler as LangfuseCallback
40+
langfuse_handler = [LangfuseCallback(
41+
secret_key=os.environ["LANGFUSE_SECRET_KEY"],
42+
public_key=os.environ["LANGFUSE_PUBLIC_KEY"],
43+
host=os.environ["LANGFUSE_HOST"],
44+
)]
45+
except Exception as e:
46+
red(f"Failed to setup langfuse callback, make sure package 'langfuse' is installed. The error was: ''{e}'")
47+
langfuse_handler = []
48+
2749

2850
@optional_typecheck
2951
def load_llm(
@@ -104,13 +126,14 @@ def load_llm(
104126
assert os.environ[f"{backend.upper()}_API_KEY"] == "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
105127

106128
assert os.environ[f"{backend.upper()}_API_KEY"] == "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
129+
assert not langfuse_handler, "Private argument but langfuse_handler appears set. Something went wrong so crashing just to be safe."
107130
else:
108131
assert not WDOC_PRIVATE_MODE
109132
assert "WDOC_PRIVATE_MODE" not in os.environ or os.environ["WDOC_PRIVATE_MODE"] == "false"
110133

111134
assert private == is_private
112135

113-
if (not private) and (backend == "openai") and (api_base is None) and ("langfuse" not in litellm.success_callback):
136+
if (not private) and (backend == "openai") and (api_base is None):
114137
max_tokens = litellm.get_model_info(modelname)["max_tokens"]
115138
if "max_tokens" not in extra_model_args:
116139
extra_model_args["max_tokens"] = int(max_tokens * 0.9)
@@ -119,7 +142,7 @@ def load_llm(
119142
cache=llm_cache,
120143
disable_streaming=True, # Not needed and might break cache
121144
verbose=llm_verbosity,
122-
callbacks=[PriceCountingCallback(verbose=llm_verbosity)],
145+
callbacks=[PriceCountingCallback(verbose=llm_verbosity)] + langfuse_handler,
123146
**extra_model_args,
124147
)
125148
else:
@@ -133,7 +156,7 @@ def load_llm(
133156
cache=llm_cache,
134157
verbose=llm_verbosity,
135158
tags=tags,
136-
callbacks=[PriceCountingCallback(verbose=llm_verbosity)],
159+
callbacks=[PriceCountingCallback(verbose=llm_verbosity)] + langfuse_handler,
137160
**extra_model_args,
138161
)
139162
litellm.drop_params = True

wdoc/wdoc.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,6 @@
8282

8383
os.environ["TOKENIZERS_PARALLELISM"] = "true"
8484

85-
if (
86-
"LANGFUSE_PUBLIC_KEY" in os.environ and
87-
"LANGFUSE_SECRET_KEY" in os.environ and
88-
"LANGFUSE_HOST" in os.environ
89-
) and not is_private:
90-
red("Activating LANGFUSE litellm callbacks (meaning litellm's backend well be used, even if you're calling openai)")
91-
try:
92-
import langfuse
93-
litellm.success_callback = ["langfuse"]
94-
litellm.failure_callback = ["langfuse"]
95-
except Exception as e:
96-
red(f"Failed to import langfuse, make sure it's installed. Error was: ''{e}'")
97-
9885

9986
@optional_typecheck
10087
@set_USAGE_as_docstring

0 commit comments

Comments
 (0)