( executeCodeResponse: ExecuteCode2Item[], )
| 479 | } |
| 480 | |
| 481 | export function convertToGraphData( |
| 482 | executeCodeResponse: ExecuteCode2Item[], |
| 483 | ): (GraphMessage | FunctionMessage)[] { |
| 484 | if (executeCodeResponse.length === 0) { |
| 485 | throw new Error("No logs, errors or API calls from code execution"); |
| 486 | } |
| 487 | let functionMessage: FunctionMessage = { |
| 488 | role: "function", |
| 489 | name: dataAnalysisActionName, |
| 490 | content: |
| 491 | `Logs from code execution and API calls for ${dataAnalysisActionName}:\n` + |
| 492 | executeCodeResponse |
| 493 | .filter((m) => ["call", "log"].includes(m.type)) |
| 494 | .map((m) => |
| 495 | m.type === "log" |
| 496 | ? m.args.message |
| 497 | : // @ts-ignore |
| 498 | `${m.args.name}(${Object.entries(m.args.params) |
| 499 | .map(([key, value]) => `${key}=${value}`) |
| 500 | .join(", ")})`, |
| 501 | ) |
| 502 | .join("\n"), |
| 503 | }; |
| 504 | |
| 505 | const errorMessages: string[] = executeCodeResponse |
| 506 | .filter((m) => m.type === "error") |
| 507 | // @ts-ignore |
| 508 | .map((e) => e.args.message); |
| 509 | if (errorMessages.length > 0) { |
| 510 | throw new Error( |
| 511 | `Error messages found in code execution:\n${errorMessages.join("\n")}`, |
| 512 | ); |
| 513 | } |
| 514 | |
| 515 | // Sometimes the AI will write a for loop and put the plot() call in the loop, leading to multiple plots which were |
| 516 | // meant to be 1, each with 1 data point. We combine these out here |
| 517 | let plotItems = executeCodeResponse |
| 518 | .filter((g) => g.type === "plot") |
| 519 | // Convert other graph types to bar |
| 520 | .map((g) => { |
| 521 | if (!["line", "bar", "table"].includes(g.args.type)) |
| 522 | return { |
| 523 | ...g, |
| 524 | type: "bar", |
| 525 | }; |
| 526 | return g; |
| 527 | }) as { |
| 528 | type: "plot"; |
| 529 | args: BertieGraphData; |
| 530 | }[]; |
| 531 | |
| 532 | const originalDataLengths = plotItems.map((g) => g.args.data.length); |
| 533 | plotItems = plotItems |
| 534 | .map((g1, idx, items) => { |
| 535 | const matchedPlotIdx = plotItems.findIndex((g2, i) => { |
| 536 | if (i >= idx) return false; |
| 537 | return ( |
| 538 | g1.args.labels?.x === g2.args.labels?.x && |
no outgoing calls
no test coverage detected