( controller: ReadableStreamDefaultController, reqData: AnswersType, actions: ActionPlusApiInfo[], org: OrgJoinIsPaidFinetunedModels, conversationId: number, chatHistory: GPTMessageInclSummary[], language: string | null, )
| 55 | ); |
| 56 | |
| 57 | export async function matchQuestionToAnswer( |
| 58 | controller: ReadableStreamDefaultController, |
| 59 | reqData: AnswersType, |
| 60 | actions: ActionPlusApiInfo[], |
| 61 | org: OrgJoinIsPaidFinetunedModels, |
| 62 | conversationId: number, |
| 63 | chatHistory: GPTMessageInclSummary[], |
| 64 | language: string | null, |
| 65 | ): Promise<{ |
| 66 | nonSystemMessages: GPTMessageInclSummary[]; |
| 67 | cost: number; |
| 68 | numUserQueries: number; |
| 69 | }> { |
| 70 | console.log("Yond Cassius has a lean and hungry look"); |
| 71 | const streamInfo = await preamble(controller, conversationId); |
| 72 | let totalCost = 0; |
| 73 | |
| 74 | // Check approval_messages cache |
| 75 | const { data: approvalQuestionMatch, error: approvalQErr } = await supabase |
| 76 | .from("approval_questions") |
| 77 | .select( |
| 78 | "answer_id, variable_values, approval_answers!inner(id, is_docs, approval_answer_messages(raw_text,message_type,message_idx))", |
| 79 | ) |
| 80 | .match({ |
| 81 | embedded_text: reqData.user_input, |
| 82 | org_id: org.id, |
| 83 | "approval_answers.approved": true, |
| 84 | }); |
| 85 | if (approvalQErr) throw new Error(approvalQErr.message); |
| 86 | |
| 87 | // Perfect match of an approved question |
| 88 | if (approvalQuestionMatch && approvalQuestionMatch.length > 0) { |
| 89 | console.log( |
| 90 | `There's a match with answer id: ${approvalQuestionMatch[0].answer_id}`, |
| 91 | ); |
| 92 | const approvalQuestion = approvalQuestionMatch[0]; |
| 93 | const userMessage = chatHistory[chatHistory.length - 1]; |
| 94 | if (userMessage.role === "user") |
| 95 | userMessage.chat_summary = reqData.user_input; |
| 96 | |
| 97 | const { data: variables, error: variableError } = await supabase |
| 98 | .from("approval_variables") |
| 99 | .select("*") |
| 100 | .eq("org_id", org.id); |
| 101 | if (variableError) throw new Error(variableError.message); |
| 102 | |
| 103 | chatHistory = await executeMessages( |
| 104 | approvalQuestion.approval_answers!.approval_answer_messages.sort( |
| 105 | (a, b) => a.message_idx - b.message_idx, |
| 106 | ), |
| 107 | approvalQuestion.approval_answers!.is_docs, |
| 108 | reqData.user_input, |
| 109 | actions, |
| 110 | org, |
| 111 | streamInfo, |
| 112 | chatHistory, |
| 113 | reqData, |
| 114 | variables.map((v) => ({ |
nothing calls this directly
no test coverage detected