antimoda1 commited on
Commit
6ec252a
·
1 Parent(s): a30cc96

change agent

Browse files
Files changed (2) hide show
  1. agent_runner.py +50 -7
  2. history_agent.py +2 -3
agent_runner.py CHANGED
@@ -1,11 +1,58 @@
1
- from smolagents import CodeAgent
 
 
2
  from smolagents.memory import ActionStep, FinalAnswerStep, PlanningStep
3
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  class StreamingAgentRunner:
6
- def __init__(self, agent: CodeAgent):
7
  self.agent = agent
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def run(self, query: str):
10
  """Генератор прогресса агента и финального ответа."""
11
  parts = ["Агент думает...\n"]
@@ -16,11 +63,7 @@ class StreamingAgentRunner:
16
  if isinstance(step, PlanningStep):
17
  parts.append(f"**План:** {step.plan.strip()}")
18
  elif isinstance(step, ActionStep):
19
- if step.tool_calls:
20
- names = ", ".join(tc.name for tc in step.tool_calls)
21
- parts.append(f"**Шаг {step.step_number}:** вызов `{names}`")
22
- if step.error:
23
- parts.append(f"**Ошибка:** {step.error}")
24
  elif isinstance(step, FinalAnswerStep):
25
  parts.append(f"## Ответ\n\n{step.output}")
26
 
 
1
+ import json
2
+
3
+ from smolagents import MultiStepAgent
4
  from smolagents.memory import ActionStep, FinalAnswerStep, PlanningStep
5
 
6
 
7
+ def _format_arguments(arguments) -> str:
8
+ if arguments is None:
9
+ return "{}"
10
+ if isinstance(arguments, str):
11
+ return arguments
12
+ try:
13
+ return json.dumps(arguments, ensure_ascii=False, indent=2)
14
+ except TypeError:
15
+ return str(arguments)
16
+
17
+
18
+ def _truncate(text: str, limit: int = 600) -> str:
19
+ if not text:
20
+ return "(пусто)"
21
+ text = str(text).strip()
22
+ if len(text) <= limit:
23
+ return text
24
+ return text[:limit] + f"\n… (ещё {len(text) - limit} символов)"
25
+
26
+
27
  class StreamingAgentRunner:
28
+ def __init__(self, agent: MultiStepAgent):
29
  self.agent = agent
30
 
31
+ def _format_action_step(self, step: ActionStep) -> list[str]:
32
+ lines = [f"### Шаг {step.step_number}"]
33
+
34
+ if step.model_output:
35
+ output = step.model_output
36
+ if isinstance(output, list):
37
+ output = json.dumps(output, ensure_ascii=False)
38
+ lines.append("**Модель:**")
39
+ lines.append(f"```\n{_truncate(output, 400)}\n```")
40
+
41
+ if step.tool_calls:
42
+ for i, tc in enumerate(step.tool_calls, start=1):
43
+ prefix = f"[{step.step_number}.{i}]" if len(step.tool_calls) > 1 else f"[{step.step_number}]"
44
+ lines.append(f"**{prefix} → `{tc.name}`**")
45
+ lines.append(f"```json\n{_format_arguments(tc.arguments)}\n```")
46
+
47
+ if step.observations:
48
+ lines.append("**← результат:**")
49
+ lines.append(f"```\n{_truncate(step.observations)}\n```")
50
+
51
+ if step.error:
52
+ lines.append(f"**⚠ ошибка:** {step.error}")
53
+
54
+ return lines
55
+
56
  def run(self, query: str):
57
  """Генератор прогресса агента и финального ответа."""
58
  parts = ["Агент думает...\n"]
 
63
  if isinstance(step, PlanningStep):
64
  parts.append(f"**План:** {step.plan.strip()}")
65
  elif isinstance(step, ActionStep):
66
+ parts.extend(self._format_action_step(step))
 
 
 
 
67
  elif isinstance(step, FinalAnswerStep):
68
  parts.append(f"## Ответ\n\n{step.output}")
69
 
history_agent.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent, tool
2
  from retrieval_impl import RETRIEVAL
3
 
4
 
@@ -91,10 +91,9 @@ search_corpus(query, year_from, year_to, top_k)
91
  - Каждый факт сопровождай названием раздела из поля «Название:».
92
  """
93
 
94
- agent = CodeAgent(
95
  tools=[search_corpus],
96
  model=model,
97
- additional_authorized_imports=[],
98
  instructions=instructions,
99
  max_steps=4,
100
  )
 
1
+ from smolagents import ToolCallingAgent, tool
2
  from retrieval_impl import RETRIEVAL
3
 
4
 
 
91
  - Каждый факт сопровождай названием раздела из поля «Название:».
92
  """
93
 
94
+ agent = ToolCallingAgent(
95
  tools=[search_corpus],
96
  model=model,
 
97
  instructions=instructions,
98
  max_steps=4,
99
  )