|
9 | 9 | log = logging.getLogger(__name__)
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def chat_completion(prompt): |
| 12 | +def chat_completion(prompt, system_message=settings.XPERT_DEFAULT_SYSTEM_MESSAGE): |
13 | 13 | """
|
14 |
| - Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting. |
| 14 | + Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API_V2 setting. |
15 | 15 | Arguments:
|
16 | 16 | prompt (str): chatGPT prompt
|
| 17 | + system_message (str): system message to be used in the chat |
17 | 18 | """
|
18 |
| - completion_endpoint = settings.CHAT_COMPLETION_API |
19 |
| - completion_endpoint_key = settings.CHAT_COMPLETION_API_KEY |
20 |
| - headers = {'Content-Type': 'application/json', 'x-api-key': completion_endpoint_key} |
| 19 | + completion_endpoint = settings.CHAT_COMPLETION_API_V2 |
| 20 | + headers = {'Content-Type': 'application/json'} |
21 | 21 | connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1)
|
22 | 22 | read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15)
|
23 |
| - body = {'message_list': [{'role': 'assistant', 'content': prompt},]} |
| 23 | + body = { |
| 24 | + 'messages': [{'role': 'assistant', 'content': prompt},], |
| 25 | + 'client_id': settings.XPERT_CLIENT_ID, |
| 26 | + 'system_message': system_message, |
| 27 | + } |
24 | 28 | response = requests.post(
|
25 | 29 | completion_endpoint,
|
26 | 30 | headers=headers,
|
27 | 31 | data=json.dumps(body),
|
28 | 32 | timeout=(connect_timeout, read_timeout)
|
29 | 33 | )
|
30 |
| - chat = response.json().get('content') |
| 34 | + if isinstance(response.json(), dict): |
| 35 | + chat = response.json().get('content') |
| 36 | + else: |
| 37 | + chat = response.json()[0].get('content') |
31 | 38 | return chat
|
0 commit comments