mriusero commited on
Commit
154d8ef
·
1 Parent(s): 05dd2f4

fix: display thoughts

Browse files
chroma_db/chroma.sqlite3 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:2cbc86c73ecee8ebf7e3173e6edb5a1a3fcd523aa94120f236e878911f76bd83
3
  size 876544
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:eee67341491b5b24def5befaa502f35455427e03f804e29e6d85649a2bdec1c4
3
  size 876544
chroma_db/d365f4bc-8099-45f4-bdc0-9c299960820d/length.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:0f4b8e138dc6fb3cfe2b386d15b3975eaa2adb8ce9a47cfa75ae4786c5ffb624
3
  size 40000
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7e2dcff542de95352682dc186432e98f0188084896773f1973276b0577d5305
3
  size 40000
src/agent/stream.py CHANGED
@@ -15,16 +15,14 @@ with open("./prompt.md", encoding="utf-8") as f:
15
  SYSTEM_PROMPT = f.read()
16
 
17
  def extract_phases(text):
18
- """Découpe le contenu en THINK / ACT / OBSERVE / FINAL ANSWER"""
19
  phases = {'think': '', 'act': '', 'observe': '', 'final': ''}
20
  matches = list(re.finditer(r'(THINK:|ACT:|OBSERVE:|FINAL ANSWER:)', text))
21
-
22
  for i, match in enumerate(matches):
23
  phase = match.group(1).lower().replace(":", "").replace("final answer", "final")
24
  start = match.end()
25
  end = matches[i+1].start() if i + 1 < len(matches) else len(text)
26
  phases[phase] = text[start:end].strip()
27
-
28
  return phases
29
 
30
 
@@ -90,12 +88,26 @@ async def respond(message, history=None, state=None):
90
 
91
  phases = extract_phases(full)
92
  buffer = phases.get(current_phase, "")
 
93
  if current_phase == "think":
94
  history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Thinking...", "status": "pending", "id": state['cycle']})
95
- elif current_phase == "act":
96
- history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Acting...", "status": "pending", "id": state['cycle']+1, 'parent_id': state["cycle"]})
 
 
 
 
 
 
 
97
  elif current_phase == "observe":
98
- history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Observing...", "status": "pending", "id": state['cycle']+2, 'parent_id': state["cycle"]})
 
 
 
 
 
 
99
  yield history
100
 
101
  if current_phase == "final":
@@ -147,7 +159,13 @@ async def respond(message, history=None, state=None):
147
  last_tool_response = next((m for m in reversed(messages) if m["role"] == "tool"), None)
148
  if last_tool_response and last_tool_response.get("content"):
149
  buffer += "\n\n" + last_tool_response["content"]
150
- history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Acting...", "status": "pending", "id": state['cycle']+1, 'parent_id': state["cycle"]})
 
 
 
 
 
 
151
  yield history
152
 
153
  if not done:
@@ -161,12 +179,19 @@ async def respond(message, history=None, state=None):
161
  final_text = phases.get("final", "")
162
 
163
  if observe_text:
164
- history[-1] = ChatMessage(role="assistant", content=observe_text, metadata={"title": "Thoughts", "status": "done", "id": state['cycle']+2, 'parent_id': state["cycle"]})
165
  messages = [msg for msg in messages if not msg.get("prefix")]
166
  messages.append({
167
  "role": "assistant",
168
  "content": observe_text,
169
  })
 
 
 
 
 
 
 
 
170
  if final_text:
171
  history.append(ChatMessage(role="assistant", content=final_text))
172
 
 
15
  SYSTEM_PROMPT = f.read()
16
 
17
  def extract_phases(text):
18
+ """Split streaming in THINK / ACT / OBSERVE / FINAL ANSWER"""
19
  phases = {'think': '', 'act': '', 'observe': '', 'final': ''}
20
  matches = list(re.finditer(r'(THINK:|ACT:|OBSERVE:|FINAL ANSWER:)', text))
 
21
  for i, match in enumerate(matches):
22
  phase = match.group(1).lower().replace(":", "").replace("final answer", "final")
23
  start = match.end()
24
  end = matches[i+1].start() if i + 1 < len(matches) else len(text)
25
  phases[phase] = text[start:end].strip()
 
26
  return phases
27
 
28
 
 
88
 
89
  phases = extract_phases(full)
90
  buffer = phases.get(current_phase, "")
91
+
92
  if current_phase == "think":
93
  history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Thinking...", "status": "pending", "id": state['cycle']})
94
+
95
+ #elif current_phase == "act":
96
+ #parent_message = next((msg for msg in history if msg.metadata.get("id") == state['cycle']), None)
97
+ #if parent_message:
98
+ # parent_message.content += "\n\n" + buffer
99
+ # parent_message.metadata["title"] = "Acting..."
100
+ #else:
101
+ # history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Acting...", "status": "pending", "id": state['cycle']+1, 'parent_id': state["cycle"]})
102
+
103
  elif current_phase == "observe":
104
+ parent_message = next((msg for msg in history if msg.metadata.get("id") == state['cycle']), None)
105
+ if parent_message:
106
+ parent_message.content += "\n\n" + buffer
107
+ parent_message.metadata["title"] = "Acting..."
108
+ else:
109
+ history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Observing...", "status": "pending", "id": state['cycle']+2, 'parent_id': state["cycle"]})
110
+
111
  yield history
112
 
113
  if current_phase == "final":
 
159
  last_tool_response = next((m for m in reversed(messages) if m["role"] == "tool"), None)
160
  if last_tool_response and last_tool_response.get("content"):
161
  buffer += "\n\n" + last_tool_response["content"]
162
+
163
+ parent_message = next((msg for msg in history if msg.metadata.get("id") == state['cycle']), None)
164
+ if parent_message:
165
+ parent_message.content += "\n\n" + buffer
166
+ parent_message.metadata["title"] = "Acting..."
167
+ else:
168
+ history[-1] = ChatMessage(role="assistant", content=buffer, metadata={"title": "Acting...", "status": "pending", "id": state['cycle']+1, 'parent_id': state["cycle"]})
169
  yield history
170
 
171
  if not done:
 
179
  final_text = phases.get("final", "")
180
 
181
  if observe_text:
 
182
  messages = [msg for msg in messages if not msg.get("prefix")]
183
  messages.append({
184
  "role": "assistant",
185
  "content": observe_text,
186
  })
187
+ parent_message = next((msg for msg in history if msg.metadata.get("id") == state['cycle']), None)
188
+ if parent_message:
189
+ parent_message.content += "\n\n" + observe_text
190
+ parent_message.metadata["title"] = "Thoughts"
191
+ parent_message.metadata["status"] = "done"
192
+ else:
193
+ history[-1] = ChatMessage(role="assistant", content=observe_text, metadata={"title": "Thoughts", "status": "done", "id": state['cycle']+2, 'parent_id': state["cycle"]})
194
+
195
  if final_text:
196
  history.append(ChatMessage(role="assistant", content=final_text))
197