wcgw 2.8.4__py3-none-any.whl → 2.8.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of wcgw might be problematic. Click here for more details.

@@ -1,462 +0,0 @@
1
- import base64
2
- import json
3
- import mimetypes
4
- import os
5
- import subprocess
6
- import tempfile
7
- import traceback
8
- import uuid
9
- from pathlib import Path
10
- from typing import DefaultDict, Optional, cast
11
-
12
- import openai
13
- import petname # type: ignore[import-untyped]
14
- import rich
15
- import tokenizers # type: ignore[import-untyped]
16
- from dotenv import load_dotenv
17
- from openai import OpenAI
18
- from openai.types.chat import (
19
- ChatCompletionContentPartParam,
20
- ChatCompletionMessageParam,
21
- ChatCompletionUserMessageParam,
22
- )
23
- from pydantic import BaseModel
24
- from typer import Typer
25
-
26
- from ..types_ import (
27
- BashCommand,
28
- BashInteraction,
29
- ContextSave,
30
- FileEdit,
31
- ReadFiles,
32
- ReadImage,
33
- ResetShell,
34
- WriteIfEmpty,
35
- )
36
- from .common import CostData, History, Models, discard_input
37
- from .memory import load_memory
38
- from .openai_utils import get_input_cost, get_output_cost
39
- from .tools import (
40
- DoneFlag,
41
- ImageData,
42
- default_enc,
43
- get_tool_output,
44
- initialize,
45
- which_tool,
46
- )
47
-
48
-
49
- class Config(BaseModel):
50
- model: Models
51
- cost_limit: float
52
- cost_file: dict[Models, CostData]
53
- cost_unit: str = "$"
54
-
55
-
56
- def text_from_editor(console: rich.console.Console) -> str:
57
- # First consume all the input till now
58
- discard_input()
59
- console.print("\n---------------------------------------\n# User message")
60
- data = input()
61
- if data:
62
- return data
63
- editor = os.environ.get("EDITOR", "vim")
64
- with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
65
- subprocess.run([editor, tf.name], check=True)
66
- with open(tf.name, "r") as f:
67
- data = f.read()
68
- console.print(data)
69
- return data
70
-
71
-
72
- def save_history(history: History, session_id: str) -> None:
73
- myid = str(history[1]["content"]).replace("/", "_").replace(" ", "_").lower()[:60]
74
- myid += "_" + session_id
75
- myid = myid + ".json"
76
-
77
- mypath = Path(".wcgw") / myid
78
- mypath.parent.mkdir(parents=True, exist_ok=True)
79
- with open(mypath, "w") as f:
80
- json.dump(history, f, indent=3)
81
-
82
-
83
- def parse_user_message_special(msg: str) -> ChatCompletionUserMessageParam:
84
- # Search for lines starting with `%` and treat them as special commands
85
- parts: list[ChatCompletionContentPartParam] = []
86
- for line in msg.split("\n"):
87
- if line.startswith("%"):
88
- args = line[1:].strip().split(" ")
89
- command = args[0]
90
- assert command == "image"
91
- image_path = " ".join(args[1:])
92
- with open(image_path, "rb") as f:
93
- image_bytes = f.read()
94
- image_b64 = base64.b64encode(image_bytes).decode("utf-8")
95
- image_type = mimetypes.guess_type(image_path)[0]
96
- dataurl = f"data:{image_type};base64,{image_b64}"
97
- parts.append(
98
- {"type": "image_url", "image_url": {"url": dataurl, "detail": "auto"}}
99
- )
100
- else:
101
- if len(parts) > 0 and parts[-1]["type"] == "text":
102
- parts[-1]["text"] += "\n" + line
103
- else:
104
- parts.append({"type": "text", "text": line})
105
- return {"role": "user", "content": parts}
106
-
107
-
108
- app = Typer(pretty_exceptions_show_locals=False)
109
-
110
-
111
- @app.command()
112
- def loop(
113
- first_message: Optional[str] = None,
114
- limit: Optional[float] = None,
115
- resume: Optional[str] = None,
116
- computer_use: bool = False,
117
- ) -> tuple[str, float]:
118
- load_dotenv()
119
-
120
- session_id = str(uuid.uuid4())[:6]
121
-
122
- history: History = []
123
- waiting_for_assistant = False
124
-
125
- memory = None
126
- if resume:
127
- try:
128
- _, memory, _ = load_memory(
129
- resume,
130
- 8000,
131
- lambda x: default_enc.encode(x).ids,
132
- lambda x: default_enc.decode(x),
133
- )
134
- except OSError:
135
- if resume == "latest":
136
- resume_path = sorted(Path(".wcgw").iterdir(), key=os.path.getmtime)[-1]
137
- else:
138
- resume_path = Path(resume)
139
- if not resume_path.exists():
140
- raise FileNotFoundError(f"File {resume} not found")
141
- with resume_path.open() as f:
142
- history = json.load(f)
143
- if len(history) <= 2:
144
- raise ValueError("Invalid history file")
145
- first_message = ""
146
- waiting_for_assistant = history[-1]["role"] != "assistant"
147
-
148
- my_dir = os.path.dirname(__file__)
149
-
150
- config = Config(
151
- model=cast(Models, os.getenv("OPENAI_MODEL", "gpt-4o-2024-08-06").lower()),
152
- cost_limit=0.1,
153
- cost_unit="$",
154
- cost_file={
155
- "gpt-4o-2024-08-06": CostData(
156
- cost_per_1m_input_tokens=5, cost_per_1m_output_tokens=15
157
- ),
158
- },
159
- )
160
-
161
- if limit is not None:
162
- config.cost_limit = limit
163
- limit = config.cost_limit
164
-
165
- enc = tokenizers.Tokenizer.from_pretrained("Xenova/gpt-4o")
166
-
167
- tools = [
168
- openai.pydantic_function_tool(
169
- BashCommand,
170
- description="""
171
- - Execute a bash command. This is stateful (beware with subsequent calls).
172
- - Do not use interactive commands like nano. Prefer writing simpler commands.
173
- - Status of the command and the current working directory will always be returned at the end.
174
- - Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands.
175
- - The first or the last line might be `(...truncated)` if the output is too long.
176
- - Always run `pwd` if you get any file or directory not found error to make sure you're not lost.
177
- - The control will return to you in 5 seconds regardless of the status. For heavy commands, keep checking status using BashInteraction till they are finished.
178
- - Run long running commands in background using screen instead of "&".
179
- - Do not use 'cat' to read files, use ReadFiles tool instead.
180
- """,
181
- ),
182
- openai.pydantic_function_tool(
183
- BashInteraction,
184
- description="""
185
- - Interact with running program using this tool
186
- - Special keys like arrows, interrupts, enter, etc.
187
- - Send text input to the running program.
188
- - Send send_specials=["Enter"] to recheck status of a running program.
189
- - Only one of send_text, send_specials, send_ascii should be provided.""",
190
- ),
191
- openai.pydantic_function_tool(
192
- ReadFiles,
193
- description="""
194
- - Read full file content of one or more files.
195
- - Provide absolute file paths only
196
- """,
197
- ),
198
- openai.pydantic_function_tool(
199
- WriteIfEmpty,
200
- description="""
201
- - Write content to an empty or non-existent file. Provide file path and content. Use this instead of BashCommand for writing new files.
202
- - Provide absolute file path only.
203
- - For editing existing files, use FileEdit instead of this tool.""",
204
- ),
205
- openai.pydantic_function_tool(
206
- FileEdit,
207
- description="""
208
- - Use absolute file path only.
209
- - Use ONLY SEARCH/REPLACE blocks to edit the file.
210
- - file_edit_using_search_replace_blocks should start with <<<<<<< SEARCH
211
- """,
212
- ),
213
- openai.pydantic_function_tool(
214
- ReadImage, description="Read an image from the shell."
215
- ),
216
- openai.pydantic_function_tool(
217
- ResetShell,
218
- description="Resets the shell. Use only if all interrupts and prompt reset attempts have failed repeatedly.",
219
- ),
220
- openai.pydantic_function_tool(
221
- ContextSave,
222
- description="""
223
-
224
- Saves provided description and file contents of all the relevant file paths or globs in a single text file.
225
- - Provide random unqiue id or whatever user provided.
226
- - Leave project path as empty string if no project path""",
227
- ),
228
- ]
229
-
230
- system = initialize(
231
- os.getcwd(),
232
- [],
233
- resume if (memory and resume) else "",
234
- max_tokens=8000,
235
- mode="wcgw",
236
- )
237
-
238
- with open(os.path.join(os.path.dirname(__file__), "diff-instructions.txt")) as f:
239
- system += f.read()
240
-
241
- if not history:
242
- history = [{"role": "system", "content": system}]
243
- else:
244
- if history[-1]["role"] == "tool":
245
- waiting_for_assistant = True
246
-
247
- client = OpenAI()
248
-
249
- cost: float = 0
250
- input_toks = 0
251
- output_toks = 0
252
- system_console = rich.console.Console(style="blue", highlight=False, markup=False)
253
- error_console = rich.console.Console(style="red", highlight=False, markup=False)
254
- user_console = rich.console.Console(
255
- style="bright_black", highlight=False, markup=False
256
- )
257
- assistant_console = rich.console.Console(
258
- style="white bold", highlight=False, markup=False
259
- )
260
- while True:
261
- if cost > limit:
262
- system_console.print(
263
- f"\nCost limit exceeded. Current cost: {cost}, input tokens: {input_toks}, output tokens: {output_toks}"
264
- )
265
- break
266
-
267
- if not waiting_for_assistant:
268
- if first_message:
269
- msg = first_message
270
- first_message = ""
271
- else:
272
- msg = text_from_editor(user_console)
273
-
274
- history.append(parse_user_message_special(msg))
275
- else:
276
- waiting_for_assistant = False
277
-
278
- cost_, input_toks_ = get_input_cost(
279
- config.cost_file[config.model], enc, history
280
- )
281
- cost += cost_
282
- input_toks += input_toks_
283
-
284
- stream = client.chat.completions.create(
285
- messages=history,
286
- model=config.model,
287
- stream=True,
288
- tools=tools,
289
- )
290
-
291
- system_console.print(
292
- "\n---------------------------------------\n# Assistant response",
293
- style="bold",
294
- )
295
- tool_call_args_by_id = DefaultDict[str, DefaultDict[int, str]](
296
- lambda: DefaultDict(str)
297
- )
298
- _histories: History = []
299
- item: ChatCompletionMessageParam
300
- full_response: str = ""
301
- image_histories: History = []
302
- try:
303
- for chunk in stream:
304
- if chunk.choices[0].finish_reason == "tool_calls":
305
- assert tool_call_args_by_id
306
- item = {
307
- "role": "assistant",
308
- "content": full_response,
309
- "tool_calls": [
310
- {
311
- "id": tool_call_id + str(toolindex),
312
- "type": "function",
313
- "function": {
314
- "arguments": tool_args,
315
- "name": type(which_tool(tool_args)).__name__,
316
- },
317
- }
318
- for tool_call_id, toolcallargs in tool_call_args_by_id.items()
319
- for toolindex, tool_args in toolcallargs.items()
320
- ],
321
- }
322
- cost_, output_toks_ = get_output_cost(
323
- config.cost_file[config.model], enc, item
324
- )
325
- cost += cost_
326
- system_console.print(
327
- f"\n---------------------------------------\n# Assistant invoked tools: {[which_tool(tool['function']['arguments']) for tool in item['tool_calls']]}"
328
- )
329
- system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
330
- output_toks += output_toks_
331
-
332
- _histories.append(item)
333
- for tool_call_id, toolcallargs in tool_call_args_by_id.items():
334
- for toolindex, tool_args in toolcallargs.items():
335
- try:
336
- output_or_dones, cost_ = get_tool_output(
337
- json.loads(tool_args),
338
- enc,
339
- limit - cost,
340
- loop,
341
- max_tokens=8000,
342
- )
343
- output_or_done = output_or_dones[0]
344
- except Exception as e:
345
- output_or_done = (
346
- f"GOT EXCEPTION while calling tool. Error: {e}"
347
- )
348
- tb = traceback.format_exc()
349
- error_console.print(output_or_done + "\n" + tb)
350
- cost_ = 0
351
- cost += cost_
352
- system_console.print(
353
- f"\nTotal cost: {config.cost_unit}{cost:.3f}"
354
- )
355
-
356
- if isinstance(output_or_done, DoneFlag):
357
- system_console.print(
358
- f"\n# Task marked done, with output {output_or_done.task_output}",
359
- )
360
- system_console.print(
361
- f"\nTotal cost: {config.cost_unit}{cost:.3f}"
362
- )
363
- return output_or_done.task_output, cost
364
-
365
- output = output_or_done
366
-
367
- if isinstance(output, ImageData):
368
- randomId = petname.Generate(2, "-")
369
- if not image_histories:
370
- image_histories.extend(
371
- [
372
- {
373
- "role": "assistant",
374
- "content": f"Share images with ids: {randomId}",
375
- },
376
- {
377
- "role": "user",
378
- "content": [
379
- {
380
- "type": "image_url",
381
- "image_url": {
382
- "url": output.dataurl,
383
- "detail": "auto",
384
- },
385
- }
386
- ],
387
- },
388
- ]
389
- )
390
- else:
391
- image_histories[0]["content"] += ", " + randomId
392
- second_content = image_histories[1]["content"]
393
- assert isinstance(second_content, list)
394
- second_content.append(
395
- {
396
- "type": "image_url",
397
- "image_url": {
398
- "url": output.dataurl,
399
- "detail": "auto",
400
- },
401
- }
402
- )
403
-
404
- item = {
405
- "role": "tool",
406
- "content": f"Ask user for image id: {randomId}",
407
- "tool_call_id": tool_call_id + str(toolindex),
408
- }
409
- else:
410
- item = {
411
- "role": "tool",
412
- "content": str(output),
413
- "tool_call_id": tool_call_id + str(toolindex),
414
- }
415
- cost_, output_toks_ = get_output_cost(
416
- config.cost_file[config.model], enc, item
417
- )
418
- cost += cost_
419
- output_toks += output_toks_
420
-
421
- _histories.append(item)
422
- waiting_for_assistant = True
423
- break
424
- elif chunk.choices[0].finish_reason:
425
- assistant_console.print("")
426
- item = {
427
- "role": "assistant",
428
- "content": full_response,
429
- }
430
- cost_, output_toks_ = get_output_cost(
431
- config.cost_file[config.model], enc, item
432
- )
433
- cost += cost_
434
- output_toks += output_toks_
435
-
436
- system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
437
- _histories.append(item)
438
- break
439
-
440
- if chunk.choices[0].delta.tool_calls:
441
- tool_call = chunk.choices[0].delta.tool_calls[0]
442
- if tool_call.function and tool_call.function.arguments:
443
- tool_call_args_by_id[tool_call.id or ""][tool_call.index] += (
444
- tool_call.function.arguments
445
- )
446
-
447
- chunk_str = chunk.choices[0].delta.content or ""
448
- assistant_console.print(chunk_str, end="")
449
- full_response += chunk_str
450
- except KeyboardInterrupt:
451
- waiting_for_assistant = False
452
- input("Interrupted...enter to redo the current turn")
453
- else:
454
- history.extend(_histories)
455
- history.extend(image_histories)
456
- save_history(history, session_id)
457
-
458
- return "Couldn't finish the task", cost
459
-
460
-
461
- if __name__ == "__main__":
462
- app()
@@ -1,78 +0,0 @@
1
- import json
2
- from pathlib import Path
3
- import select
4
- import sys
5
- import termios
6
- import traceback
7
- import tty
8
- from typing import Callable, DefaultDict, Literal, Optional, cast
9
- import openai
10
- from openai import OpenAI
11
- from openai.types.chat import (
12
- ChatCompletionMessageParam,
13
- ChatCompletionAssistantMessageParam,
14
- ChatCompletionMessage,
15
- ParsedChatCompletionMessage,
16
- )
17
- import rich
18
- from tokenizers import Tokenizer # type: ignore[import-untyped]
19
- from typer import Typer
20
- import uuid
21
-
22
- from .common import CostData, History
23
-
24
-
25
- def get_input_cost(
26
- cost_map: CostData, enc: Tokenizer, history: History
27
- ) -> tuple[float, int]:
28
- input_tokens = 0
29
- for msg in history:
30
- content = msg["content"]
31
- refusal = msg.get("refusal")
32
- if isinstance(content, list):
33
- for part in content:
34
- if "text" in part:
35
- input_tokens += len(enc.encode(part["text"]))
36
- elif content is None:
37
- if refusal is None:
38
- raise ValueError("Expected content or refusal to be present")
39
- input_tokens += len(enc.encode(str(refusal)))
40
- elif not isinstance(content, str):
41
- raise ValueError(f"Expected content to be string, got {type(content)}")
42
- else:
43
- input_tokens += len(enc.encode(content))
44
- cost = input_tokens * cost_map.cost_per_1m_input_tokens / 1_000_000
45
- return cost, input_tokens
46
-
47
-
48
- def get_output_cost(
49
- cost_map: CostData,
50
- enc: Tokenizer,
51
- item: ChatCompletionMessage | ChatCompletionMessageParam,
52
- ) -> tuple[float, int]:
53
- if isinstance(item, ChatCompletionMessage):
54
- content = item.content
55
- if not isinstance(content, str):
56
- raise ValueError(f"Expected content to be string, got {type(content)}")
57
- else:
58
- if not isinstance(item["content"], str):
59
- raise ValueError(
60
- f"Expected content to be string, got {type(item['content'])}"
61
- )
62
- content = item["content"]
63
- if item["role"] == "tool":
64
- return 0, 0
65
- output_tokens = len(enc.encode(content))
66
-
67
- if "tool_calls" in item:
68
- item = cast(ChatCompletionAssistantMessageParam, item)
69
- toolcalls = item["tool_calls"]
70
- for tool_call in toolcalls or []:
71
- output_tokens += len(enc.encode(tool_call["function"]["arguments"]))
72
- elif isinstance(item, ParsedChatCompletionMessage):
73
- if item.tool_calls:
74
- for tool_callf in item.tool_calls:
75
- output_tokens += len(enc.encode(tool_callf.function.arguments))
76
-
77
- cost = output_tokens * cost_map.cost_per_1m_output_tokens / 1_000_000
78
- return cost, output_tokens
File without changes