(
instruction: string,
actions: ActionPlusApiInfo[],
fullChatHistory: GPTMessageInclSummary[],
org: Pick<Organization, "id" | "name" | "description">,
dbData: { conversationId: number; index: number },
cache: LlmResponseCache,
)
| 68 | } |
| 69 | |
| 70 | export async function runDataAnalysis( |
| 71 | instruction: string, |
| 72 | actions: ActionPlusApiInfo[], |
| 73 | fullChatHistory: GPTMessageInclSummary[], |
| 74 | org: Pick<Organization, "id" | "name" | "description">, |
| 75 | dbData: { conversationId: number; index: number }, |
| 76 | cache: LlmResponseCache, |
| 77 | ): Promise<GraphData | { error: string } | null> { |
| 78 | // Step by step, go back through user-response sequences until we find the last function |
| 79 | // message that's not a data analysis call |
| 80 | const relevantMessages = getRelevantMessages(fullChatHistory); |
| 81 | if (!relevantMessages) { |
| 82 | console.info("No relevant messages found"); |
| 83 | return { |
| 84 | error: "Cannot perform data analysis: no APIs called", |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | // We want to get the output of all API calls since last user message from responseMessages: |
| 89 | // First, split responseMessages into an array of pairs of assistant message & sequence of |
| 90 | // function messages that follow it |
| 91 | const assistantFnMessagePairs = getAssistantFnMessagePairs(relevantMessages); |
| 92 | console.log("assistantFnMessagePairs", assistantFnMessagePairs); |
| 93 | |
| 94 | // Then convert to 'CalledAction' format |
| 95 | const calledActions = getCalledActions(assistantFnMessagePairs, actions); |
| 96 | console.log("calledActions", calledActions); |
| 97 | const varNames = getVarNames(calledActions); |
| 98 | console.log( |
| 99 | "varNames", |
| 100 | varNames, |
| 101 | "number of calledActions:", |
| 102 | calledActions.length, |
| 103 | ); |
| 104 | const dataAnalysisPrompt = getDataAnalysisPrompt( |
| 105 | instruction, |
| 106 | calledActions, |
| 107 | varNames, |
| 108 | org, |
| 109 | ); |
| 110 | |
| 111 | let { llmResponse, graphData } = await cache.checkAnalyticsCache( |
| 112 | dataAnalysisPrompt[1].content, |
| 113 | org.id, |
| 114 | fullChatHistory, |
| 115 | supabase, |
| 116 | ); |
| 117 | if (graphData) { |
| 118 | await saveAnalyticsToDB( |
| 119 | dataAnalysisPrompt[1].content, |
| 120 | llmResponse, |
| 121 | org.id, |
| 122 | dbData, |
| 123 | ); |
| 124 | return graphData as GraphData; |
| 125 | } |
| 126 | |
| 127 | if (!llmResponse) { |
no test coverage detected