-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (50 loc) · 2.01 KB
/
Copy pathutils.py
File metadata and controls
64 lines (50 loc) · 2.01 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
import json
from openai.types.chat import (
ChatCompletionUserMessageParam,
ChatCompletionAssistantMessageParam,
ChatCompletionMessageParam,
ChatCompletionMessageToolCallParam,
ChatCompletionToolMessageParam,
ChatCompletionToolParam,
)
class DualPrinter:
def __init__(self, file_path="output.log"):
self.file = open(file_path, "a", encoding="utf-8")
def print(self, *args, **kwargs):
print(*args, **kwargs)
kwargs_no_flush = {k: v for k, v in kwargs.items() if k != "flush"}
print(*args, file=self.file, **kwargs_no_flush)
self.file.flush()
def close(self):
self.file.close()
def stringify_tool_call_results(tool_call_result: dict) -> str:
if 'content' not in tool_call_result:
return ""
try:
content_dict = json.loads(tool_call_result['content'])
except json.JSONDecodeError:
return tool_call_result['content']
concat = ''
for tool_name, result in content_dict.items():
if isinstance(result, list) and len(result) == 1:
result = result[0]
concat += f"""\n* `{tool_name}`:
```
{result}
```"""
return concat
def stringify_tool_call_requests(tool_call: ChatCompletionMessageToolCallParam):
return f"[Assistant requested to call tool {tool_call['function']['name']} with arguments {tool_call['function']['arguments']}]"
def format_assistant_responses(messages, last_user_messages_index=-1):
assistant_messages = messages[last_user_messages_index + 1:]
formatted = []
for msg in assistant_messages:
if msg['role'] == "tool":
formatted.append(stringify_tool_call_results(msg))
elif msg['role'] == "assistant":
if 'tool_calls' in msg and len(msg['tool_calls']) > 0:
for tool_call in msg['tool_calls']:
formatted.append(stringify_tool_call_requests(tool_call))
if 'content' in msg and len(msg['content']) > 0:
formatted.append(msg['content'])
return '\n'.join(formatted)