lvwerra HF staff commited on
Commit
100570e
·
verified ·
1 Parent(s): 1b870a9

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +51 -12
utils.py CHANGED
@@ -71,9 +71,17 @@ def parse_exec_result_nb(execution):
71
  return outputs
72
 
73
 
74
- system_template = """<div class="alert alert-block alert-info">
75
- <b>System:</b> {}
76
- </div>
 
 
 
 
 
 
 
 
77
  """
78
 
79
  user_template = """<div class="alert alert-block alert-success">
@@ -115,19 +123,50 @@ def create_base_notebook(messages):
115
  "source": "",
116
  "outputs": []
117
  })
118
-
 
119
 
120
  for message in messages:
121
  if message["role"] == "system":
122
  text = system_template.format(message["content"].replace('\n', '<br>'))
 
 
 
 
 
123
  elif message["role"] == "user":
124
- text = user_template.format(message["content"])
125
- base_notebook["cells"].append({
126
- "cell_type": "markdown",
127
- "metadata": {},
128
- "source": text
 
 
 
 
 
 
 
 
 
129
  })
130
- return base_notebook
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  def execute_code(sbx, code):
133
  execution = sbx.run_code(code, on_stdout=lambda data: print('stdout:', data))
@@ -158,9 +197,9 @@ def update_notebook_display(notebook_data):
158
  return notebook_body
159
 
160
  def run_interactive_notebook(client, model, messages, sbx, max_new_tokens=512):
161
- notebook_data = create_base_notebook(messages)
162
  try:
163
- code_cell_counter = 0
164
  while True:
165
  response_stream = client.chat.completions.create(
166
  model=model,
 
71
  return outputs
72
 
73
 
74
+ system_template = """\
75
+ <details>
76
+ <summary>
77
+ <div class="alert alert-block alert-info">
78
+ <b>System:</b>
79
+ </div>
80
+ </summary>
81
+ <div class="alert alert-block alert-info">
82
+ {}
83
+ </div>
84
+ </details>
85
  """
86
 
87
  user_template = """<div class="alert alert-block alert-success">
 
123
  "source": "",
124
  "outputs": []
125
  })
126
+
127
+ code_cell_counter = 0
128
 
129
  for message in messages:
130
  if message["role"] == "system":
131
  text = system_template.format(message["content"].replace('\n', '<br>'))
132
+ base_notebook["cells"].append({
133
+ "cell_type": "markdown",
134
+ "metadata": {},
135
+ "source": text
136
+ })
137
  elif message["role"] == "user":
138
+ text = user_template.format(message["content"].replace('\n', '<br>'))
139
+ base_notebook["cells"].append({
140
+ "cell_type": "markdown",
141
+ "metadata": {},
142
+ "source": text
143
+ })
144
+
145
+ elif message["role"] == "assistant" and "tool_call" in message:
146
+ base_notebook["cells"].append({
147
+ "cell_type": "code",
148
+ "execution_count": None,
149
+ "metadata": {},
150
+ "source": message["content"],
151
+ "outputs": []
152
  })
153
+
154
+ elif message["role"] == "ipython":
155
+ code_cell_counter +=1
156
+ base_notebook["cells"][-1]["outputs"].append(message["content"])
157
+ base_notebook["cells"][-1]["execution_count"] = code_cell_counter
158
+
159
+ elif message["role"] == "assistant" and "tool_call" not in message:
160
+ base_notebook["cells"].append({
161
+ "cell_type": "markdown",
162
+ "metadata": {},
163
+ "source": message["content"]
164
+ })
165
+
166
+ else:
167
+ raise ValueError(message)
168
+
169
+ return base_notebook, code_cell_counter
170
 
171
  def execute_code(sbx, code):
172
  execution = sbx.run_code(code, on_stdout=lambda data: print('stdout:', data))
 
197
  return notebook_body
198
 
199
  def run_interactive_notebook(client, model, messages, sbx, max_new_tokens=512):
200
+ notebook_data, code_cell_counter = create_base_notebook(messages)
201
  try:
202
+ #code_cell_counter = 0
203
  while True:
204
  response_stream = client.chat.completions.create(
205
  model=model,