( chatGptPrompt: ChatGPTMessage[], fnNames?: string[], )
| 87 | }; |
| 88 | |
| 89 | export function hideLongGraphOutputs( |
| 90 | chatGptPrompt: ChatGPTMessage[], |
| 91 | fnNames?: string[], |
| 92 | ): { |
| 93 | chatGptPrompt: ChatGPTMessage[]; |
| 94 | graphDataHidden: boolean; |
| 95 | } { |
| 96 | const names = fnNames ?? [dataAnalysisActionName]; |
| 97 | let graphDataHidden = false; |
| 98 | const out = chatGptPrompt.map((m) => { |
| 99 | if (m.role === "function" && names.includes(m.name)) { |
| 100 | if ( |
| 101 | // No newlines in our minified JSONs |
| 102 | !m.content.includes("\n") && |
| 103 | // Below are to check it's a JSON |
| 104 | ["{", "["].includes(m.content[0]) && |
| 105 | ["}", "]"].includes(m.content[m.content.length - 1]) && |
| 106 | // Got to be long otherwise no point |
| 107 | getTokenCount([m]) > 500 |
| 108 | ) { |
| 109 | try { |
| 110 | const graphData = JSON.parse(m.content); |
| 111 | const arrayLength = graphData.data.length; |
| 112 | const graphOrTable = graphData.type === "table" ? "table" : "graph"; |
| 113 | let dataStr = ""; |
| 114 | if (getTokenCount(JSON.stringify(graphData.data.slice(0, 3))) > 300) { |
| 115 | dataStr = `[${JSON.stringify(graphData.data[0])},`; |
| 116 | } else { |
| 117 | dataStr = `[${graphData.data |
| 118 | .slice(0, 3) |
| 119 | .map(JSON.stringify) |
| 120 | .join(",")},`; |
| 121 | } |
| 122 | delete graphData.data; |
| 123 | m.content = JSON.stringify(graphData); |
| 124 | dataStr += `<further elements cut for brevity (total length: ${arrayLength}) - DO NOT pretend to know the data, instead tell the user to look at this ${graphOrTable}>]`; |
| 125 | m.content = m.content.slice(0, -1) + ',"data":' + dataStr + "}"; |
| 126 | graphDataHidden = true; |
| 127 | } catch (e) {} |
| 128 | } else if (getTokenCount([m]) > 500) { |
| 129 | // Hiding logs |
| 130 | m.content = "<cut for brevity - DO NOT pretend to know the logs>"; |
| 131 | } |
| 132 | } |
| 133 | return m; |
| 134 | }); |
| 135 | return { chatGptPrompt: out, graphDataHidden }; |
| 136 | } |
| 137 | |
| 138 | export async function Bertie( // Bertie will eat you for breakfast |
| 139 | controller: ReadableStreamDefaultController, |
no test coverage detected