Skip to content

Commit 4f0ab13

Browse files
author
Aman Rusia
committed
Removed is waiting for user input model
1 parent dea4515 commit 4f0ab13

File tree

2 files changed

+104
-111
lines changed

2 files changed

+104
-111
lines changed

src/wcgw/basic.py

+46-50
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ParsedChatCompletionMessage,
1717
)
1818
import rich
19-
import petname
19+
import petname # type: ignore[import-untyped]
2020
from typer import Typer
2121
import uuid
2222

@@ -30,7 +30,6 @@
3030
Confirmation,
3131
DoneFlag,
3232
Writefile,
33-
get_is_waiting_user_input,
3433
get_tool_output,
3534
SHELL,
3635
start_shell,
@@ -92,29 +91,22 @@ def parse_user_message_special(msg: str) -> ChatCompletionUserMessageParam:
9291
if line.startswith("%"):
9392
args = line[1:].strip().split(" ")
9493
command = args[0]
95-
assert command == 'image'
94+
assert command == "image"
9695
image_path = args[1]
97-
with open(image_path, 'rb') as f:
96+
with open(image_path, "rb") as f:
9897
image_bytes = f.read()
9998
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
10099
image_type = mimetypes.guess_type(image_path)[0]
101-
dataurl=f'data:{image_type};base64,{image_b64}'
102-
parts.append({
103-
'type': 'image_url',
104-
'image_url': {
105-
'url': dataurl,
106-
'detail': 'auto'
107-
}
108-
})
100+
dataurl = f"data:{image_type};base64,{image_b64}"
101+
parts.append(
102+
{"type": "image_url", "image_url": {"url": dataurl, "detail": "auto"}}
103+
)
109104
else:
110-
if len(parts) > 0 and parts[-1]['type'] == 'text':
111-
parts[-1]['text'] += '\n' + line
105+
if len(parts) > 0 and parts[-1]["type"] == "text":
106+
parts[-1]["text"] += "\n" + line
112107
else:
113-
parts.append({'type': 'text', 'text': line})
114-
return {
115-
'role': 'user',
116-
'content': parts
117-
}
108+
parts.append({"type": "text", "text": line})
109+
return {"role": "user", "content": parts}
118110

119111

120112
app = Typer(pretty_exceptions_show_locals=False)
@@ -146,7 +138,7 @@ def loop(
146138
if history[1]["role"] != "user":
147139
raise ValueError("Invalid history file, second message should be user")
148140
first_message = ""
149-
waiting_for_assistant = history[-1]['role'] != 'assistant'
141+
waiting_for_assistant = history[-1]["role"] != "assistant"
150142

151143
my_dir = os.path.dirname(__file__)
152144
config_file = os.path.join(my_dir, "..", "..", "config.toml")
@@ -161,9 +153,6 @@ def loop(
161153
enc = tiktoken.encoding_for_model(
162154
config.model if not config.model.startswith("o1") else "gpt-4o"
163155
)
164-
is_waiting_user_input = get_is_waiting_user_input(
165-
config.model, config.cost_file[config.model]
166-
)
167156

168157
tools = [
169158
openai.pydantic_function_tool(
@@ -290,7 +279,7 @@ def loop(
290279
)
291280
system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
292281
output_toks += output_toks_
293-
282+
294283
_histories.append(item)
295284
for tool_call_id, toolcallargs in tool_call_args_by_id.items():
296285
for toolindex, tool_args in toolcallargs.items():
@@ -300,7 +289,7 @@ def loop(
300289
enc,
301290
limit - cost,
302291
loop,
303-
is_waiting_user_input,
292+
max_tokens=2048,
304293
)
305294
except Exception as e:
306295
output_or_done = (
@@ -322,42 +311,49 @@ def loop(
322311
f"\nTotal cost: {config.cost_unit}{cost:.3f}"
323312
)
324313
return output_or_done.task_output, cost
325-
314+
326315
output = output_or_done
327316

328317
if isinstance(output, ImageData):
329318
randomId = petname.Generate(2, "-")
330319
if not image_histories:
331-
image_histories.extend([
332-
{
333-
'role': 'assistant',
334-
'content': f'Share images with ids: {randomId}'
335-
336-
},
337-
{
338-
'role': 'user',
339-
'content': [{
340-
'type': 'image_url',
341-
'image_url': {
342-
'url': output.dataurl,
343-
'detail': 'auto'
344-
}
345-
}]
346-
}]
320+
image_histories.extend(
321+
[
322+
{
323+
"role": "assistant",
324+
"content": f"Share images with ids: {randomId}",
325+
},
326+
{
327+
"role": "user",
328+
"content": [
329+
{
330+
"type": "image_url",
331+
"image_url": {
332+
"url": output.dataurl,
333+
"detail": "auto",
334+
},
335+
}
336+
],
337+
},
338+
]
347339
)
348340
else:
349-
image_histories[0]['content'] += ', ' + randomId
350-
image_histories[1]["content"].append({ # type: ignore
351-
'type': 'image_url',
352-
'image_url': {
353-
'url': output.dataurl,
354-
'detail': 'auto'
341+
image_histories[0]["content"] += ", " + randomId
342+
second_content = image_histories[1]["content"]
343+
assert isinstance(second_content, list)
344+
second_content.append(
345+
{
346+
"type": "image_url",
347+
"image_url": {
348+
"url": output.dataurl,
349+
"detail": "auto",
350+
},
355351
}
356-
})
352+
)
357353

358354
item = {
359355
"role": "tool",
360-
"content": f'Ask user for image id: {randomId}',
356+
"content": f"Ask user for image id: {randomId}",
361357
"tool_call_id": tool_call_id + str(toolindex),
362358
}
363359
else:

0 commit comments

Comments
 (0)