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 eb86b7f
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions taxonomy/openai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@ 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.'
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 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

return chat

0 comments on commit eb86b7f

Please sign in to comment.