Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions backend/app/api/routes/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
logger = logging.getLogger(__name__)
router = APIRouter(tags=["threads"])

from pydantic import BaseModel
from typing import Optional


class StartThreadRequest(BaseModel):
question: str = Field(..., description="The user's input question.")
Expand Down Expand Up @@ -243,6 +246,8 @@
_current_user: UserOrganization = Depends(get_current_user_org),
):
"""Asynchronous endpoint that processes requests in background."""

# Fetch OpenAI credentials (required)
credentials = get_provider_credential(
session=_session,
org_id=_current_user.organization_id,
Expand All @@ -251,6 +256,9 @@
)
client, success = configure_openai(credentials)
if not success:
logging.error(

Check warning on line 259 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L259

Added line #L259 was not covered by tests
f"Failed to configure OpenAI API for organization { _current_user.organization_id }"
)
return APIResponse.failure_response(
error="OpenAI API key not configured for this organization."
)
Expand All @@ -261,15 +269,17 @@
provider="langfuse",
project_id=request.get("project_id"),
)
if not langfuse_credentials:
raise HTTPException(404, "LANGFUSE keys not configured for this organization.")

# Configure Langfuse
_, success = configure_langfuse(langfuse_credentials)
if not success:
return APIResponse.failure_response(
error="Failed to configure Langfuse client."
if langfuse_credentials:
logging.info(

Check warning on line 274 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L273-L274

Added lines #L273 - L274 were not covered by tests
f"Langfuse credentials found for organization {_current_user.organization_id}. Enabling tracing."
)
_, success = configure_langfuse(langfuse_credentials, enabled=True)

Check warning on line 277 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L277

Added line #L277 was not covered by tests
else:
logging.info(

Check warning on line 279 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L279

Added line #L279 was not covered by tests
f"Langfuse credentials not found for organization {_current_user.organization_id}. Disabling tracing."
)
_, success = configure_langfuse({}, enabled=False)

Check warning on line 282 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L282

Added line #L282 was not covered by tests

# Validate thread
is_valid, error_message = validate_thread(client, request.get("thread_id"))
Expand Down
53 changes: 33 additions & 20 deletions backend/app/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,50 @@
return not errno


def configure_langfuse(credentials: dict) -> tuple[Langfuse, bool]:
def configure_langfuse(credentials: dict, enabled: bool) -> tuple[Langfuse, bool]:
"""
Configure Langfuse client and context with the provided credentials.
Configure Langfuse client and context with the provided credentials and tracing status.

Args:
credentials: Dictionary containing Langfuse credentials (public_key, secret_key, host)
enabled: Boolean to enable/disable Langfuse tracing.

Returns:
Tuple of (Langfuse client instance, success boolean)
"""
if not credentials:
return None, False

try:
# Configure Langfuse client
langfuse = Langfuse(
public_key=credentials["public_key"],
secret_key=credentials["secret_key"],
host=credentials.get("host", "https://cloud.langfuse.com"),
)

# Configure Langfuse context
langfuse_context.configure(
secret_key=credentials["secret_key"],
public_key=credentials["public_key"],
host=credentials.get("host", "https://cloud.langfuse.com"),
)
if credentials:

Check warning on line 52 in backend/app/core/util.py

View check run for this annotation

Codecov / codecov/patch

backend/app/core/util.py#L52

Added line #L52 was not covered by tests
# If credentials exist, configure Langfuse client and enable tracing
# Configure Langfuse client
langfuse = Langfuse(

Check warning on line 55 in backend/app/core/util.py

View check run for this annotation

Codecov / codecov/patch

backend/app/core/util.py#L55

Added line #L55 was not covered by tests
public_key=credentials["public_key"],
secret_key=credentials["secret_key"],
host=credentials.get("host", "https://cloud.langfuse.com"),
)

# Configure Langfuse context with tracing status
langfuse_context.configure(

Check warning on line 62 in backend/app/core/util.py

View check run for this annotation

Codecov / codecov/patch

backend/app/core/util.py#L62

Added line #L62 was not covered by tests
secret_key=credentials["secret_key"],
public_key=credentials["public_key"],
host=credentials.get("host", "https://cloud.langfuse.com"),
enabled=enabled,
)

return langfuse, True

Check warning on line 69 in backend/app/core/util.py

View check run for this annotation

Codecov / codecov/patch

backend/app/core/util.py#L69

Added line #L69 was not covered by tests

else:
# If no credentials, disable tracing
# Disable Langfuse context if no credentials are provided
langfuse_context.configure(

Check warning on line 74 in backend/app/core/util.py

View check run for this annotation

Codecov / codecov/patch

backend/app/core/util.py#L74

Added line #L74 was not covered by tests
secret_key=None,
public_key=None,
host=None,
enabled=False,
)

return None, False

Check warning on line 81 in backend/app/core/util.py

View check run for this annotation

Codecov / codecov/patch

backend/app/core/util.py#L81

Added line #L81 was not covered by tests

return langfuse, True
except Exception as e:
warnings.warn(f"Failed to configure Langfuse: {str(e)}")
return None, False


Expand Down