Skip to content

Commit 2900865

Browse files
Akhilesh NegiAkhilesh Negi
authored andcommitted
updating threads codes
1 parent 0e64dfd commit 2900865

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

backend/app/api/routes/threads.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from app.core import logging, settings
1212
from app.models import UserOrganization
1313
from app.utils import APIResponse
14-
from app.crud import get_key_by_org
15-
from app.core.security import decrypt_api_key
14+
from app.crud.credentials import get_provider_credential
15+
from app.core.security import decrypt_credentials
1616

1717
logger = logging.getLogger(__name__)
1818
router = APIRouter(tags=["threads"])
@@ -169,15 +169,17 @@ async def threads(
169169
_current_user: UserOrganization = Depends(get_current_user_org),
170170
):
171171
"""Asynchronous endpoint that processes requests in background."""
172-
encrypted_key = get_key_by_org(
173-
session=_session, org_id=_current_user.organization_id
172+
credentials = get_provider_credential(
173+
session=_session,
174+
org_id=_current_user.organization_id,
175+
provider="openai",
176+
project_id=request.get("project_id"),
174177
)
175-
api_key = decrypt_api_key(encrypted_key)
176-
if not api_key:
178+
if not credentials or "api_key" not in credentials:
177179
return APIResponse.failure_response(
178-
error="API key not configured for this organization."
180+
error="OpenAI API key not configured for this organization."
179181
)
180-
client = OpenAI(api_key=api_key)
182+
client = OpenAI(api_key=credentials["api_key"])
181183

182184
langfuse_context.configure(
183185
secret_key=settings.LANGFUSE_SECRET_KEY,
@@ -218,16 +220,18 @@ async def threads_sync(
218220
):
219221
"""Synchronous endpoint that processes requests immediately."""
220222

221-
encrypted_key = get_key_by_org(
222-
session=_session, org_id=_current_user.organization_id
223+
credentials = get_provider_credential(
224+
session=_session,
225+
org_id=_current_user.organization_id,
226+
provider="openai",
227+
project_id=_current_user.project_id,
223228
)
224-
api_key = decrypt_api_key(encrypted_key)
225-
if not api_key:
229+
if not credentials or "api_key" not in credentials:
226230
return APIResponse.failure_response(
227-
error="API key not configured for this organization."
231+
error="OpenAI API key not configured for this organization."
228232
)
229233

230-
client = OpenAI(api_key=api_key)
234+
client = OpenAI(api_key=credentials["api_key"])
231235

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

0 commit comments

Comments
 (0)