Julia Ostheimer commited on
Commit
599b085
·
1 Parent(s): 3051bba

Initialize the Langfuse client in its own script and refactor code

Browse files
Files changed (3) hide show
  1. app.py +3 -15
  2. conversation/generate.py +6 -15
  3. tools/langfuse_client.py +22 -0
app.py CHANGED
@@ -9,8 +9,6 @@ from langgraph.checkpoint.memory import MemorySaver
9
  from langgraph.graph import MessagesState, StateGraph, END
10
  from langgraph.prebuilt import ToolNode, tools_condition
11
  from langgraph.prebuilt import ToolNode
12
- from langfuse import Langfuse, get_client
13
- from langfuse.langchain import CallbackHandler
14
 
15
  import structlog
16
 
@@ -21,23 +19,13 @@ import logging_config as _
21
  # from conversation.main import graph
22
  from conversation.generate import generate
23
  from ingestion.main import ingest_document
 
24
 
25
  from config import app_settings
26
 
27
 
28
- # Initialize Langfuse client with constructor arguments
29
- Langfuse(
30
- public_key=app_settings.langfuse_public_api_key,
31
- secret_key=app_settings.langfuse_secret_api_key,
32
- host=app_settings.langfuse_host
33
- )
34
-
35
- # Get the configured client instance
36
- langfuse = get_client()
37
-
38
- # Initialize the Langfuse handler
39
- langfuse_handler = CallbackHandler()
40
-
41
 
42
  # Create a logger instance
43
  logger = structlog.get_logger(__name__)
 
9
  from langgraph.graph import MessagesState, StateGraph, END
10
  from langgraph.prebuilt import ToolNode, tools_condition
11
  from langgraph.prebuilt import ToolNode
 
 
12
 
13
  import structlog
14
 
 
19
  # from conversation.main import graph
20
  from conversation.generate import generate
21
  from ingestion.main import ingest_document
22
+ from tools.langfuse_client import get_langfuse_client_and_handler
23
 
24
  from config import app_settings
25
 
26
 
27
+ # Get Langfuse client and Langfuse Callback handler
28
+ langfuse_client, langfuse_handler = get_langfuse_client_and_handler()
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Create a logger instance
31
  logger = structlog.get_logger(__name__)
conversation/generate.py CHANGED
@@ -8,10 +8,12 @@ from langchain_core.prompts import (
8
  )
9
  from langchain_core.runnables import RunnableParallel
10
  from langgraph.graph import MessagesState
11
- from langfuse import Langfuse, get_client
12
- from langfuse.langchain import CallbackHandler
13
  from pydantic import BaseModel
14
 
 
 
 
 
15
  from config import app_settings
16
 
17
  logger = structlog.get_logger(__name__)
@@ -21,20 +23,9 @@ llm = init_chat_model(
21
  model_provider="openai",
22
  api_key=app_settings.llm_api_key
23
  )
24
-
25
- # Initialize Langfuse client with constructor arguments
26
- Langfuse(
27
- public_key=app_settings.langfuse_public_api_key,
28
- secret_key=app_settings.langfuse_secret_api_key,
29
- host=app_settings.langfuse_host
30
- )
31
-
32
- # Get the configured client instance
33
- langfuse = get_client()
34
-
35
- # Initialize the Langfuse handler
36
- langfuse_handler = CallbackHandler()
37
 
 
 
38
 
39
  # RAG answer synthesis prompt
40
  system_template = """
 
8
  )
9
  from langchain_core.runnables import RunnableParallel
10
  from langgraph.graph import MessagesState
 
 
11
  from pydantic import BaseModel
12
 
13
+
14
+ # Write the import statement to import the function get_langfuse_client_and_handler from the langfuse_client module
15
+ from tools.langfuse_client import get_langfuse_client_and_handler
16
+
17
  from config import app_settings
18
 
19
  logger = structlog.get_logger(__name__)
 
23
  model_provider="openai",
24
  api_key=app_settings.llm_api_key
25
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Get Langfuse client and Langfuse Callback handler
28
+ langfuse_client, langfuse_handler = get_langfuse_client_and_handler()
29
 
30
  # RAG answer synthesis prompt
31
  system_template = """
tools/langfuse_client.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langfuse import Langfuse, get_client
2
+ from langfuse.langchain import CallbackHandler
3
+
4
+ from config import app_settings
5
+
6
+
7
+ # Initialize Langfuse client with constructor arguments
8
+ Langfuse(
9
+ public_key=app_settings.langfuse_public_api_key,
10
+ secret_key=app_settings.langfuse_secret_api_key,
11
+ host=app_settings.langfuse_host
12
+ )
13
+
14
+ # Get the configured client instance
15
+ langfuse_client = get_client()
16
+
17
+ # Initialize the Langfuse handler
18
+ langfuse_handler = CallbackHandler()
19
+
20
+
21
+ def get_langfuse_client_and_handler():
22
+ return langfuse_client, langfuse_handler