Skip to content

Commit

Permalink
refactor: replace openai usage with xpert api
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Jul 4, 2024
1 parent 7fe3b76 commit f039b99
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Unreleased
----------
* nothing unreleased

[4.21.0]
---------
* refactor: replace openai usage with xpert api

[4.20.14]
---------
* chore: Update requirements
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.20.14"
__version__ = "4.21.0"
31 changes: 0 additions & 31 deletions enterprise/api_client/open_ai.py

This file was deleted.

40 changes: 40 additions & 0 deletions enterprise/api_client/xpert_ai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Xpert client
"""

import json

import requests

from django.conf import settings

CONNECT_TIMOUET_SECONDS = 5
READ_TIMEOUT_SECONDS = 20


def chat_completion(prompt, role):
"""
Generate response using xpert api.
Arguments:
prompt (str): ChatGPT prompt
role (str): ChatGPT role to assume for the prompt.
Returns:
(str): Prompt response from OpenAI.
"""
headers = {
'Content-Type': 'application/json',
'x-api-key': settings.CHAT_COMPLETION_API_KEY
}

body = {'message_list': [{'role': role, 'content': prompt},]}

response = requests.post(
settings.CHAT_COMPLETION_API,
headers=headers,
data=json.dumps(body),
timeout=(CONNECT_TIMOUET_SECONDS, READ_TIMEOUT_SECONDS)
)

return response.json().get('content')
2 changes: 1 addition & 1 deletion enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
from enterprise.api_client.ecommerce import EcommerceApiClient
from enterprise.api_client.enterprise_catalog import EnterpriseCatalogApiClient
from enterprise.api_client.lms import EnrollmentApiClient, ThirdPartyAuthApiClient
from enterprise.api_client.open_ai import chat_completion
from enterprise.api_client.sso_orchestrator import EnterpriseSSOOrchestratorApiClient
from enterprise.api_client.xpert_ai import chat_completion
from enterprise.constants import (
ALL_ACCESS_CONTEXT,
AVAILABLE_LANGUAGES,
Expand Down
3 changes: 2 additions & 1 deletion enterprise/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ def root(*args):
# disable indexing on history_date. Otherwise it will add new alter migrations.
SIMPLE_HISTORY_DATE_INDEX = False

OPENAI_API_KEY = ''
CHAT_COMPLETION_API = 'https://example.com/chat/completion'
CHAT_COMPLETION_API_KEY = 'i am a key'

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.'
Expand Down
14 changes: 9 additions & 5 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7423,21 +7423,25 @@ def test_view_with_admin_user_tries(self):
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json() == {'detail': 'Missing: enterprise.can_access_admin_dashboard'}

@mock.patch('enterprise.models.chat_completion')
def test_view_with_admin_user(self, mock_chat_completion):
@mock.patch('enterprise.api_client.xpert_ai.requests.post')
def test_view_with_admin_user(self, mock_post):
"""
Verify that an enterprise admin user having `enterprise.can_access_admin_dashboard` role can access the view.
"""
mock_chat_completion.return_value = 'Test Response.'
xpert_response = 'Response from Xpert AI'
mock_response = mock.Mock()
mock_response.json.return_value = {'content': xpert_response}
mock_post.return_value = mock_response

self.set_jwt_cookie(ENTERPRISE_ADMIN_ROLE, self.enterprise_uuid)

self.client.login(username=self.user.username, password=TEST_PASSWORD)
EnterpriseCustomerFactory.create(uuid=self.enterprise_uuid)

response = self.client.post(self.url, data=self.payload, format='json')
assert response.status_code == status.HTTP_200_OK
assert 'learner_progress' in response.json()
assert 'learner_engagement' in response.json()
assert response.json()['learner_progress'] == xpert_response
assert response.json()['learner_engagement'] == xpert_response

@mock.patch('enterprise.models.chat_completion')
def test_404_if_enterprise_customer_does_not_exist(self, mock_chat_completion):
Expand Down

0 comments on commit f039b99

Please sign in to comment.