-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
193 lines (155 loc) · 7.69 KB
/
utils.py
File metadata and controls
193 lines (155 loc) · 7.69 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import json
import os
from termcolor import colored
from datetime import datetime, timedelta
from openai.types.chat import ChatCompletionMessage
def open_file(file_path):
"""
Opens a file and returns its content as a string.
Args:
file_path (str): The path to the file to be opened.
Returns:
str: The content of the file.
Raises:
FileNotFoundError: If the file at the specified path does not exist.
IOError: If an error occurs while opening or reading the file.
"""
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def save_file(file_path, content):
"""
Saves the provided content to a file at the specified file path.
If the directory does not exist, it is created.
Args:
file_path (str): The path where the file will be saved.
content (str): The content to write into the file.
Raises:
IOError: If an error occurs during the file writing process.
"""
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
def get_scheduled_date(date_str):
date_format = "%Y-%m-%dT%H:%M:%SZ"
date_object = datetime.strptime(date_str, date_format)
new_date = date_object + timedelta(days=7)
return new_date.strftime(date_format)
def schedule_follow_up(interviewer, candidate, interview_date, sentiment=None):
"""
Schedules a follow-up call by calculating a new date 7 days after the given interview date and returning the details
in JSON format. Optionally includes the sentiment of the interview.
Args:
interviewer (str): The name of the interviewer.
candidate (str): The name of the candidate.
interview_date (str): The date and time of the interview in ISO 8601 format ("YYYY-MM-DDTHH:MM:SSZ").
sentiment (str, optional): The sentiment of the interview. Can be one of "positive", "negative", or "neutral".
Defaults to None.
Returns:
str: A JSON string containing the details of the scheduled follow-up, including:
- interviewer (str): The name of the interviewer.
- candidate (str): The name of the candidate.
- schedule_date (str): The new follow-up date, 7 days after the interview, in ISO 8601 format.
- sentiment (str, optional): The sentiment of the interview, if provided.
Raises:
ValueError: If the interview_date is not in the correct ISO 8601 format.
"""
new_date_str = get_scheduled_date(interview_date)
response = {
"interviewer": interviewer,
"candidate": candidate,
"schedule_date": new_date_str,
}
if sentiment is not None:
response["sentiment"] = sentiment
return json.dumps(response)
def get_follow_up_function_desc():
"""
Returns a description of the follow-up function, including the parameters needed for scheduling a follow-up call.
The function description is structured as a list containing a dictionary that defines the function type, name, description,
and parameters. The parameters describe details about the follow-up call, such as the interviewer's name, candidate's name,
interview date, and sentiment of the interview.
Returns:
list: A list containing a dictionary that describes the follow-up function, including:
- name (str): The name of the function ("schedule_follow_up").
- description (str): A brief description of the function's purpose.
- parameters (dict): An object containing the required and optional parameters for the follow-up function:
- interviewer (str): The name of the interviewer.
- candidate (str): The name of the candidate.
- interview_date (str): The date and time of the interview in ISO 8601 format.
- sentiment (str): The sentiment of the interview (can be "positive", "negative", or "neutral").
- required (list): The parameters that must be provided ("interviewer", "candidate", "interview_date").
"""
return [
{
"type": "function",
"function": {
"name": "schedule_follow_up",
"description": "Get the details of the scheduled follow-up call",
"parameters": {
"type": "object",
"properties": {
"interviewer": {
"type": "string",
"description": "The name of the interviewer",
},
"candidate": {
"type": "string",
"description": "The name of the candidate",
},
"interview_date": {
"type": "string",
"format": "date-time",
"description": "The date and time of the interview in ISO 8601 format",
},
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"],
"description": "The sentiment of the interview",
}
},
"required": ["interviewer", "candidate", "interview_date"]
}
}
}
]
def pretty_print_conversation(messages):
"""
Prints a formatted conversation with colored roles for better readability.
This function takes a list of message objects, processes them, and prints each message
in a formatted and colored output depending on the role of the sender. Supported roles
include 'system', 'user', 'assistant', and 'tool'. The colors are:
- 'system' messages are printed in magenta.
- 'user' messages are printed in green.
- 'assistant' messages are printed in yellow. If the message includes a tool call, the
tool call content is printed instead of the message content.
- 'tool' messages are printed in blue, showing the tool's name and content.
Args:
messages (list): A list of message objects, where each message is either a dictionary
or a `ChatCompletionMessage` object. Each message must contain at least
a 'role' and 'content', with optional keys such as 'tool_calls' for
assistant messages and 'name' for tool messages.
Raises:
AttributeError: If a message is not in the expected format or missing required fields.
"""
role_to_color = {
"system": "magenta",
"user": "green",
"assistant": "yellow",
"tool": "blue"
}
for raw_message in messages:
message = raw_message
if isinstance(raw_message, ChatCompletionMessage):
message = raw_message.dict()
if message["role"] == "system":
print(colored(f"system: {message['content']}\n", role_to_color[message["role"]]))
elif message["role"] == "user":
print(colored(f"user: {message['content']}\n", role_to_color[message["role"]]))
elif message["role"] == "assistant" and message.get("tool_calls"):
print(colored(f"assistant: {message['tool_calls']}\n", role_to_color[message["role"]]))
elif message["role"] == "assistant" and not message.get("tool_calls"):
print(colored(f"assistant: {message['content']}\n", role_to_color[message["role"]]))
elif message["role"] == "tool":
print(colored(f"function: {message['name']}: {message['content']}\n", role_to_color[message["role"]]))