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 70e5829
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
43 changes: 17 additions & 26 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 @@ -18,28 +17,20 @@ def chat_completion(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.'

return chat
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')
return chat
except Exception as ex:
message = f'[CHAT_COMPLETION] Chat response failure. Message body: {body} Error: {ex}'
log.exception(message)
raise RuntimeError('Chat response failure') from ex
8 changes: 4 additions & 4 deletions tests/openai/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def test_client_exceptions(self, post_mock):
"""
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.')
with self.assertRaises(RuntimeError):
chat_completion(chat_prompt)

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.')
with self.assertRaises(RuntimeError):
chat_completion(chat_prompt)

0 comments on commit 70e5829

Please sign in to comment.