File size: 20,999 Bytes
3d3d712 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
import os
from injector import Injector
from taskweaver.code_interpreter.code_generator.code_generator_plugin_only import _compose_prompt
from taskweaver.config.config_mgt import AppConfigSource
from taskweaver.logging import LoggingModule
from taskweaver.memory.attachment import AttachmentType
from taskweaver.memory.plugin import PluginModule
def test_compose_prompt():
app_injector = Injector(
[PluginModule, LoggingModule],
)
app_config = AppConfigSource(
config={
"app_dir": os.path.dirname(os.path.abspath(__file__)),
"llm.api_key": "this_is_not_a_real_key", # pragma: allowlist secret
"code_generator.prompt_compression": True,
"code_generator.prompt_file_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/prompts/generator_prompt.yaml",
),
},
)
app_injector.binder.bind(AppConfigSource, to=app_config)
from taskweaver.code_interpreter.code_generator import CodeGenerator
from taskweaver.memory import Attachment, Memory, Post, Round
code_generator = app_injector.create_object(CodeGenerator)
code1 = (
"df = pd.DataFrame(np.random.rand(10, 2), columns=['DATE', 'VALUE'])\n"
'descriptions = [("sample_code_description", "Sample code has been generated to get a dataframe `df` \n'
"with 10 rows and 2 columns: 'DATE' and 'VALUE'\")]"
)
post1 = Post.create(
message="create a dataframe",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
post2 = Post.create(
message="A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
send_from="CodeInterpreter",
send_to="Planner",
attachment_list=[],
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees the user wants generate a DataFrame.",
),
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees all required Python libs have been imported, so will not generate import codes.",
),
)
post2.add_attachment(Attachment.create(AttachmentType.python, code1))
post2.add_attachment(Attachment.create(AttachmentType.execution_status, "SUCCESS"))
post2.add_attachment(
Attachment.create(
AttachmentType.execution_result,
"A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
),
)
round1 = Round.create(user_query="hello", id="round-1")
round1.add_post(post1)
round1.add_post(post2)
round2 = Round.create(user_query="hello again", id="round-2")
post3 = Post.create(
message="what is the data range",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
post4 = Post.create(
message="The data range for the 'VALUE' column is 0.94",
send_from="CodeInterpreter",
send_to="Planner",
attachment_list=[],
)
post4.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} understands the user wants to find the data range for the DataFrame.",
),
)
post4.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} will generate code to calculate the data range of the 'VALUE' column since it is the "
"only numeric column.",
),
)
post4.add_attachment(
Attachment.create(
AttachmentType.python,
(
"min_value = df['VALUE'].min()\n"
"max_value = df['VALUE'].max()\n"
"data_range = max_value - min_value\n"
"descriptions = [\n"
'("min_value", f"The minimum value in the \'VALUE\' column is {min_value:.2f}"),\n'
'("max_value", f"The maximum value in the \'VALUE\' column is {max_value:.2f}"),\n'
'("data_range", f"The data range for the \'VALUE\' column is {data_range:.2f}")\n'
"]"
),
),
)
post4.add_attachment(Attachment.create(AttachmentType.execution_status, "SUCCESS"))
post4.add_attachment(
Attachment.create(
AttachmentType.execution_result,
"The minimum value in the 'VALUE' column is 0.05;The "
"maximum value in the 'VALUE' column is 0.99;The "
"data range for the 'VALUE' column is 0.94",
),
)
round2.add_post(post3)
round2.add_post(post4)
round3 = Round.create(user_query="hello again", id="round-3")
post5 = Post.create(
message="what is the max value?",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
round3.add_post(post5)
memory = Memory(session_id="session-1")
memory.conversation.add_round(round1)
memory.conversation.add_round(round2)
memory.conversation.add_round(round3)
messages = code_generator.compose_prompt(
rounds=memory.conversation.rounds,
plugins=code_generator.get_plugin_pool(),
)
assert messages[0]["role"] == "system"
assert messages[0]["content"].startswith("## On conversations:")
assert messages[1]["role"] == "user"
assert messages[1]["content"] == (
"==============================\n"
"## Conversation Start\n"
"\n"
"### Context Summary\n"
"The context summary of previous rounds and the variables that "
"ProgramApe can refer to:\n"
"None\n"
"\n"
"### Plugin Functions\n"
"The functions can be directly called without importing:\n"
"None\n"
"-----------------------------\n"
"# Feedback of the code in the last round (None if no feedback):\n"
"None\n"
"\n"
"# Request from the User in this round:\n"
"create a dataframe"
)
assert messages[2]["role"] == "assistant"
assert messages[2]["content"] == (
'{"response": [{"type": "thought", "content": "ProgramApe sees the user wants '
'generate a DataFrame."}, {"type": "thought", "content": "ProgramApe sees all '
"required Python libs have been imported, so will not generate import "
'codes."}, {"type": "python", "content": "df = pd.DataFrame(np.random.rand(10, '
"2), columns=['DATE', 'VALUE'])\\ndescriptions = "
'[(\\"sample_code_description\\", \\"Sample code has been generated to get a '
"dataframe `df` \\nwith 10 rows and 2 columns: 'DATE' and "
"'VALUE'\\\")]\"}]}"
)
assert messages[5]["role"] == "user"
assert messages[5]["content"] == (
"-----------------------------\n"
"# Feedback of the code in the last round (None if no feedback):\n"
"## Execution\n"
"Your code has been executed successfully with the following result:\n"
"The minimum value in the 'VALUE' column is 0.05;The maximum value in the "
"'VALUE' column is 0.99;The data range for the 'VALUE' column is 0.94\n"
"\n"
"\n"
"# Request from the User in this round:\n"
"what is the max value?\n"
"Please follow the instructions below to complete the task:\n"
"- ProgramApe can refer to intermediate variables in the generated code from "
"previous successful rounds and the context summary in the current "
"Conversation, \n"
"- ProgramApe should not refer to any information from failed rounds, rounds "
"that have not been executed, or previous Conversations.\n"
"- ProgramApe put all the result variables in the last line of the code.\n"
"- ProgramApe must not import the plugins and otherwise the code will be "
"failed to execute.\n"
)
def test_compose_prompt_with_plugin():
app_injector = Injector(
[PluginModule, LoggingModule],
)
app_config = AppConfigSource(
config={
"app_dir": os.path.dirname(os.path.abspath(__file__)),
"llm.api_key": "test_key", # pragma: allowlist secret
"code_generator.prompt_compression": True,
"code_generator.prompt_file_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/prompts/generator_prompt.yaml",
),
"plugin.base_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/plugins",
),
},
)
app_injector.binder.bind(AppConfigSource, to=app_config)
from taskweaver.code_interpreter.code_generator import CodeGenerator
from taskweaver.memory import Attachment, Memory, Post, Round
code_generator = app_injector.create_object(CodeGenerator)
code1 = (
"df = pd.DataFrame(np.random.rand(10, 2), columns=['DATE', 'VALUE'])\n"
'descriptions = [("sample_code_description", "Sample code has been generated to get a dataframe `df` \n'
"with 10 rows and 2 columns: 'DATE' and 'VALUE'\")]"
)
post1 = Post.create(
message="create a dataframe",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
post2 = Post.create(
message="A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
send_from="CodeInterpreter",
send_to="Planner",
attachment_list=[],
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees the user wants generate a DataFrame.",
),
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees all required Python libs have been imported, so will not generate import codes.",
),
)
post2.add_attachment(Attachment.create(AttachmentType.python, code1))
post2.add_attachment(Attachment.create(AttachmentType.execution_status, "SUCCESS"))
post2.add_attachment(
Attachment.create(
AttachmentType.execution_result,
"A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
),
)
round1 = Round.create(user_query="hello", id="round-1")
round1.add_post(post1)
round1.add_post(post2)
memory = Memory(session_id="session-1")
memory.conversation.add_round(round1)
messages = code_generator.compose_prompt(
rounds=memory.conversation.rounds,
plugins=code_generator.get_plugin_pool(),
)
assert messages[1]["role"] == "user"
assert "sql_pull_data" in messages[1]["content"]
assert "anomaly_detection" in messages[1]["content"]
assert "klarna_search" in messages[1]["content"]
assert "paper_summary" in messages[1]["content"]
def test_compose_prompt_with_plugin_only():
app_injector = Injector(
[PluginModule, LoggingModule],
)
app_config = AppConfigSource(
config={
"app_dir": os.path.dirname(os.path.abspath(__file__)),
"llm.api_key": "test_key", # pragma: allowlist secret
"code_generator.prompt_compression": False,
"code_generator.prompt_file_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/prompts/generator_plugin_only.yaml",
),
"plugin.base_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/plugins",
),
},
)
app_injector.binder.bind(AppConfigSource, to=app_config)
from taskweaver.code_interpreter.code_generator import CodeGeneratorPluginOnly
from taskweaver.memory import Attachment, Memory, Post, Round
code_generator = app_injector.get(CodeGeneratorPluginOnly)
code1 = "r0 = klarna_search('iphone')\n" "r0"
post1 = Post.create(
message="find iphones on sale",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
post2 = Post.create(
message="The iphone 15 pro is on sale.",
send_from="CodeInterpreter",
send_to="Planner",
attachment_list=[],
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees the user wants to find iphones on sale.",
),
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} can use the `klarna_search` function to find iphones on sale.",
),
)
post2.add_attachment(Attachment.create(AttachmentType.python, code1))
post2.add_attachment(Attachment.create(AttachmentType.execution_status, "SUCCESS"))
post2.add_attachment(
Attachment.create(
AttachmentType.execution_result,
"A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
),
)
round1 = Round.create(user_query="find iphones on sale", id="round-1")
round1.add_post(post1)
round1.add_post(post2)
memory = Memory(session_id="session-1")
memory.conversation.add_round(round1)
messages, functions = _compose_prompt(
system_instructions=code_generator.instruction_template.format(
ROLE_NAME=code_generator.role_name,
),
rounds=memory.conversation.rounds,
plugin_pool=code_generator.plugin_pool,
)
assert len(functions) == 1
assert functions[0]["function"]["name"] == "klarna_search"
assert messages[1]["role"] == "user"
assert messages[1]["content"] == "find iphones on sale"
assert messages[2]["role"] == "assistant"
assert messages[2]["content"] == "The iphone 15 pro is on sale."
def test_compose_prompt_with_not_plugin_only():
app_injector = Injector(
[PluginModule, LoggingModule],
)
app_config = AppConfigSource(
config={
"app_dir": os.path.dirname(os.path.abspath(__file__)),
"llm.api_key": "test_key", # pragma: allowlist secret
"code_generator.prompt_compression": True,
"code_generator.prompt_file_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/prompts/generator_prompt.yaml",
),
"plugin.base_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/plugins",
),
"code_generator.example_base_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/examples/codeinterpreter_examples",
),
},
)
app_injector.binder.bind(AppConfigSource, to=app_config)
from taskweaver.code_interpreter.code_generator import CodeGenerator
from taskweaver.memory import Attachment, Memory, Post, Round
code_generator = app_injector.get(CodeGenerator)
code1 = (
"df = pd.DataFrame(np.random.rand(10, 2), columns=['DATE', 'VALUE'])\n"
'descriptions = [("sample_code_description", "Sample code has been generated to get a dataframe `df` \n'
"with 10 rows and 2 columns: 'DATE' and 'VALUE'\")]"
)
post1 = Post.create(
message="create a dataframe",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
post2 = Post.create(
message="A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
send_from="CodeInterpreter",
send_to="Planner",
attachment_list=[],
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees the user wants generate a DataFrame.",
),
)
post2.add_attachment(
Attachment.create(
AttachmentType.thought,
"{ROLE_NAME} sees all required Python libs have been imported, so will not generate import codes.",
),
)
post2.add_attachment(Attachment.create(AttachmentType.python, code1))
post2.add_attachment(Attachment.create(AttachmentType.execution_status, "SUCCESS"))
post2.add_attachment(
Attachment.create(
AttachmentType.execution_result,
"A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
),
)
round1 = Round.create(user_query="hello", id="round-1")
round1.add_post(post1)
round1.add_post(post2)
memory = Memory(session_id="session-1")
memory.conversation.add_round(round1)
messages = code_generator.compose_prompt(
rounds=memory.conversation.rounds,
plugins=code_generator.get_plugin_pool(),
)
assert len(code_generator.plugin_pool) == 4
assert "anomaly_detection" in messages[16]["content"]
assert "klarna_search" in messages[16]["content"]
assert "paper_summary" in messages[16]["content"]
assert "sql_pull_data" in messages[16]["content"]
def test_code_correction_prompt():
app_injector = Injector(
[PluginModule, LoggingModule],
)
app_config = AppConfigSource(
config={
"app_dir": os.path.dirname(os.path.abspath(__file__)),
"llm.api_key": "test_key", # pragma: allowlist secret
"code_generator.prompt_file_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/prompts/generator_prompt.yaml",
),
},
)
app_injector.binder.bind(AppConfigSource, to=app_config)
from taskweaver.code_interpreter.code_generator import CodeGenerator
from taskweaver.memory import Attachment, Memory, Post, Round
prompt_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/prompts/generator_prompt.yaml",
)
code_generator = app_injector.create_object(CodeGenerator)
code1 = (
"df = pd.DataFrame(np.random.rand(10, 2), columns=['DATE', 'VALUE'])\n"
'descriptions = [("sample_code_description", "Sample code has been generated to get a dataframe `df` \n'
"with 10 rows and 2 columns: 'DATE' and 'VALUE'\")]"
)
post1 = Post.create(
message="create a dataframe",
send_from="Planner",
send_to="CodeInterpreter",
attachment_list=[],
)
post2 = Post.create(
message="A dataframe `df` with 10 rows and 2 columns: 'DATE' and 'VALUE' has been generated.",
send_from="CodeInterpreter",
send_to="CodeInterpreter",
attachment_list=[],
)
post2.add_attachment(
Attachment.create(
"thought",
"{ROLE_NAME} sees the user wants generate a DataFrame.",
),
)
post2.add_attachment(
Attachment.create(
"thought",
"{ROLE_NAME} sees all required Python libs have been imported, so will not generate import codes.",
),
)
post2.add_attachment(Attachment.create("python", code1))
post2.add_attachment(Attachment.create("execution_status", "FAILURE"))
post2.add_attachment(
Attachment.create(
"execution_result",
"The code failed to execute. Please check the code and try again.",
),
)
post2.add_attachment(
Attachment.create("revise_message", "Please check the code and try again."),
)
round1 = Round.create(user_query="hello", id="round-1")
round1.add_post(post1)
round1.add_post(post2)
memory = Memory(session_id="session-1")
memory.conversation.add_round(round1)
messages = code_generator.compose_prompt(
rounds=memory.conversation.rounds,
plugins=code_generator.get_plugin_pool(),
)
assert len(messages) == 4
assert messages[3]["role"] == "user"
assert messages[3]["content"] == (
"-----------------------------\n"
"# Feedback of the code in the last round (None if no feedback):\n"
"## Execution\n"
"Your code has failed to execute with the following error:\n"
"The code failed to execute. Please check the code and try again.\n"
"\n"
"\n"
"# Request from the User in this round:\n"
"Please check the code and try again.\n"
"Please follow the instructions below to complete the task:\n"
"- ProgramApe can refer to intermediate variables in the generated code from "
"previous successful rounds and the context summary in the current "
"Conversation, \n"
"- ProgramApe should not refer to any information from failed rounds, rounds "
"that have not been executed, or previous Conversations.\n"
"- ProgramApe put all the result variables in the last line of the code.\n"
"- ProgramApe must not import the plugins and otherwise the code will be "
"failed to execute.\n"
)
|