jarvis-ai-assistant 0.1.58__py3-none-any.whl → 0.1.74__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 CHANGED
@@ -1,3 +1,3 @@
1
1
  """Jarvis AI Assistant"""
2
2
 
3
- __version__ = "0.1.58"
3
+ __version__ = "0.1.74"
jarvis/agent.py CHANGED
@@ -25,6 +25,7 @@ class Agent:
25
25
  self.name = name
26
26
  self.is_sub_agent = is_sub_agent
27
27
  self.prompt = ""
28
+ self.conversation_turns = 0
28
29
 
29
30
 
30
31
  @staticmethod
@@ -87,17 +88,112 @@ class Agent:
87
88
  continue
88
89
 
89
90
 
90
- def _load_methodology(self) -> str:
91
- """加载经验总结"""
91
+ def _load_methodology(self) -> Dict[str, str]:
92
+ """加载方法论"""
92
93
  user_jarvis_methodology = os.path.expanduser("~/.jarvis_methodology")
93
- ret = ""
94
94
  if os.path.exists(user_jarvis_methodology):
95
95
  with open(user_jarvis_methodology, "r", encoding="utf-8") as f:
96
96
  data = yaml.safe_load(f)
97
- for k, v in data.items():
98
- ret += f"问题类型: \n{k}\n经验总结: \n{v}\n\n"
99
- PrettyOutput.print(f"从 {user_jarvis_methodology} 加载经验总结: {', '.join(data.keys())}", OutputType.INFO)
100
- return ret
97
+ PrettyOutput.print(f"从 {user_jarvis_methodology} 加载方法论: {', '.join(data.keys())}", OutputType.INFO)
98
+ return data
99
+ return {}
100
+
101
+ def _summarize_and_clear_history(self) -> None:
102
+ """总结当前对话历史并清空历史记录,只保留系统消息和总结
103
+
104
+ 这个方法会:
105
+ 1. 请求模型总结当前对话的关键信息
106
+ 2. 清空对话历史
107
+ 3. 保留系统消息
108
+ 4. 添加总结作为新的上下文
109
+ 5. 重置对话轮数
110
+ """
111
+ # 创建一个新的模型实例来做总结,避免影响主对话
112
+
113
+ PrettyOutput.print("总结对话历史,准备生成总结,开始新的对话...", OutputType.PLANNING)
114
+
115
+ prompt = """请总结之前对话中的关键信息,包括:
116
+ 1. 当前任务目标
117
+ 2. 已经确认的关键信息
118
+ 3. 已经尝试过的方案
119
+ 4. 当前进展
120
+ 5. 待解决的问题
121
+
122
+ 请用简洁的要点形式描述,突出重要信息。不要包含对话细节。
123
+ """
124
+
125
+ try:
126
+ summary = self.model.chat(prompt)
127
+
128
+ # 清空当前对话历史,但保留系统消息
129
+ self.model.delete_chat()
130
+
131
+ # 添加总结作为新的上下文
132
+ self.prompt = f"""以下是之前对话的关键信息总结:
133
+
134
+ {summary}
135
+
136
+ 请基于以上信息继续完成任务。
137
+ """
138
+
139
+ # 重置对话轮数
140
+ self.conversation_turns = 0
141
+
142
+ except Exception as e:
143
+ PrettyOutput.print(f"总结对话历史失败: {str(e)}", OutputType.ERROR)
144
+
145
+ def _complete_task(self) -> str:
146
+ """完成任务并生成总结
147
+
148
+ Returns:
149
+ str: 任务总结或完成状态
150
+ """
151
+ PrettyOutput.section("任务完成", OutputType.SUCCESS)
152
+
153
+ # 询问是否生成方法论,带输入验证
154
+ while True:
155
+ user_input = input("是否要为此任务生成方法论?(y/n): ").strip().lower()
156
+ if user_input in ['y', 'n', '']:
157
+ break
158
+ PrettyOutput.print("无效输入,请输入 y 或 n", OutputType.WARNING)
159
+
160
+ if user_input == 'y':
161
+ try:
162
+ # 让模型判断是否需要生成方法论
163
+ analysis_prompt = """本次任务已结束,请分析是否需要生成方法论。
164
+ 如果认为需要生成方法论,请先判断是创建新的方法论还是更新已有方法论。如果是更新已有方法论,使用update,否则使用add。
165
+ 如果认为不需要生成方法论,请说明原因。
166
+ 仅输出方法论工具的调用指令,或者是不需要生成方法论的说明,除此之外不要输出任何内容。
167
+ """
168
+ self.prompt = analysis_prompt
169
+ response = self._call_model(self.prompt)
170
+
171
+ # 检查是否包含工具调用
172
+ try:
173
+ result = Agent.extract_tool_calls(response)
174
+ PrettyOutput.print(result, OutputType.RESULT)
175
+ except Exception as e:
176
+ PrettyOutput.print(f"处理方法论生成失败: {str(e)}", OutputType.ERROR)
177
+
178
+ except Exception as e:
179
+ PrettyOutput.print(f"生成方法论时发生错误: {str(e)}", OutputType.ERROR)
180
+
181
+ if not self.is_sub_agent:
182
+ return "Task completed"
183
+
184
+ # 生成任务总结
185
+ summary_prompt = f"""请对以上任务执行情况生成一个简洁的总结报告,包括:
186
+
187
+ 1. 任务目标: 任务重述
188
+ 2. 执行结果: 成功/失败
189
+ 3. 关键信息: 提取执行过程中的重要信息
190
+ 4. 重要发现: 任何值得注意的发现
191
+ 5. 后续建议: 如果有的话
192
+
193
+ 请用简洁的要点形式描述,突出重要信息。
194
+ """
195
+ self.prompt = summary_prompt
196
+ return self._call_model(self.prompt)
101
197
 
102
198
  def run(self, user_input: str, file_list: Optional[List[str]] = None, keep_history: bool = False) -> str:
103
199
  """处理用户输入并返回响应,返回任务总结报告
@@ -111,20 +207,20 @@ class Agent:
111
207
  str: 任务总结报告
112
208
  """
113
209
  try:
114
- self.clear_history()
115
-
116
210
  if file_list:
117
211
  self.model.upload_files(file_list)
118
212
 
119
- # 加载经验总结
213
+ # 加载方法论
120
214
  methodology = self._load_methodology()
121
-
122
215
  methodology_prompt = ""
123
216
  if methodology:
124
- methodology_prompt = f"""这是以往处理问题的标准经验总结,如果当前任务与此类似,可参考:
217
+ methodology_prompt = f"""这是以往处理问题的标准方法论,如果当前任务与此类似,可参考:
125
218
  {methodology}
126
219
 
127
220
  """
221
+
222
+ self.clear_history()
223
+ self.conversation_turns = 0
128
224
 
129
225
  # 显示任务开始
130
226
  PrettyOutput.section(f"开始新任务: {self.name}", OutputType.PLANNING)
@@ -146,11 +242,11 @@ class Agent:
146
242
  6. 制定行动计划:根据目前可以使用的工具制定行动计划
147
243
  7. 执行行动计划:每步执行一个步骤,最多使用一个工具(工具执行完成后,等待工具结果再执行下一步)
148
244
  8. 监控与调整:如果执行结果与预期不符,则反思并调整行动计划,迭代之前的步骤
149
- 9. 经验总结:如果当前任务具有通用性且执行过程中遇到了值得记录的经验,使用经验总结工具记录经验总结
150
-
245
+ 9. 方法论:如果当前任务具有通用性且执行过程中遇到了值得记录的经验,使用方法论工具记录方法论,以提升后期处理类似问题的能力
246
+ 10. 任务结束:如果任务已经完成,使用任务结束指令结束任务
151
247
  -------------------------------------------------------------
152
248
 
153
- 经验总结模板:
249
+ 方法论模板:
154
250
  1. 问题重述
155
251
  2. 最优解决方案
156
252
  3. 最优方案执行步骤(失败的行动不需要体现)
@@ -181,6 +277,12 @@ arguments:
181
277
  6. 处理问题的每个步骤不是必须有的,可按情况省略
182
278
  7. 在执行一些可能对系统或者用户代码库造成破坏的工具时,请先询问用户
183
279
  8. 在多次迭代却没有任何进展时,可请求用户指导
280
+ 9. 如果返回的yaml字符串中包含冒号,请将整个字符串用引号包裹,避免yaml解析错误
281
+
282
+ -------------------------------------------------------------
283
+
284
+ 特殊指令:
285
+ 1. !<<SUMMARIZE>>! - 当你发现对话历史过长可能导致token超限时,可以使用此指令总结当前对话要点并清空历史。使用方法:直接回复"!<<SUMMARIZE>>!"即可。
184
286
 
185
287
  -------------------------------------------------------------
186
288
 
@@ -196,8 +298,20 @@ arguments:
196
298
  # 显示思考状态
197
299
  PrettyOutput.print("分析任务...", OutputType.PROGRESS)
198
300
 
301
+ # 增加对话轮次
302
+ self.conversation_turns += 1
303
+
304
+ # 如果对话超过10轮,在提示中添加提醒
305
+ if self.conversation_turns > 10:
306
+ self.prompt = f"{self.prompt}\n(提示:当前对话已超过10轮,建议使用 !<<SUMMARIZE>>! 指令总结对话历史,避免token超限)"
307
+
199
308
  current_response = self._call_model(self.prompt)
200
309
 
310
+ # 检查是否需要总结对话历史
311
+ if "!<<SUMMARIZE>>!" in current_response:
312
+ self._summarize_and_clear_history()
313
+ continue
314
+
201
315
  try:
202
316
  result = Agent.extract_tool_calls(current_response)
203
317
  except Exception as e:
@@ -206,7 +320,6 @@ arguments:
206
320
  continue
207
321
 
208
322
  if len(result) > 0:
209
-
210
323
  PrettyOutput.print("执行工具调用...", OutputType.PROGRESS)
211
324
  tool_result = self.tool_registry.handle_tool_calls(result)
212
325
  PrettyOutput.print(tool_result, OutputType.RESULT)
@@ -225,34 +338,7 @@ arguments:
225
338
  continue
226
339
 
227
340
  if not user_input:
228
- while True:
229
- choice = prompt("是否需要手动为此任务生成经验总结以提升Jarvis对类似任务的处理能力?(y/n), 回车跳过: ")
230
- if choice == "y":
231
- self._make_methodology()
232
- break
233
- elif choice == "n" or choice == "":
234
- break
235
- else:
236
- PrettyOutput.print("请输入y或n", OutputType.ERROR)
237
- continue
238
- PrettyOutput.section("任务完成", OutputType.SUCCESS)
239
- if self.is_sub_agent:
240
- # 生成任务总结
241
- summary_prompt = f"""请对以上任务执行情况生成一个简洁的总结报告,包括:
242
-
243
- 1. 任务目标: 任务重述
244
- 2. 执行结果: 成功/失败
245
- 3. 关键信息: 提取执行过程中的重要信息
246
- 4. 重要发现: 任何值得注意的发现
247
- 5. 后续建议: 如果有的话
248
-
249
- 请用简洁的要点形式描述,突出重要信息。
250
- """
251
- self.prompt = summary_prompt
252
- summary = self._call_model(self.prompt)
253
- return summary
254
- else:
255
- return "Task completed"
341
+ return self._complete_task()
256
342
 
257
343
  except Exception as e:
258
344
  PrettyOutput.print(str(e), OutputType.ERROR)
@@ -274,23 +360,6 @@ arguments:
274
360
  """清除对话历史,只保留系统提示"""
275
361
  self.prompt = ""
276
362
  self.model.reset()
363
+ self.conversation_turns = 0 # 重置对话轮次
364
+
277
365
 
278
- def _make_methodology(self):
279
- """生成经验总结"""
280
- current_response = self._call_model("""请根据之前的对话内容,判断是否有必要更新、添加、删除现有经验总结,如果有,使用methodology工具进行管理。
281
- 经验总结模板:
282
- 1. 问题重述
283
- 2. 最优解决方案
284
- 3. 最优方案执行步骤(失败的行动不需要体现)
285
- """)
286
-
287
- try:
288
- result = Agent.extract_tool_calls(current_response)
289
- except Exception as e:
290
- PrettyOutput.print(f"工具调用错误: {str(e)}", OutputType.ERROR)
291
- return
292
- if len(result) > 0:
293
- PrettyOutput.print("执行工具调用...", OutputType.PROGRESS)
294
- tool_result = self.tool_registry.handle_tool_calls(result)
295
- PrettyOutput.print(tool_result, OutputType.RESULT)
296
-
File without changes