tt

<p>private async getIntentAIPartialFiller(<br /> &nbsp; &nbsp; userText: string,<br /> &nbsp; &nbsp; highLevelIntent: string,<br /> &nbsp; &nbsp; prescriptions: any[],<br /> &nbsp; &nbsp; configPrefix: string,<br /> &nbsp; &nbsp; masterIntentResponse: MasterIntentClassifierResponse<br /> ) {<br /> &nbsp; &nbsp; const startTime = performance.now();</p> <p>&nbsp; &nbsp; // Extract drug names<br /> &nbsp; &nbsp; const drugResp = this.extractDrugNames(prescriptions, highLevelIntent);<br /> &nbsp; &nbsp; masterIntentResponse.drugList = drugResp?.drugList;</p> <p>&nbsp; &nbsp; // Process all drug types in one iteration<br /> &nbsp; &nbsp; const drugCategories = [&#39;refillable&#39;, &#39;expeditedPrescriptionEligible&#39;, &#39;transferPrescriptionEligible&#39;, &#39;cancelPrescriptionEligible&#39;];<br /> &nbsp; &nbsp; const categorizedDrugs = this.extractAndProcessDrugsByEligibility(prescriptions, drugCategories);</p> <p>&nbsp; &nbsp; masterIntentResponse.refillEligibleDrugs = categorizedDrugs.refillable;<br /> &nbsp; &nbsp; masterIntentResponse.expediteEligibleDrugs = categorizedDrugs.expeditedPrescriptionEligible;<br /> &nbsp; &nbsp; masterIntentResponse.transferEligibleDrugs = categorizedDrugs.transferPrescriptionEligible;<br /> &nbsp; &nbsp; masterIntentResponse.cancelEligibleDrugs = categorizedDrugs.cancelPrescriptionEligible;</p> <p>&nbsp; &nbsp; // Consolidated logging for better efficiency<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;drugLists&#39;, JSON.stringify({<br /> &nbsp; &nbsp; &nbsp; &nbsp; drugList: masterIntentResponse.drugList,<br /> &nbsp; &nbsp; &nbsp; &nbsp; categorizedDrugs,<br /> &nbsp; &nbsp; }));</p> <p>&nbsp; &nbsp; // Prepare AI request<br /> &nbsp; &nbsp; const intentAIRequest: any = {<br /> &nbsp; &nbsp; &nbsp; &nbsp; additional_data: {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: JSON.stringify(drugResp),<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query: userText,<br /> &nbsp; &nbsp; &nbsp; &nbsp; },<br /> &nbsp; &nbsp; &nbsp; &nbsp; prompt_id: this.configService.get(`client.${configPrefix}.LLMClassifier.promptConfig.partialFiller`),<br /> &nbsp; &nbsp; };</p> <p>&nbsp; &nbsp; this.logger.addFunctionalTags(&#39;getIntentAIPartialFiller&#39;, intentAIRequest);</p> <p>&nbsp; &nbsp; // API call - can be parallelized if needed<br /> &nbsp; &nbsp; const clientName = `${configPrefix}.intentAI`;<br /> &nbsp; &nbsp; const uri = this.configService.get&lt;string&gt;(`client.${clientName}.uri`);<br /> &nbsp; &nbsp;&nbsp;<br /> &nbsp; &nbsp; const intentAIResponse = await this.axiosUtil.getInstance(clientName).post(uri, intentAIRequest);</p> <p>&nbsp; &nbsp; const duration = performance.now() - startTime;<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;IntentAIPartialFillerTimeTaken&#39;, `${duration}ms`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;IntentAIPartialFillerResponse&#39;, intentAIResponse?.chat_response);</p> <p>&nbsp; &nbsp; return intentAIResponse;<br /> }</p> <p>// **Optimized method to extract &amp; process drugs in a single iteration**<br /> private extractAndProcessDrugsByEligibility(<br /> &nbsp; &nbsp; prescriptions: any[],<br /> &nbsp; &nbsp; criteria: string[]<br /> ): Record&lt;string, string[]&gt; {<br /> &nbsp; &nbsp; if (!prescriptions || !Array.isArray(prescriptions)) return {};</p> <p>&nbsp; &nbsp; return prescriptions.reduce((acc, prescription) =&gt; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; criteria.forEach((criterion) =&gt; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (prescription[criterion]) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!acc[criterion]) acc[criterion] = [];<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let processedDrug = this.processDrugName(prescription.drugName);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acc[criterion].push(processedDrug);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; });<br /> &nbsp; &nbsp; &nbsp; &nbsp; return acc;<br /> &nbsp; &nbsp; }, {} as Record&lt;string, string[]&gt;);<br /> }</p> <p>// **Process a single drug name (abbreviation &amp; capitalization in one function)**<br /> private processDrugName(drugName: string): string {<br /> &nbsp; &nbsp; if (!drugName) return &#39;&#39;;<br /> &nbsp; &nbsp; const replacements = this.configService.get(&#39;replacements&#39;);</p> <p>&nbsp; &nbsp; return drugName<br /> &nbsp; &nbsp; &nbsp; &nbsp; .split(&#39; &#39;)<br /> &nbsp; &nbsp; &nbsp; &nbsp; .map((word) =&gt; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const lowerCaseWord = word.toLowerCase();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return replacements[lowerCaseWord] ? replacements[lowerCaseWord] : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();<br /> &nbsp; &nbsp; &nbsp; &nbsp; })<br /> &nbsp; &nbsp; &nbsp; &nbsp; .join(&#39; &#39;);<br /> }</p> <p>// **Extract drug names while processing them efficiently**<br /> private extractDrugNames(prescriptions: any[], intent: string) {<br /> &nbsp; &nbsp; const vaccineList = this.configService.get(&#39;vaccineList&#39;);</p> <p>&nbsp; &nbsp; if (!prescriptions || !Array.isArray(prescriptions)) return { intent, drugList: [], vaccineList };</p> <p>&nbsp; &nbsp; const drugList = prescriptions<br /> &nbsp; &nbsp; &nbsp; &nbsp; .filter(prescription =&gt; prescription.drugName)<br /> &nbsp; &nbsp; &nbsp; &nbsp; .map(prescription =&gt; this.processDrugName(prescription.drugName));</p> <p>&nbsp; &nbsp; return { intent, drugList, vaccineList };<br /> }<br /> &nbsp;</p>