-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replaced openai client with chat completion api
- Loading branch information
1 parent
91491d7
commit f66b1d9
Showing
5 changed files
with
41 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
"""openai client""" | ||
"""CHAT_COMPLETION_API client""" | ||
import logging | ||
|
||
import openai | ||
import requests | ||
from requests.exceptions import ConnectTimeout | ||
|
||
from django.conf import settings | ||
|
||
openai.api_key = settings.OPENAI_API_KEY | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
def chat_completion(prompt): | ||
""" | ||
Use chatGPT https://api.openai.com/v1/chat/completions endpoint to generate a response. | ||
Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting. | ||
Arguments: | ||
prompt (str): chatGPT prompt | ||
""" | ||
response = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=[ | ||
{"role": "user", "content": 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) | ||
try: | ||
response = requests.post( | ||
completion_endpoint, | ||
headers=headers, | ||
data=prompt, | ||
timeout=(connect_timeout, read_timeout) | ||
) | ||
chat = response.json() | ||
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.' | ||
|
||
content = response['choices'][0]['message']['content'] | ||
return content | ||
return chat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters