From 908a184c5b5ea246963d6040d0f8ec6baa243205 Mon Sep 17 00:00:00 2001 From: omar-sarfraz Date: Thu, 6 Mar 2025 15:34:22 +0500 Subject: [PATCH] chore: migrate analytics_summary API to Xpert v2 --- CHANGELOG.rst | 4 ++++ enterprise/__init__.py | 2 +- enterprise/api/v1/views/analytics_summary.py | 6 +++--- enterprise/api_client/xpert_ai.py | 9 ++++++--- enterprise/settings/test.py | 5 +++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3c889eebd2..70b8e92fe1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,10 @@ Unreleased ---------- * nothing unreleased +[5.8.3] +-------- +* chore: migrate Xpert API to v2 for analytics_summary endpoint. + [5.8.2] -------- * feat: add index to `user_fk` in `EnterpriseCustomerUser` model. Part 2 of adding auth_user as a foreign key. diff --git a/enterprise/__init__.py b/enterprise/__init__.py index fae5fb85b3..e2ddf673c8 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,4 +2,4 @@ Your project description goes here. """ -__version__ = "5.8.2" +__version__ = "5.8.3" diff --git a/enterprise/api/v1/views/analytics_summary.py b/enterprise/api/v1/views/analytics_summary.py index c63f6d2c87..0665dea5e2 100644 --- a/enterprise/api/v1/views/analytics_summary.py +++ b/enterprise/api/v1/views/analytics_summary.py @@ -20,7 +20,7 @@ class AnalyticsSummaryView(generics.GenericAPIView): """ - API to generate a signed token for an enterprise admin to use Plotly analytics. + API to retrieve analytics summary using Xpert AI """ authentication_classes = [JwtAuthentication, SessionAuthentication] permission_classes = (IsAuthenticated,) @@ -33,9 +33,9 @@ class AnalyticsSummaryView(generics.GenericAPIView): ) def post(self, request, enterprise_uuid): """ - Generate auth token for plotly. + Perform analysis on given data and return result from Xpert AI. """ - role = 'system' + role = 'user' enterprise_customer = get_object_or_404(EnterpriseCustomer, uuid=enterprise_uuid) # Validate payload data diff --git a/enterprise/api_client/xpert_ai.py b/enterprise/api_client/xpert_ai.py index 9686e004d1..e97fdc38a8 100644 --- a/enterprise/api_client/xpert_ai.py +++ b/enterprise/api_client/xpert_ai.py @@ -25,13 +25,16 @@ def chat_completion(prompt, role): """ headers = { 'Content-Type': 'application/json', - 'x-api-key': settings.CHAT_COMPLETION_API_KEY } - body = {'message_list': [{'role': role, 'content': prompt},]} + body = { + 'messages': [{'role': role, 'content': prompt},], + 'client_id': settings.ENTERPRISE_ANALYSIS_CLIENT_ID, + 'system_message': settings.ENTERPRISE_ANALYSIS_SYSTEM_PROMPT + } response = requests.post( - settings.CHAT_COMPLETION_API, + settings.CHAT_COMPLETION_API_V2, headers=headers, data=json.dumps(body), timeout=(CONNECT_TIMOUET_SECONDS, READ_TIMEOUT_SECONDS) diff --git a/enterprise/settings/test.py b/enterprise/settings/test.py index 30087edd13..9f8f401e73 100644 --- a/enterprise/settings/test.py +++ b/enterprise/settings/test.py @@ -366,8 +366,9 @@ def root(*args): # disable indexing on history_date. Otherwise it will add new alter migrations. SIMPLE_HISTORY_DATE_INDEX = False -CHAT_COMPLETION_API = 'https://example.com/chat/completion' -CHAT_COMPLETION_API_KEY = 'i am a key' +CHAT_COMPLETION_API_V2 = 'https://example.com/chat/completion' +ENTERPRISE_ANALYSIS_CLIENT_ID = 'test_client_id' +ENTERPRISE_ANALYSIS_SYSTEM_PROMPT = 'This is a test prompt' LEARNER_ENGAGEMENT_PROMPT_FOR_ACTIVE_CONTRACT = 'Create learner engagement report for active contracts.' LEARNER_ENGAGEMENT_PROMPT_FOR_NON_ACTIVE_CONTRACT = 'Create learner engagement report for non active contracts.'