Andrei Sferdian commited on
Commit
b249147
·
1 Parent(s): 993b644

Integrate with langfuse

Browse files
Files changed (3) hide show
  1. app.py +7 -1
  2. langfuse.py +36 -0
  3. requirements.txt +1 -0
app.py CHANGED
@@ -7,11 +7,14 @@ import requests
7
  from langchain_core.messages import HumanMessage
8
 
9
  from agent import agent
 
10
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
 
 
15
 
16
  def run_and_submit_all(profile: gr.OAuthProfile | None):
17
  """
@@ -89,7 +92,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
89
  "messages": [
90
  HumanMessage(content=question_text),
91
  ],
92
- }
 
 
 
93
  )
94
  submitted_answer = (
95
  response["messages"][-1].content
 
7
  from langchain_core.messages import HumanMessage
8
 
9
  from agent import agent
10
+ from langfuse import create_langfuse_handler
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
+ langfuse_handler = create_langfuse_handler()
17
+
18
 
19
  def run_and_submit_all(profile: gr.OAuthProfile | None):
20
  """
 
92
  "messages": [
93
  HumanMessage(content=question_text),
94
  ],
95
+ },
96
+ config={
97
+ "callbacks": [langfuse_handler],
98
+ },
99
  )
100
  submitted_answer = (
101
  response["messages"][-1].content
langfuse.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from langfuse.callback import CallbackHandler
4
+
5
+
6
+ def create_langfuse_handler(*args, **kwargs) -> CallbackHandler:
7
+ """
8
+ Create a Langfuse handler for tracking and logging.
9
+ """
10
+ public_key = kwargs.get("public_key") or (
11
+ args[0] if len(args) > 0 else os.getenv("LANGFUSE_PUBLIC_KEY")
12
+ )
13
+ secret_key = kwargs.get("secret_key") or (
14
+ args[1] if len(args) > 1 else os.getenv("LANGFUSE_SECRET_KEY")
15
+ )
16
+ host = kwargs.get("host") or (
17
+ args[2]
18
+ if len(args) > 2
19
+ else os.getenv("LANGFUSE_HOST", "https://api.langfuse.com")
20
+ )
21
+
22
+ if not public_key:
23
+ raise ValueError(
24
+ "Please provide a public key or set the environment variable 'LANGFUSE_PUBLIC_KEY'."
25
+ )
26
+ if not secret_key:
27
+ raise ValueError(
28
+ "Please provide a secret key or set the environment variable 'LANGFUSE_SECRET_KEY'."
29
+ )
30
+
31
+ return CallbackHandler(
32
+ public_key=public_key,
33
+ secret_key=secret_key,
34
+ host=host,
35
+ **kwargs,
36
+ )
requirements.txt CHANGED
@@ -70,6 +70,7 @@ langchain-openai==0.3.14
70
  langchain-text-splitters==0.3.8
71
  langchain-together==0.3.0
72
  langdetect==1.0.9
 
73
  langgraph==0.3.34
74
  langgraph-checkpoint==2.0.24
75
  langgraph-prebuilt==0.1.8
 
70
  langchain-text-splitters==0.3.8
71
  langchain-together==0.3.0
72
  langdetect==1.0.9
73
+ langfuse==2.60.3
74
  langgraph==0.3.34
75
  langgraph-checkpoint==2.0.24
76
  langgraph-prebuilt==0.1.8