Skip to content

Commit

Permalink
fix: Added review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanuddinahmad committed Jul 18, 2024
1 parent 4e25d31 commit eb1b67f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 47 deletions.
40 changes: 13 additions & 27 deletions taxonomy/openai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging

import requests
from requests.exceptions import ConnectTimeout

from django.conf import settings

Expand All @@ -16,30 +15,17 @@ def chat_completion(prompt):
Arguments:
prompt (str): chatGPT prompt
"""
completion_endpoint = getattr(settings, 'CHAT_COMPLETION_API', None)
completion_endpoint_key = getattr(settings, 'CHAT_COMPLETION_API_KEY', None)
if completion_endpoint and completion_endpoint_key:
headers = {'Content-Type': 'application/json', 'x-api-key': completion_endpoint_key}
connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1)
read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15)
body = {'message_list': [{'role': 'assistant', 'content': prompt},]}
try:
response = requests.post(
completion_endpoint,
headers=headers,
data=json.dumps(body),
timeout=(connect_timeout, read_timeout)
)
chat = response.json().get('content')
except (ConnectTimeout, ConnectionError) as e:
error_message = str(e)
connection_message = 'Failed to connect to chat completion API.'
log.error(
'%(connection_message)s %(error)s',
{'connection_message': connection_message, 'error': error_message}
)
chat = connection_message
else:
chat = 'Completion endpoint is not defined.'

completion_endpoint = settings.CHAT_COMPLETION_API
completion_endpoint_key = settings.CHAT_COMPLETION_API_KEY
headers = {'Content-Type': 'application/json', 'x-api-key': completion_endpoint_key}
connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1)
read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15)
body = {'message_list': [{'role': 'assistant', 'content': prompt},]}
response = requests.post(
completion_endpoint,
headers=headers,
data=json.dumps(body),
timeout=(connect_timeout, read_timeout)
)
chat = response.json().get('content')
return chat
20 changes: 0 additions & 20 deletions tests/openai/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Tests for chat completion client.
"""
import responses
from mock import patch

from django.conf import settings

Expand Down Expand Up @@ -31,22 +30,3 @@ def test_client(self):
)
chat_response = chat_completion(chat_prompt)
self.assertEqual(chat_response, expected_chat_response['content'])

@patch('taxonomy.openai.client.requests.post')
def test_client_exceptions(self, post_mock):
"""
Test that the chat completion client handles exceptions as expected.
"""
chat_prompt = 'how many courses are offered by edx in the data science area'
post_mock.side_effect = ConnectionError()
chat_response = chat_completion(chat_prompt)
self.assertEqual(chat_response, 'Failed to connect to chat completion API.')

def test_client_missing_settings(self):
"""
Test that the chat completion client handles missing settings as expected.
"""
chat_prompt = 'how many courses are offered by edx in the data science area'
settings.CHAT_COMPLETION_API_KEY = None
chat_response = chat_completion(chat_prompt)
self.assertEqual(chat_response, 'Completion endpoint is not defined.')

0 comments on commit eb1b67f

Please sign in to comment.