shenchucheng commited on
Commit
b8b81b1
1 Parent(s): 1819680

format json stream log

Browse files
Files changed (1) hide show
  1. software_company.py +23 -0
software_company.py CHANGED
@@ -1,4 +1,5 @@
1
  import asyncio
 
2
  import json
3
  from pydantic import BaseModel, Field
4
  import datetime
@@ -28,6 +29,9 @@ from metagpt.utils.common import any_to_str, read_json_file, write_json_file
28
  from metagpt.utils.git_repository import GitRepository
29
 
30
 
 
 
 
31
  class RoleRun(Action):
32
  role: Role
33
 
@@ -131,10 +135,29 @@ class SoftwareCompany(Role):
131
  self.finish = True
132
  return Message(output.content, role=self.profile, cause_by=type(self._rc.todo))
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  output = await self.active_role._act()
135
  self.active_role._set_state(state=-1)
136
  self.active_role.publish_message(output)
137
 
 
 
 
138
  cause_by = output.cause_by
139
 
140
  if cause_by == any_to_str(WritePRD):
 
1
  import asyncio
2
+ from functools import partial
3
  import json
4
  from pydantic import BaseModel, Field
5
  import datetime
 
29
  from metagpt.utils.git_repository import GitRepository
30
 
31
 
32
+ _default_llm_stream_log = partial(print, end="")
33
+
34
+
35
  class RoleRun(Action):
36
  role: Role
37
 
 
135
  self.finish = True
136
  return Message(output.content, role=self.profile, cause_by=type(self._rc.todo))
137
 
138
+ default_log_stream = CONFIG.get("LLM_STREAM_LOG", _default_llm_stream_log)
139
+
140
+ start = False
141
+ insert_code = False
142
+
143
+ def log_stream(msg):
144
+ nonlocal start, insert_code
145
+ if not start:
146
+ if msg.startswith("["):
147
+ msg = "```json\n" + msg
148
+ insert_code = True
149
+ start = True
150
+ return default_log_stream(msg)
151
+
152
+ CONFIG.LLM_STREAM_LOG = log_stream
153
+
154
  output = await self.active_role._act()
155
  self.active_role._set_state(state=-1)
156
  self.active_role.publish_message(output)
157
 
158
+ if insert_code:
159
+ default_log_stream("```")
160
+
161
  cause_by = output.cause_by
162
 
163
  if cause_by == any_to_str(WritePRD):