-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompletion.py
More file actions
34 lines (26 loc) · 1.25 KB
/
completion.py
File metadata and controls
34 lines (26 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import Dict
class ResponseFormatException(Exception):
"""Error in the API response format"""
pass
def process_completion(completion: str) -> Dict[str, str]:
""" Process the completion obtained from ChatGPT's API response. Args:
* `completion`: Completion received from OpenAI API.
Returns a dictionary with key parts extracted from the completion.
"""
sample_dict = {}
try:
# Obtain the content of the message (call and summary)
content = completion.choices[0].message['content']
# Obtain the Conversation field of the response
conversation = content.split('[Call]')[1].split('[Summary]')[0].strip()
conversation = conversation[1:] if conversation[0] == ':' else conversation
conversation = conversation[1:] if conversation[0] == ' ' else conversation
sample_dict["Call"] = conversation
# Obtain the Summary field of the response
summary = content.split("[Summary]")[1].split("}")[0].strip()
summary = summary[1:] if summary[0] == ':' else summary
summary = summary[1:] if summary[0] == ' ' else summary
sample_dict["Summary"] = summary
except IndexError:
raise ResponseFormatException()
return sample_dict