jarvis-ai-assistant 0.1.125__py3-none-any.whl → 0.1.128__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of jarvis-ai-assistant might be problematic. Click here for more details.
- jarvis/__init__.py +1 -1
- jarvis/jarvis_agent/__init__.py +205 -187
- jarvis/jarvis_code_agent/code_agent.py +116 -109
- jarvis/jarvis_code_agent/patch.py +157 -138
- jarvis/jarvis_code_agent/shell_input_handler.py +22 -0
- jarvis/jarvis_codebase/main.py +314 -288
- jarvis/jarvis_dev/main.py +695 -716
- jarvis/jarvis_lsp/base.py +0 -12
- jarvis/jarvis_lsp/cpp.py +0 -9
- jarvis/jarvis_lsp/go.py +0 -9
- jarvis/jarvis_lsp/python.py +0 -28
- jarvis/jarvis_lsp/registry.py +0 -1
- jarvis/jarvis_lsp/rust.py +0 -9
- jarvis/jarvis_multi_agent/__init__.py +52 -52
- jarvis/jarvis_platform/base.py +6 -5
- jarvis/jarvis_platform_manager/main.py +1 -1
- jarvis/jarvis_rag/main.py +250 -186
- jarvis/jarvis_smart_shell/main.py +0 -1
- jarvis/jarvis_tools/ask_codebase.py +10 -9
- jarvis/jarvis_tools/ask_user.py +2 -2
- jarvis/jarvis_tools/base.py +4 -4
- jarvis/jarvis_tools/chdir.py +28 -28
- jarvis/jarvis_tools/code_review.py +44 -39
- jarvis/jarvis_tools/create_code_agent.py +4 -4
- jarvis/jarvis_tools/create_sub_agent.py +7 -7
- jarvis/jarvis_tools/execute_shell.py +53 -23
- jarvis/jarvis_tools/execute_shell_script.py +3 -3
- jarvis/jarvis_tools/file_operation.py +70 -41
- jarvis/jarvis_tools/git_commiter.py +61 -51
- jarvis/jarvis_tools/lsp_find_definition.py +7 -7
- jarvis/jarvis_tools/lsp_prepare_rename.py +7 -7
- jarvis/jarvis_tools/methodology.py +6 -6
- jarvis/jarvis_tools/rag.py +5 -5
- jarvis/jarvis_tools/read_webpage.py +52 -32
- jarvis/jarvis_tools/registry.py +167 -180
- jarvis/jarvis_tools/search_web.py +66 -41
- jarvis/jarvis_tools/select_code_files.py +3 -3
- jarvis/jarvis_tools/tool_generator.py +68 -55
- jarvis/jarvis_utils/methodology.py +77 -59
- jarvis/jarvis_utils/output.py +1 -0
- {jarvis_ai_assistant-0.1.125.dist-info → jarvis_ai_assistant-0.1.128.dist-info}/METADATA +31 -17
- jarvis_ai_assistant-0.1.128.dist-info/RECORD +74 -0
- {jarvis_ai_assistant-0.1.125.dist-info → jarvis_ai_assistant-0.1.128.dist-info}/WHEEL +1 -1
- jarvis/jarvis_tools/lsp_validate_edit.py +0 -141
- jarvis/jarvis_tools/read_code.py +0 -192
- jarvis_ai_assistant-0.1.125.dist-info/RECORD +0 -75
- {jarvis_ai_assistant-0.1.125.dist-info → jarvis_ai_assistant-0.1.128.dist-info}/LICENSE +0 -0
- {jarvis_ai_assistant-0.1.125.dist-info → jarvis_ai_assistant-0.1.128.dist-info}/entry_points.txt +0 -0
- {jarvis_ai_assistant-0.1.125.dist-info → jarvis_ai_assistant-0.1.128.dist-info}/top_level.txt +0 -0
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
from typing import Dict, Any, List
|
|
2
|
+
|
|
3
|
+
from regex import W
|
|
4
|
+
from yaspin import yaspin
|
|
2
5
|
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
3
6
|
from jarvis.jarvis_tools.read_webpage import WebpageTool
|
|
4
7
|
from playwright.sync_api import sync_playwright
|
|
@@ -12,52 +15,72 @@ def bing_search(query):
|
|
|
12
15
|
try:
|
|
13
16
|
with sync_playwright() as p:
|
|
14
17
|
# Set parameters when starting the browser
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
with yaspin(text="正在启动浏览器...", color="cyan") as spinner:
|
|
19
|
+
browser = p.chromium.launch(
|
|
20
|
+
headless=True, # Headless mode
|
|
21
|
+
args=['--disable-gpu', '--no-sandbox', '--disable-dev-shm-usage']
|
|
22
|
+
)
|
|
23
|
+
spinner.text = "浏览器启动完成"
|
|
24
|
+
spinner.ok("✅")
|
|
19
25
|
|
|
20
26
|
# Create a new page and set timeout
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
with yaspin(text="正在创建新页面...", color="cyan") as spinner:
|
|
28
|
+
page = browser.new_page(
|
|
29
|
+
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
|
|
30
|
+
viewport={'width': 1920, 'height': 1080}
|
|
31
|
+
)
|
|
32
|
+
spinner.text = "新页面创建完成"
|
|
33
|
+
spinner.ok("✅")
|
|
25
34
|
|
|
26
35
|
# Set page timeout
|
|
27
|
-
|
|
36
|
+
with yaspin(text="正在设置页面超时...", color="cyan") as spinner:
|
|
37
|
+
page.set_default_timeout(60000)
|
|
38
|
+
spinner.text = "页面超时设置完成"
|
|
39
|
+
spinner.ok("✅")
|
|
28
40
|
|
|
29
41
|
# Visit search page
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
with yaspin(text=f"正在搜索 {query}...", color="cyan") as spinner:
|
|
43
|
+
url = f"https://www.bing.com/search?q={quote(query)}&form=QBLH&sp=-1"
|
|
44
|
+
page.goto(url, wait_until="networkidle")
|
|
45
|
+
spinner.text = "搜索完成"
|
|
46
|
+
spinner.ok("✅")
|
|
32
47
|
|
|
33
48
|
# Wait for search results to load
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
49
|
+
with yaspin(text="正在等待搜索结果加载...", color="cyan") as spinner:
|
|
50
|
+
page.wait_for_selector("#b_results", state="visible", timeout=30000)
|
|
51
|
+
# Wait for a moment to ensure the results are fully loaded
|
|
52
|
+
page.wait_for_timeout(1000)
|
|
53
|
+
spinner.text = "搜索结果加载完成"
|
|
54
|
+
spinner.ok("✅")
|
|
38
55
|
|
|
39
56
|
# Extract search results
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
for (const el of elements) {
|
|
45
|
-
const titleEl = el.querySelector("h2");
|
|
46
|
-
const linkEl = titleEl ? titleEl.querySelector("a") : null;
|
|
47
|
-
const abstractEl = el.querySelector(".b_caption p");
|
|
57
|
+
with yaspin(text="正在提取搜索结果...", color="cyan") as spinner:
|
|
58
|
+
summaries = page.evaluate("""() => {
|
|
59
|
+
const results = [];
|
|
60
|
+
const elements = document.querySelectorAll("#b_results > .b_algo");
|
|
48
61
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
for (const el of elements) {
|
|
63
|
+
const titleEl = el.querySelector("h2");
|
|
64
|
+
const linkEl = titleEl ? titleEl.querySelector("a") : null;
|
|
65
|
+
const abstractEl = el.querySelector(".b_caption p");
|
|
66
|
+
|
|
67
|
+
if (linkEl) {
|
|
68
|
+
results.push({
|
|
69
|
+
title: titleEl.innerText.trim(),
|
|
70
|
+
href: linkEl.href,
|
|
71
|
+
abstract: abstractEl ? abstractEl.innerText.trim() : ""
|
|
72
|
+
});
|
|
73
|
+
}
|
|
55
74
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
75
|
+
return results;
|
|
76
|
+
}""")
|
|
77
|
+
spinner.text = "搜索结果提取完成"
|
|
78
|
+
spinner.ok("✅")
|
|
79
|
+
|
|
80
|
+
with yaspin(text="正在关闭浏览器...", color="cyan") as spinner:
|
|
81
|
+
browser.close()
|
|
82
|
+
spinner.text = "浏览器关闭完成"
|
|
83
|
+
spinner.ok("✅")
|
|
61
84
|
return summaries
|
|
62
85
|
|
|
63
86
|
except Exception as error:
|
|
@@ -66,21 +89,21 @@ def bing_search(query):
|
|
|
66
89
|
|
|
67
90
|
class SearchTool:
|
|
68
91
|
name = "search_web"
|
|
69
|
-
description = "
|
|
92
|
+
description = "使用Bing搜索引擎搜索信息,并根据问题提取关键信息"
|
|
70
93
|
parameters = {
|
|
71
94
|
"type": "object",
|
|
72
95
|
"properties": {
|
|
73
96
|
"query": {
|
|
74
97
|
"type": "string",
|
|
75
|
-
"description": "
|
|
98
|
+
"description": "搜索关键词"
|
|
76
99
|
},
|
|
77
100
|
"question": {
|
|
78
101
|
"type": "string",
|
|
79
|
-
"description": "
|
|
102
|
+
"description": "要回答的具体问题,用于从搜索结果中提取相关信息"
|
|
80
103
|
},
|
|
81
104
|
"max_results": {
|
|
82
105
|
"type": "integer",
|
|
83
|
-
"description": "
|
|
106
|
+
"description": "最大搜索结果数量",
|
|
84
107
|
"default": 3
|
|
85
108
|
}
|
|
86
109
|
},
|
|
@@ -235,8 +258,10 @@ Please synthesize a final answer that:
|
|
|
235
258
|
}
|
|
236
259
|
|
|
237
260
|
# Extract information
|
|
238
|
-
|
|
239
|
-
|
|
261
|
+
with yaspin(text="正在提取信息...", color="cyan") as spinner:
|
|
262
|
+
analysis = self._extract_info(contents, question)
|
|
263
|
+
spinner.text = "信息提取完成"
|
|
264
|
+
spinner.ok("✅")
|
|
240
265
|
|
|
241
266
|
return {
|
|
242
267
|
"success": True,
|
|
@@ -293,4 +318,4 @@ def main():
|
|
|
293
318
|
sys.exit(1)
|
|
294
319
|
|
|
295
320
|
if __name__ == "__main__":
|
|
296
|
-
main()
|
|
321
|
+
main()
|
|
@@ -6,7 +6,7 @@ from jarvis.jarvis_utils.output import OutputType, PrettyOutput
|
|
|
6
6
|
|
|
7
7
|
class CodeFileSelecterTool:
|
|
8
8
|
name = "select_code_files"
|
|
9
|
-
description = "
|
|
9
|
+
description = "通过交互式文件选择工具选择和修改代码文件"
|
|
10
10
|
parameters = {
|
|
11
11
|
"type": "object",
|
|
12
12
|
"properties": {
|
|
@@ -15,12 +15,12 @@ class CodeFileSelecterTool:
|
|
|
15
15
|
"items": {
|
|
16
16
|
"type": "string",
|
|
17
17
|
},
|
|
18
|
-
"description": "
|
|
18
|
+
"description": "初始相关的文件列表",
|
|
19
19
|
"default": []
|
|
20
20
|
},
|
|
21
21
|
"root_dir": {
|
|
22
22
|
"type": "string",
|
|
23
|
-
"description": "
|
|
23
|
+
"description": "代码库的根目录",
|
|
24
24
|
"default": "."
|
|
25
25
|
}
|
|
26
26
|
},
|
|
@@ -4,25 +4,27 @@ Tool Generator Tool - Automatically creates new tools using LLM
|
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
import re
|
|
6
6
|
from typing import Dict, Any
|
|
7
|
+
|
|
8
|
+
from yaspin import yaspin
|
|
7
9
|
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
8
10
|
|
|
9
11
|
class ToolGenerator:
|
|
10
12
|
name = "tool_generator"
|
|
11
|
-
description = "
|
|
13
|
+
description = "使用LLM自动生成与系统集成的新工具"
|
|
12
14
|
parameters = {
|
|
13
15
|
"type": "object",
|
|
14
16
|
"properties": {
|
|
15
17
|
"tool_name": {
|
|
16
18
|
"type": "string",
|
|
17
|
-
"description": "
|
|
19
|
+
"description": "新工具的名称"
|
|
18
20
|
},
|
|
19
21
|
"description": {
|
|
20
22
|
"type": "string",
|
|
21
|
-
"description": "
|
|
23
|
+
"description": "工具用途描述"
|
|
22
24
|
},
|
|
23
25
|
"input_spec": {
|
|
24
26
|
"type": "string",
|
|
25
|
-
"description": "
|
|
27
|
+
"description": "所需输入和功能的规范说明"
|
|
26
28
|
}
|
|
27
29
|
},
|
|
28
30
|
"required": ["tool_name", "description", "input_spec"]
|
|
@@ -39,33 +41,45 @@ class ToolGenerator:
|
|
|
39
41
|
input_spec = arguments["input_spec"]
|
|
40
42
|
|
|
41
43
|
# Generate tool implementation using LLM
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
with yaspin(text="正在生成工具...", color="cyan") as spinner:
|
|
45
|
+
prompt = self._create_prompt(tool_name, description, input_spec)
|
|
46
|
+
llm_response = model.chat_until_success(prompt)
|
|
47
|
+
spinner.text = "工具生成完成"
|
|
48
|
+
spinner.ok("✅")
|
|
44
49
|
|
|
45
50
|
# Extract implementation with more flexible parsing
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
with yaspin(text="正在提取工具实现...", color="cyan") as spinner:
|
|
52
|
+
implementation = self._extract_code(llm_response)
|
|
53
|
+
if not implementation:
|
|
54
|
+
return {
|
|
55
|
+
"success": False,
|
|
56
|
+
"stdout": "",
|
|
57
|
+
"stderr": "Could not extract valid Python code from LLM response"
|
|
58
|
+
}
|
|
59
|
+
spinner.text = "工具实现提取完成"
|
|
60
|
+
spinner.ok("✅")
|
|
53
61
|
|
|
54
62
|
# Validate return value format
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
with yaspin(text="正在验证工具返回值格式...", color="cyan") as spinner:
|
|
64
|
+
if not self._validate_return_value_format(implementation):
|
|
65
|
+
return {
|
|
66
|
+
"success": False,
|
|
67
|
+
"stdout": "",
|
|
68
|
+
"stderr": "Generated tool does not follow required return value format"
|
|
69
|
+
}
|
|
70
|
+
spinner.text = "工具返回值格式验证完成"
|
|
71
|
+
spinner.ok("✅")
|
|
61
72
|
|
|
62
73
|
# Save the new tool
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
with yaspin(text="正在保存工具...", color="cyan") as spinner:
|
|
75
|
+
tools_dir = Path.home() / ".jarvis" / "tools"
|
|
76
|
+
tools_dir.mkdir(parents=True, exist_ok=True)
|
|
77
|
+
tool_file = tools_dir / f"{tool_name}.py"
|
|
78
|
+
|
|
79
|
+
with open(tool_file, "w") as f:
|
|
80
|
+
f.write(implementation)
|
|
81
|
+
spinner.text = "工具保存完成"
|
|
82
|
+
spinner.ok("✅")
|
|
69
83
|
|
|
70
84
|
return {
|
|
71
85
|
"success": True,
|
|
@@ -81,7 +95,7 @@ class ToolGenerator:
|
|
|
81
95
|
}
|
|
82
96
|
|
|
83
97
|
def _create_prompt(self, tool_name: str, description: str, input_spec: str) -> str:
|
|
84
|
-
"""
|
|
98
|
+
"""创建用于工具生成的LLM提示"""
|
|
85
99
|
example_code = '''
|
|
86
100
|
<TOOL>
|
|
87
101
|
from typing import Dict, Any
|
|
@@ -89,24 +103,24 @@ from jarvis.utils import OutputType, PrettyOutput
|
|
|
89
103
|
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
90
104
|
|
|
91
105
|
class CustomTool:
|
|
92
|
-
name = "
|
|
93
|
-
description = "
|
|
94
|
-
parameters = { #
|
|
106
|
+
name = "工具名称" # 调用时使用的工具名称
|
|
107
|
+
description = "工具描述" # 工具用途
|
|
108
|
+
parameters = { # 参数JSON Schema
|
|
95
109
|
"type": "object",
|
|
96
110
|
"properties": {
|
|
97
111
|
"param1": {
|
|
98
112
|
"type": "string",
|
|
99
|
-
"description": "
|
|
113
|
+
"description": "参数描述"
|
|
100
114
|
}
|
|
101
115
|
},
|
|
102
116
|
"required": ["param1"]
|
|
103
117
|
}
|
|
104
118
|
|
|
105
119
|
def execute(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
|
106
|
-
"""
|
|
120
|
+
"""执行工具功能
|
|
107
121
|
|
|
108
122
|
Args:
|
|
109
|
-
args:
|
|
123
|
+
args: 传递给工具的参数
|
|
110
124
|
|
|
111
125
|
Returns:
|
|
112
126
|
{
|
|
@@ -116,12 +130,12 @@ class CustomTool:
|
|
|
116
130
|
}
|
|
117
131
|
"""
|
|
118
132
|
try:
|
|
119
|
-
#
|
|
120
|
-
#
|
|
133
|
+
# 在此实现工具逻辑
|
|
134
|
+
# 使用LLM
|
|
121
135
|
# model = PlatformRegistry.get_global_platform_registry().get_codegen_platform()
|
|
122
136
|
# result = model.chat_until_success(prompt)
|
|
123
137
|
|
|
124
|
-
result = "
|
|
138
|
+
result = "工具执行结果"
|
|
125
139
|
return {
|
|
126
140
|
"success": True,
|
|
127
141
|
"stdout": result,
|
|
@@ -136,29 +150,28 @@ class CustomTool:
|
|
|
136
150
|
</TOOL>
|
|
137
151
|
'''
|
|
138
152
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
-
|
|
146
|
-
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
-
|
|
151
|
-
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
10. Output the code in the following format:
|
|
153
|
+
return f'''创建一个与Jarvis系统集成的Python工具类。请遵循以下要求:
|
|
154
|
+
1. 类名: {tool_name.capitalize()}Tool
|
|
155
|
+
2. 描述: {description}
|
|
156
|
+
3. 输入规范: {input_spec}
|
|
157
|
+
4. 必须包含以下类属性:
|
|
158
|
+
- name: str (工具标识符)
|
|
159
|
+
- description: str (工具用途)
|
|
160
|
+
- parameters: dict (输入的JSON schema)
|
|
161
|
+
5. 必须实现 execute(self, args: Dict) -> Dict 方法
|
|
162
|
+
6. execute方法必须返回包含以下字段的字典:
|
|
163
|
+
- success: bool (指示操作是否成功)
|
|
164
|
+
- stdout: str (主要输出/结果)
|
|
165
|
+
- stderr: str (错误信息,如果有)
|
|
166
|
+
7. 必须优雅地处理错误
|
|
167
|
+
8. 仅返回Python实现代码
|
|
168
|
+
9. 代码应该是完整且可直接使用的
|
|
169
|
+
10. 按照以下格式输出代码:
|
|
157
170
|
<TOOL>
|
|
158
171
|
{example_code}
|
|
159
172
|
</TOOL>
|
|
160
173
|
|
|
161
|
-
|
|
174
|
+
示例:
|
|
162
175
|
{example_code}
|
|
163
176
|
'''
|
|
164
177
|
|
|
@@ -179,4 +192,4 @@ Example:
|
|
|
179
192
|
return False
|
|
180
193
|
|
|
181
194
|
# Check for required fields in return statement
|
|
182
|
-
return all(field in code for field in required_fields)
|
|
195
|
+
return all(field in code for field in required_fields)
|
|
@@ -42,18 +42,19 @@ def _create_methodology_embedding(embedding_model: Any, methodology_text: str) -
|
|
|
42
42
|
return np.zeros(1536, dtype=np.float32)
|
|
43
43
|
def make_methodology_prompt(data: Dict[str, str]) -> str:
|
|
44
44
|
"""
|
|
45
|
-
|
|
45
|
+
从方法论数据生成格式化提示
|
|
46
46
|
|
|
47
47
|
Args:
|
|
48
|
-
data:
|
|
48
|
+
data: 方法论数据字典
|
|
49
49
|
|
|
50
50
|
Returns:
|
|
51
|
-
str:
|
|
51
|
+
str: 格式化后的提示字符串
|
|
52
52
|
"""
|
|
53
|
-
ret = """
|
|
53
|
+
ret = """这是处理以往问题的标准方法论,如果当前任务类似,可以参考使用,如果不相关,请忽略:\n"""
|
|
54
54
|
for key, value in data.items():
|
|
55
|
-
ret += f"
|
|
55
|
+
ret += f"问题: {key}\n方法论: {value}\n"
|
|
56
56
|
return ret
|
|
57
|
+
|
|
57
58
|
def load_methodology(user_input: str) -> str:
|
|
58
59
|
"""
|
|
59
60
|
Load methodology and build vector index for similarity search.
|
|
@@ -64,65 +65,82 @@ def load_methodology(user_input: str) -> str:
|
|
|
64
65
|
Returns:
|
|
65
66
|
str: Relevant methodology prompt or empty string if no methodology found
|
|
66
67
|
"""
|
|
67
|
-
|
|
68
|
+
from yaspin import yaspin
|
|
68
69
|
user_jarvis_methodology = os.path.expanduser("~/.jarvis/methodology")
|
|
69
70
|
if not os.path.exists(user_jarvis_methodology):
|
|
70
71
|
return ""
|
|
71
72
|
|
|
72
73
|
try:
|
|
73
|
-
with
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
ids: List[int] = []
|
|
81
|
-
# Get embedding model
|
|
82
|
-
embedding_model = load_embedding_model()
|
|
74
|
+
with yaspin(text="加载方法论文件...", color="yellow") as spinner:
|
|
75
|
+
with open(user_jarvis_methodology, "r", encoding="utf-8") as f:
|
|
76
|
+
data = yaml.safe_load(f)
|
|
77
|
+
if dont_use_local_model():
|
|
78
|
+
spinner.text = "加载方法论文件完成"
|
|
79
|
+
spinner.ok("✅")
|
|
80
|
+
return make_methodology_prompt(data)
|
|
83
81
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
methodology_index.add_with_ids(vectors_array, np.array(ids)) # type: ignore
|
|
102
|
-
query_embedding = _create_methodology_embedding(embedding_model, user_input)
|
|
103
|
-
k = min(3, len(methodology_data))
|
|
104
|
-
PrettyOutput.print(f"检索方法论...", OutputType.INFO)
|
|
105
|
-
distances, indices = methodology_index.search(
|
|
106
|
-
query_embedding.reshape(1, -1), k
|
|
107
|
-
) # type: ignore
|
|
108
|
-
relevant_methodologies = {}
|
|
109
|
-
output_lines = []
|
|
110
|
-
for dist, idx in zip(distances[0], indices[0]):
|
|
111
|
-
if idx >= 0:
|
|
112
|
-
similarity = 1.0 / (1.0 + float(dist))
|
|
113
|
-
methodology = methodology_data[idx]
|
|
114
|
-
output_lines.append(
|
|
115
|
-
f"Methodology '{methodology['key']}' similarity: {similarity:.3f}"
|
|
116
|
-
)
|
|
117
|
-
if similarity >= 0.5:
|
|
118
|
-
relevant_methodologies[methodology["key"]] = methodology["value"]
|
|
82
|
+
with yaspin(text="初始化数据结构...", color="yellow") as spinner:
|
|
83
|
+
methodology_data: List[Dict[str, str]] = []
|
|
84
|
+
vectors: List[np.ndarray] = []
|
|
85
|
+
ids: List[int] = []
|
|
86
|
+
spinner.text = "初始化数据结构完成"
|
|
87
|
+
spinner.ok("✅")
|
|
88
|
+
|
|
89
|
+
with yaspin(text="加载嵌入模型...", color="yellow") as spinner:
|
|
90
|
+
embedding_model = load_embedding_model()
|
|
91
|
+
spinner.text = "加载嵌入模型完成"
|
|
92
|
+
spinner.ok("✅")
|
|
93
|
+
|
|
94
|
+
with yaspin(text="创建测试嵌入...", color="yellow") as spinner:
|
|
95
|
+
test_embedding = _create_methodology_embedding(embedding_model, "test")
|
|
96
|
+
embedding_dimension = len(test_embedding)
|
|
97
|
+
spinner.text = "创建测试嵌入完成"
|
|
98
|
+
spinner.ok("✅")
|
|
119
99
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
100
|
+
with yaspin(text="处理方法论数据...", color="yellow") as spinner:
|
|
101
|
+
for i, (key, value) in enumerate(data.items()):
|
|
102
|
+
methodology_text = f"{key}\n{value}"
|
|
103
|
+
embedding = _create_methodology_embedding(embedding_model, methodology_text)
|
|
104
|
+
vectors.append(embedding)
|
|
105
|
+
ids.append(i)
|
|
106
|
+
methodology_data.append({"key": key, "value": value})
|
|
107
|
+
spinner.text = "处理方法论数据完成"
|
|
108
|
+
spinner.ok("✅")
|
|
109
|
+
|
|
110
|
+
if vectors:
|
|
111
|
+
with yaspin(text="构建索引...", color="yellow") as spinner:
|
|
112
|
+
vectors_array = np.vstack(vectors)
|
|
113
|
+
hnsw_index = faiss.IndexHNSWFlat(embedding_dimension, 16)
|
|
114
|
+
hnsw_index.hnsw.efConstruction = 40
|
|
115
|
+
hnsw_index.hnsw.efSearch = 16
|
|
116
|
+
methodology_index = faiss.IndexIDMap(hnsw_index)
|
|
117
|
+
methodology_index.add_with_ids(vectors_array, np.array(ids)) # type: ignore
|
|
118
|
+
spinner.text = "构建索引完成"
|
|
119
|
+
spinner.ok("✅")
|
|
120
|
+
|
|
121
|
+
with yaspin(text="执行搜索...", color="yellow") as spinner:
|
|
122
|
+
query_embedding = _create_methodology_embedding(embedding_model, user_input)
|
|
123
|
+
k = min(3, len(methodology_data))
|
|
124
|
+
distances, indices = methodology_index.search(
|
|
125
|
+
query_embedding.reshape(1, -1), k
|
|
126
|
+
) # type: ignore
|
|
127
|
+
spinner.text = "执行搜索完成"
|
|
128
|
+
spinner.ok("✅")
|
|
129
|
+
|
|
130
|
+
with yaspin(text="处理搜索结果...", color="yellow") as spinner:
|
|
131
|
+
relevant_methodologies = {}
|
|
132
|
+
for dist, idx in zip(distances[0], indices[0]):
|
|
133
|
+
if idx >= 0:
|
|
134
|
+
similarity = 1.0 / (1.0 + float(dist))
|
|
135
|
+
methodology = methodology_data[idx]
|
|
136
|
+
if similarity >= 0.5:
|
|
137
|
+
relevant_methodologies[methodology["key"]] = methodology["value"]
|
|
138
|
+
spinner.text = "处理搜索结果完成"
|
|
139
|
+
spinner.ok("✅")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
if relevant_methodologies:
|
|
143
|
+
return make_methodology_prompt(relevant_methodologies)
|
|
144
|
+
return make_methodology_prompt(data)
|
|
126
145
|
except Exception as e:
|
|
127
|
-
|
|
128
|
-
return ""
|
|
146
|
+
return ""
|