Skip to content

Commit fa509ad

Browse files
Akhilesh NegiAkhilesh Negi
authored andcommitted
using utils
1 parent 168f521 commit fa509ad

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

backend/app/api/routes/evaluation.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from app.utils import APIResponse
99
from app.crud.credentials import get_provider_credential
1010
from app.api.routes.threads import threads_sync
11+
from app.core.util import configure_langfuse
1112

1213
router = APIRouter(tags=["evaluation"])
1314

@@ -47,17 +48,11 @@ async def evaluate_threads(
4748
)
4849

4950
# Configure Langfuse
50-
langfuse = Langfuse(
51-
public_key=langfuse_credentials["public_key"],
52-
secret_key=langfuse_credentials["secret_key"],
53-
host=langfuse_credentials.get("host", "https://cloud.langfuse.com"),
54-
)
55-
56-
langfuse_context.configure(
57-
secret_key=langfuse_credentials["secret_key"],
58-
public_key=langfuse_credentials["public_key"],
59-
host=langfuse_credentials.get("host", "https://cloud.langfuse.com"),
60-
)
51+
langfuse, success = configure_langfuse(langfuse_credentials)
52+
if not success:
53+
return APIResponse.failure_response(
54+
error="Failed to configure Langfuse client."
55+
)
6156

6257
try:
6358
# Get dataset

backend/app/api/routes/threads.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from app.utils import APIResponse
1515
from app.crud.credentials import get_provider_credential
1616
from app.core.security import decrypt_credentials
17+
from app.core.util import configure_langfuse
1718

1819
logger = logging.getLogger(__name__)
1920
router = APIRouter(tags=["threads"])
@@ -245,11 +246,13 @@ async def threads(
245246
error="LANGFUSE keys not configured for this organization."
246247
)
247248

248-
langfuse_context.configure(
249-
secret_key=langfuse_credentials["secret_key"],
250-
public_key=langfuse_credentials["public_key"],
251-
host=langfuse_credentials["host"],
252-
)
249+
# Configure Langfuse
250+
_, success = configure_langfuse(langfuse_credentials)
251+
if not success:
252+
return APIResponse.failure_response(
253+
error="Failed to configure Langfuse client."
254+
)
255+
253256
# Validate thread
254257
is_valid, error_message = validate_thread(client, request.get("thread_id"))
255258
if not is_valid:
@@ -310,11 +313,11 @@ async def threads_sync(
310313
)
311314

312315
# Configure Langfuse
313-
langfuse_context.configure(
314-
secret_key=langfuse_credentials["secret_key"],
315-
public_key=langfuse_credentials["public_key"],
316-
host=langfuse_credentials.get("host", "https://cloud.langfuse.com"),
317-
)
316+
_, success = configure_langfuse(langfuse_credentials)
317+
if not success:
318+
return APIResponse.failure_response(
319+
error="Failed to configure Langfuse client."
320+
)
318321

319322
# Validate thread
320323
is_valid, error_message = validate_thread(client, request.get("thread_id"))

backend/app/core/util.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from fastapi import HTTPException
66
from requests import Session, RequestException
77
from pydantic import BaseModel, HttpUrl
8+
from langfuse import Langfuse
9+
from langfuse.decorators import langfuse_context
810

911

1012
def now():
@@ -32,3 +34,37 @@ def post_callback(url: HttpUrl, payload: BaseModel):
3234
errno += 1
3335

3436
return not errno
37+
38+
39+
def configure_langfuse(credentials: dict) -> tuple[Langfuse, bool]:
40+
"""
41+
Configure Langfuse client and context with the provided credentials.
42+
43+
Args:
44+
credentials: Dictionary containing Langfuse credentials (public_key, secret_key, host)
45+
46+
Returns:
47+
Tuple of (Langfuse client instance, success boolean)
48+
"""
49+
if not credentials:
50+
return None, False
51+
52+
try:
53+
# Configure Langfuse client
54+
langfuse = Langfuse(
55+
public_key=credentials["public_key"],
56+
secret_key=credentials["secret_key"],
57+
host=credentials.get("host", "https://cloud.langfuse.com"),
58+
)
59+
60+
# Configure Langfuse context
61+
langfuse_context.configure(
62+
secret_key=credentials["secret_key"],
63+
public_key=credentials["public_key"],
64+
host=credentials.get("host", "https://cloud.langfuse.com"),
65+
)
66+
67+
return langfuse, True
68+
except Exception as e:
69+
warnings.warn(f"Failed to configure Langfuse: {str(e)}")
70+
return None, False

0 commit comments

Comments
 (0)