Skip to content

Commit 962e5b1

Browse files
committed
feat: test knowledge profile
1 parent 132edeb commit 962e5b1

File tree

4 files changed

+79
-25
lines changed

4 files changed

+79
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,5 @@ cython_debug/
199199
**./output
200200

201201
*.mp3
202-
*.pcm
202+
*.pcm
203+
.trae

veadk/knowledgebase/knowledgebase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,6 @@ async def generate_profiles(self, files: list[str], profile_path: str = ""):
303303
profile_names = [profile.name for profile in profiles]
304304

305305
write_string_to_file(
306-
profile_path + "/profile_list.json",
306+
profile_path + "/all_profile_list.json",
307307
json.dumps(profile_names, indent=4, ensure_ascii=False),
308308
)

veadk/tools/builtin_tools/load_kb_queries.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,19 @@ def load_kb_queries(profile_names: list[str], tool_context: ToolContext) -> list
5151

5252
index = tool_context._invocation_context.agent.knowledgebase.index
5353

54+
all_profile_names = []
55+
56+
with open(
57+
f"./profiles/knowledgebase/profiles_{index}/all_profile_list.json",
58+
"r",
59+
) as f:
60+
all_profile_names = json.load(f)
61+
5462
recommanded_queries = []
5563
for profile_name in profile_names:
64+
if profile_name not in all_profile_names:
65+
logger.info(f"{profile_name} Invalid")
66+
continue
5667
profile_path = Path(
5768
f"./profiles/knowledgebase/profiles_{index}/profile_{profile_name}.json"
5869
)

veadk/tools/builtin_tools/load_knowledgebase.py

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from veadk.knowledgebase.entry import KnowledgebaseEntry
2828
from veadk.tools.builtin_tools.load_kb_queries import load_profile
2929
from veadk.utils.logger import get_logger
30+
from veadk import Agent
3031

3132
logger = get_logger(__name__)
3233

@@ -47,6 +48,46 @@ def __init__(self, knowledgebase: KnowledgeBase):
4748
self.custom_metadata = {}
4849
self.custom_metadata["backend"] = knowledgebase.backend
4950

51+
# if self.knowledgebase.enable_profile:
52+
# from pathlib import Path
53+
#
54+
# profile_names = []
55+
# profile_descriptions = []
56+
#
57+
# with open(
58+
# f"./profiles/knowledgebase/profiles_{self.knowledgebase.index}/all_profile_list.json",
59+
# "r",
60+
# ) as f:
61+
# profile_names = json.load(f)
62+
#
63+
# for profile_name in profile_names:
64+
# profile_descriptions.append(
65+
#
66+
# load_profile(
67+
# Path(
68+
# f"./profiles/knowledgebase/profiles_{self.knowledgebase.index}/profile_{profile_name}.json"
69+
# ),
70+
# )["description"]
71+
# )
72+
#
73+
# profiles_text = "\n".join(
74+
# f"- profile_name: **{name}**\n profile_description: {profile_descriptions[idx]}"
75+
# for idx, name in enumerate(profile_names)
76+
# )
77+
#
78+
# self.select_agent = Agent(
79+
# name="DocRetrieveAgent",
80+
# description="A helpful assistant for retrieving documents relevant to user query.",
81+
# instruction=f"""
82+
# The knowledgebase is divided into the following profiles:
83+
#
84+
# {profiles_text}
85+
#
86+
# You should choose some profile_name which are most relevant to the user question.
87+
# """,
88+
# model_name="doubao-seed-1-6-251015",
89+
# )
90+
5091
@override
5192
def _get_declaration(self) -> types.FunctionDeclaration | None:
5293
return types.FunctionDeclaration(
@@ -65,10 +106,10 @@ def _get_declaration(self) -> types.FunctionDeclaration | None:
65106

66107
@override
67108
async def process_llm_request(
68-
self,
69-
*,
70-
tool_context: ToolContext,
71-
llm_request: LlmRequest,
109+
self,
110+
*,
111+
tool_context: ToolContext,
112+
llm_request: LlmRequest,
72113
) -> None:
73114
await super().process_llm_request(
74115
tool_context=tool_context, llm_request=llm_request
@@ -82,13 +123,14 @@ async def process_llm_request(
82123
profile_descriptions = []
83124

84125
with open(
85-
f"./profiles/knowledgebase/profiles_{index}/profile_list.json",
86-
"r",
126+
f"./profiles/knowledgebase/profiles_{index}/all_profile_list.json",
127+
"r",
87128
) as f:
88129
profile_names = json.load(f)
89130

90131
for profile_name in profile_names:
91132
profile_descriptions.append(
133+
92134
load_profile(
93135
Path(
94136
f"./profiles/knowledgebase/profiles_{index}/profile_{profile_name}.json"
@@ -97,30 +139,30 @@ async def process_llm_request(
97139
)
98140

99141
profiles_text = "\n".join(
100-
f"- profile_name: {name}\n profile_description: {profile_descriptions[idx]}"
142+
f"- profile_name: **{name}**\n profile_description: {profile_descriptions[idx]}"
101143
for idx, name in enumerate(profile_names)
102144
)
103145

104146
# Tell the model about the knowledgebase.
105147
llm_request.append_instructions(
106148
[
107149
f"""
108-
You have a knowledgebase (knowledegebase name is `{self.knowledgebase.name}`, knowledgebase description is `{self.knowledgebase.description}`). You can use it to answer questions. If any questions need
109-
you to look up the knowledgebase, you should call load_knowledgebase function with a query.
110-
"""
150+
You have a knowledgebase (knowledegebase name is `{self.knowledgebase.name}`, knowledgebase description is `{self.knowledgebase.description}`). You can use it to answer questions. If any questions need
151+
you to look up the knowledgebase, you should call load_knowledgebase function with a query.
152+
"""
111153
],
112154
)
113155

114156
if self.knowledgebase.enable_profile:
115157
llm_request.append_instructions(
116158
[
117159
f"""
118-
The knowledgebase is divided into the following profiles:
119-
120-
{profiles_text}
121-
122-
You should choose some profiles which are relevant to the user question. Before load the knowledgebase, you must call `load_kb_queries` to load the recommanded queries of the knowledgebase profiles. You should generate final knowledgebase queries based on the user question and recommanded queries.
123-
"""
160+
The knowledgebase is divided into the following profiles:
161+
162+
{profiles_text}
163+
164+
You should choose some profile_name which are most relevant to the user question. Before load the knowledgebase, you must call `load_kb_queries` to load the recommanded queries of the knowledgebase profiles. You should generate final knowledgebase queries based on the user question and recommanded queries.
165+
"""
124166
]
125167
)
126168

@@ -140,17 +182,17 @@ async def process_llm_request(
140182
llm_request.append_instructions(
141183
[
142184
f"""
143-
Please generate the knowledgebase queries based on the user profile (description) at the same time. For example, for a query `quick sort algorithm`, you should generate `quick sort algorithm for python` if the user is a python developer, or `quick sort algorithm friendly introduction` if the user is a beginner.
144-
145-
The user profile is :
146-
147-
{user_profile}
148-
"""
185+
Please generate the knowledgebase queries based on the user profile (description) at the same time. For example, for a query `quick sort algorithm`, you should generate `quick sort algorithm for python` if the user is a python developer, or `quick sort algorithm friendly introduction` if the user is a beginner.
186+
187+
The user profile is :
188+
189+
{user_profile}
190+
"""
149191
]
150192
)
151193

152194
async def load_knowledgebase(
153-
self, query: str, tool_context: ToolContext
195+
self, query: str, tool_context: ToolContext
154196
) -> LoadKnowledgebaseResponse:
155197
"""Loads the knowledgebase for the user.
156198

0 commit comments

Comments
 (0)