forked from modelscope/agentscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
81 lines (67 loc) · 2.9 KB
/
tools.py
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
# -*- coding: utf-8 -*-
"""some utils of angent_builder example"""
import re
def load_txt(instruction_file: str) -> str:
"""
load .txt file
Arguments:
instruction_file: str type, which is the .txt file pth
Returns:
instruction: str type, which is the str in the instruction_file
"""
with open(instruction_file, "r", encoding="utf-8") as f:
instruction = f.read()
return instruction
# extract scenario和participants
def extract_scenario_and_participants(content: str) -> dict:
"""
extract the scenario and participants from agent builder's response
Arguments:
content: the agent builders response
Returns:
result: dict
Examples:
content: #scenario#: Astronomy club meeting
#participants#:
* Club Leader: Act as the club leader who is knowledgeable about\
astronomy and optics. You are leading a discussion about the \
capabilities of telescopes versus the human eye. Please provide \
accurate information and guide the discussion.
* Curious Member: Act as a curious club member who is interested \
in astronomy but may not know all the technical details. You are \
eager to learn and ask questions.
* Experienced Astronomer: Act as an experienced astronomer who has \
practical experience using telescopes for stargazing. You can \
provide real-world examples and insights into the topic.
Return:
{'Scenario': 'Astronomy club meeting',
'Participants':
{'Club_Leader': 'Act as the club leader who is knowledgeable \
about astronomy and optics. You are leading a discussion about the \
capabilities of telescopes versus the human eye. Please provide\
accurate information and guide the discussion.',
'Curious_Member': 'Act as a curious club member who is interested \
in astronomy but may not know all the technical details. You are \
eager to learn and ask questions.',
'Experienced_Astronomer': 'Act as an experienced astronomer who has\
practical experience using telescopes for stargazing. You can\
provide real-world examples and insights into the topic.'}}
"""
result = {}
# define regular expression
scenario_pattern = r"#scenario#:\s*(.*)"
participants_pattern = r"\*\s*([^:\n]+):\s*([^\n]+)"
# search and extract scenario
scenario_match = re.search(scenario_pattern, content)
if scenario_match:
result["Scenario"] = scenario_match.group(1).strip()
# search and extract participants
participants_matches = re.finditer(participants_pattern, content)
participants_dict = {}
for match in participants_matches:
participant_type, characteristic = match.groups()
participants_dict[
participant_type.strip().replace(" ", "_")
] = characteristic.strip()
result["Participants"] = participants_dict
return result