ai-compare-candidates 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.editorconfig +51 -0
  2. package/.vscode/settings.json +3 -0
  3. package/.yarnrc.yml +16 -0
  4. package/LICENSE +6 -0
  5. package/README.md +77 -0
  6. package/dist/index.cjs +24 -0
  7. package/dist/index.cjs.map +1 -0
  8. package/dist/index.d.cts +134 -0
  9. package/dist/index.d.cts.map +1 -0
  10. package/dist/index.d.mts +134 -0
  11. package/dist/index.d.mts.map +1 -0
  12. package/dist/index.mjs +24 -0
  13. package/dist/index.mjs.map +1 -0
  14. package/example/.editorconfig +51 -0
  15. package/example/.vscode/extensions.json +13 -0
  16. package/example/.vscode/settings.json +5 -0
  17. package/example/README.md +21 -0
  18. package/example/index.html +21 -0
  19. package/example/package.json +37 -0
  20. package/example/postcss.config.js +29 -0
  21. package/example/public/favicon.ico +0 -0
  22. package/example/public/icons/favicon-128x128.png +0 -0
  23. package/example/public/icons/favicon-16x16.png +0 -0
  24. package/example/public/icons/favicon-32x32.png +0 -0
  25. package/example/public/icons/favicon-96x96.png +0 -0
  26. package/example/quasar.config.ts +222 -0
  27. package/example/src/App.vue +5 -0
  28. package/example/src/boot/electronHuggingFaceFix.ts +8 -0
  29. package/example/src/boot/icons.ts +20 -0
  30. package/example/src/css/app.scss +1 -0
  31. package/example/src/css/quasar.variables.scss +25 -0
  32. package/example/src/env.d.ts +7 -0
  33. package/example/src/layouts/app.vue +147 -0
  34. package/example/src/router/index.ts +37 -0
  35. package/example/src/router/routes.ts +8 -0
  36. package/example/src/stores/index.ts +32 -0
  37. package/example/src/stores/store.ts +19 -0
  38. package/example/tsconfig.json +3 -0
  39. package/package.json +55 -0
  40. package/src/index.ts +478 -0
  41. package/tsconfig.json +12 -0
  42. package/tsconfig.node.json +12 -0
  43. package/tsconfig.tsbuildinfo +1 -0
  44. package/tsdown.config.ts +12 -0
package/.editorconfig ADDED
@@ -0,0 +1,51 @@
1
+ root=true
2
+
3
+ [*]
4
+ charset=utf-8
5
+ indent_style=tab
6
+ indent_size=4
7
+ end_of_line=lf
8
+ insert_final_newline=false
9
+ trim_trailing_whitespace=false
10
+ space_before_type_parameters_brackets=false
11
+ space_within_type_parameters_brackets=false
12
+ space_before_colon_in_type_annotation=false
13
+ space_after_colon_in_type_annotation=false
14
+ space_around_pipe_or_amper_in_type_usage=false
15
+ space_after_comma=false
16
+ space_before_comma=false
17
+ space_before_semicolon=false
18
+ space_before_semicolon_in_for_statement=false
19
+ space_after_semicolon_in_for_statement=false
20
+ space_before_method_call_parentheses=false
21
+ space_before_empty_method_call_parentheses=false
22
+ space_between_method_call_parameter_list_parentheses=false
23
+ space_between_method_call_empty_parameter_list_parentheses=false
24
+ space_before_method_parentheses=false
25
+ space_before_method_parentheses_anonymous=false
26
+ space_between_method_declaration_empty_parameter_list_parentheses=false
27
+ space_between_method_declaration_parameter_list_parentheses=false
28
+ space_after_keywords_in_control_flow_statements=false
29
+ space_between_parentheses_of_control_flow_statements=false
30
+ space_before_open_square_brackets=false
31
+ space_between_square_brackets=false
32
+ space_before_ternary_quest=false
33
+ space_after_ternary_quest=false
34
+ space_before_ternary_colon=false
35
+ space_after_ternary_colon=false
36
+ space_before_property_colon=false
37
+ space_after_property_colon=false
38
+ space_within_object_literal_braces=false
39
+ space_within_empty_object_literal_braces=false
40
+ space_within_import_braces=false
41
+ space_between_square_brackets=false
42
+ space_between_empty_square_brackets=false
43
+ space_around_binary_operator=false
44
+ space_around_assignment_operator=false
45
+ space_around_dot=false
46
+ space_within_parentheses=false
47
+ space_before_colon_in_case=false
48
+ space_before_arrow=false
49
+ space_after_arrow=false
50
+ space_within_template_argument=false
51
+ types_braces=end_of_line_no_space
@@ -0,0 +1,3 @@
1
+ {
2
+ "typescript.tsdk": "node_modules/typescript/lib"
3
+ }
package/.yarnrc.yml ADDED
@@ -0,0 +1,16 @@
1
+ nodeLinker: node-modules
2
+
3
+ supportedArchitectures:
4
+ cpu:
5
+ - current
6
+ - x86
7
+ - x64
8
+ - arm64
9
+ - ia32
10
+ os:
11
+ - current
12
+ - darwin
13
+ - linux
14
+ - win32
15
+
16
+ yarnPath: .yarn/releases/yarn-4.12.0.cjs
package/LICENSE ADDED
@@ -0,0 +1,6 @@
1
+ © 2025 Wilson Foo Yu Kang. All rights reserved except as expressly set out hereinafter.
2
+ Unrestricted sole licence granted to Custom Automated Systems ® Pte Ltd, revocable with express written notice by Wilson Foo Yu Kang.
3
+
4
+ Dual licensed under:
5
+ CC-BY-NC-SA-4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
6
+ Proprietary sub-licence with Custom Automated Systems Pte Ltd (contact sales@customautosys.com to negotiate licence terms)
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # ai-compare-candidates
2
+
3
+ ## Compare and rank multiple candidate objects using artificial intelligence retrieval augmented generation, providing the rationale
4
+
5
+ This package allows you to rank multiple candidate objects in a customised manner by providing a user-supplied function that converts each such object into a candidate document in the form of a string. It then uses a vector database and embedding to perform a similarity search retrieval of an initial number of candidates. This search is then refined into the same or a smaller number of candidates by feeding them into a large language model which will rank the candidates. The candidate documents or part thereof can also be summarised into a defined word limit.
6
+
7
+ ## Cloning of package
8
+
9
+ After performing a git clone:
10
+
11
+ 0. This assumes that you have a suitable node version installed. If you don't already have yarn installed globally:
12
+
13
+ ```bash
14
+ npm install -g yarn
15
+ ```
16
+
17
+ 1. Remove the line ```"packageManager": "yarn@4.12.0"``` and the preceding comma in package.json
18
+ 2. Remove the line ```yarnPath: .yarn/releases/yarn-4.12.0.cjs``` from .yarnrc.yml
19
+ 3.
20
+ ```bash
21
+ yarn set version 4.12.0
22
+ yarn --immutable
23
+ ```
24
+
25
+ ### Running of example
26
+
27
+ 0. If you don't already have @quasar/cli installed globally
28
+
29
+ ```bash
30
+ npm install -g @quasar/cli
31
+ ```
32
+
33
+ 1.
34
+ ```bash
35
+ cd example
36
+ yarn workspaces focus
37
+ yarn dev
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```typescript
43
+ AICompareCandidates.compareCandidates<Candidate>({
44
+ candidates,
45
+ problemDescription='',
46
+ generateSearchAreasInstruction=this.defaultGenerateSearchAreasInstruction.bind(this),
47
+ convertCandidateToDocument=this.defaultConvertCandidateToDocument.bind(this),
48
+ candidatesForInitialSelection=2,
49
+ candidatesForFinalSelection=1,
50
+ generateRankingInstruction=this.defaultGenerateRankingInstruction.bind(this),
51
+ extractIdentifiersFromRationale=this.defaultExtractIdentifiersFromRationale.bind(this),
52
+ extractIdentifierFromCandidateDocument=this.defaultExtractIdentifierFromCandidateDocument.bind(this),
53
+ candidateIdentifierField=undefined,
54
+ getSummarisableSubstringIndices
55
+ }:AICompareCandidates.CompareArguments<Candidate>=<AICompareCandidates.CompareArguments<Candidate>>{}):Promise<AICompareCandidates.CompareCandidatesReturn<Candidate>|void>
56
+ ```
57
+ candidates: Candidate objects as you define them
58
+
59
+ problemDescription: The prompt for description of the problem, i.e. how you would like the AI model to select the candidates.
60
+
61
+ generateSearchAreasInstruction: A function for generating the instruction for determining the relevant subject areas to use to search the vector database.
62
+
63
+ convertCandidateToDocument: A function to convert a candidate object to a candidate document, i.e. a string, which can be embedded in the vector database.
64
+
65
+ candidatesForInitialSelection: The number of candidates which should be returned by the initial vector database search.
66
+
67
+ candidatesForFinalSelection: The number of candidates which should be finally selected by the text generation ranking model.
68
+
69
+ generateRankingInstruction: A function for generating the instruction for ranking the candidates returned by the initial vector database search.
70
+
71
+ extractIdentifiersFromRationale: A function for extracting the correct candidate object identifiers from the entire rationale generated by the ranking model.
72
+
73
+ extractIdentifierFromCandidateDocument: A function for extracting the correct candidate object identifier from a candidate document string.
74
+
75
+ candidateIdentifierField: The field in the candidate object which is used to identify it, e.g. name or ID number.
76
+
77
+ getSummarisableSubstringIndices: A function for identifying the substring indices of a candidate document string which can be summarised by the summarisation model.
package/dist/index.cjs ADDED
@@ -0,0 +1,24 @@
1
+ Object.defineProperty(exports,`__esModule`,{value:!0});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`@huggingface/transformers`),l=require(`@langchain/classic/vectorstores/memory`),u=require(`@langchain/core/embeddings`),d=require(`lodash`);d=s(d);let f=require(`jsan`);f=s(f);var p=class extends u.Embeddings{env=c.env;DEBUG=!0;generator=null;generatorModelName=`Xenova/LaMini-GPT-774M`;generatorPromise=null;generatorProgressInfo={};generatorProgressCallback=null;summariser=null;summariserModelName=`Xenova/distilbart-cnn-12-6`;summariserPromise=null;summariserProgressInfo={};summariserProgressCallback=null;embedder=null;embedderModelName=`Xenova/all-MiniLM-L12-v2`;embedderPromise=null;embedderProgressInfo={};embedderProgressCallback=null;tokeniser=null;tokeniserModelName=this.generatorModelName;tokeniserPromise=null;tokeniserProgressInfo={};tokeniserProgressCallback=null;generateSearchAreasMaxNewTokens=64;generateSearchAreasTemperature=.35;generateSearchAreasRepetitionPenalty=1.5;rankingMaxNewTokens=64;rankingTemperature=.35;rankingRepetitionPenalty=1.5;targetSummarisedStringTokenCount=420;static{c.env.localModelPath=``,c.env.allowRemoteModels=!0,c.env.allowLocalModels=!1}constructor(){super({})}async loadGenerator({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.generatorModelName=t),!this.generatorModelName)throw Error(`Invalid generator model name`);return e&&(this.generatorProgressCallback=e),this.generatorPromise=(0,c.pipeline)(`text-generation`,this.generatorModelName,{device:`webgpu`,progress_callback:e=>(this.DEBUG&&console.log(f.default.stringify(e)),Object.assign(this.generatorProgressInfo,e),this.generatorProgressCallback?.(e))}),this.generator=await this.generatorPromise,this.generator}async checkGeneratorLoaded(){if(this.generatorPromise||this.loadGenerator(),this.generator||await this.generatorPromise,!this.generator)throw Error(`Unable to load generator`)}async loadSummariser({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.summariserModelName=t),!this.summariserModelName)throw Error(`Invalid summariser model name`);return e&&(this.summariserProgressCallback=e),this.summariserPromise=(0,c.pipeline)(`summarization`,this.summariserModelName,{device:`webgpu`,progress_callback:e=>(this.DEBUG&&console.log(f.default.stringify(e)),Object.assign(this.summariserProgressInfo,e),this.summariserProgressCallback?.(e))}),this.summariser=await this.summariserPromise,this.summariser}async checkSummariserLoaded(){if(this.summariserPromise||this.loadSummariser(),this.summariser||await this.summariserPromise,!this.summariser)throw Error(`Unable to load summariser`)}async loadEmbedder({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.embedderModelName=t),!this.embedderModelName)throw Error(`Invalid embedder model name`);return e&&(this.embedderProgressCallback=e),this.embedderPromise=(0,c.pipeline)(`feature-extraction`,this.embedderModelName,{device:`webgpu`,progress_callback:e=>(this.DEBUG&&console.log(f.default.stringify(e)),Object.assign(this.embedderProgressInfo,e),this.embedderProgressCallback?.(e))}),this.embedder=await this.embedderPromise,this.embedder}async checkEmbedderLoaded(){if(this.embedderPromise||this.loadEmbedder(),this.embedder||await this.embedderPromise,!this.embedder)throw Error(`Unable to load embedder`)}async loadTokeniser({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.tokeniserModelName=t),!this.tokeniserModelName)throw Error(`Invalid tokeniser model name`);return e&&(this.tokeniserProgressCallback=e),this.tokeniserPromise=c.AutoTokenizer.from_pretrained(this.tokeniserModelName,{progress_callback:e=>(this.DEBUG&&console.log(f.default.stringify(e)),Object.assign(this.tokeniserProgressInfo,e),this.tokeniserProgressCallback?.(e))}),this.tokeniser=await this.tokeniserPromise,this.tokeniser}async checkTokeniserLoaded(){if(this.tokeniserPromise||this.loadTokeniser(),this.tokeniser||await this.tokeniserPromise,!this.tokeniser)throw Error(`Unable to load tokeniser`)}async embedQuery(e){return await this.checkEmbedderLoaded(),Array.from((await this.embedder?.(e,{pooling:`mean`,normalize:!0}))?.data)}async embedDocuments(e){return Promise.all(e.map(e=>this.embedQuery(e)))}generatePromptTemplate(e){return`Below is an instruction that describes a task. Write a response that appropriately completes the request.
2
+
3
+ ### Instruction:
4
+ `+e+`
5
+
6
+ ### Response:`}defaultGenerateSearchAreasInstruction(e){return`List the relevant subject areas for the following issues. Limit your response to 100 words.
7
+ Issues: "`+e+`"`}defaultConvertCandidateToDocument({candidate:e,index:t}={}){let n=`Start of Candidate #`+t;for(let t in e)n+=`
8
+ `+d.default.startCase(t)+`: `+(typeof e[t]==`object`?f.default.stringify(e[t]):String(e[t]));return n+=`
9
+ End of Candidate #`+t,n}defaultGenerateRankingInstruction({problemDescription:e,summaries:t,candidatesForFinalSelection:n,candidateIdentifierField:r}={}){return`Strictly follow these rules:
10
+ 1. Rank ONLY the top `+n+` candidates with one 15-word sentence explaining why
11
+ 2. Rank the candidates based on "`+e.replace(/(\r|\n)/g,` `)+`"
12
+ 3. If unclear, say "Insufficient information to determine"
13
+
14
+ Candidates:
15
+
16
+ `+t.join(`
17
+
18
+ `)+`
19
+
20
+ Format exactly:
21
+ #1. "Full `+d.default.startCase(r)+`": 15-word explanation
22
+ #2. ...`}regexIndexOf(e,t,n){let r=e.slice(n).search(t);return r<0?r:r+n}defaultExtractIdentifierFromCandidateDocument({candidateDocument:e,candidateIdentifierField:t}={}){this.DEBUG&&console.log(e,t);let n=d.default.startCase(t),r=e.indexOf(n);if(r<0&&(r=e.toLowerCase().indexOf(n.toLowerCase())),this.DEBUG&&console.log(r),r>=0&&(r+=n.length),r<0&&(r=e.toLowerCase().indexOf(t.toLowerCase()),r>=0&&(r+=t.length)),this.DEBUG)console.log(r);else return``;if(r=e.indexOf(`:`,r),this.DEBUG&&console.log(r),r<0&&(r=this.regexIndexOf(e,/\s+/,r)),this.DEBUG&&console.log(r),r<0)return``;let i=e.indexOf(`
23
+ `,r);return i<0&&(i=e.length),this.DEBUG&&console.log(i),e.substring(r,i).trim()}defaultExtractIdentifiersFromRationale(e){let t=/^\s*#\s*\d+\s*\.?\s*"([^"]+)"/gm,n=[];for(let r;Array.isArray(r=t.exec(e));)r[1]&&n.push(r[1]);return n}async compareCandidates({candidates:e,problemDescription:t=``,generateSearchAreasInstruction:n=this.defaultGenerateSearchAreasInstruction.bind(this),convertCandidateToDocument:r=this.defaultConvertCandidateToDocument.bind(this),candidatesForInitialSelection:i=2,candidatesForFinalSelection:a=1,generateRankingInstruction:o=this.defaultGenerateRankingInstruction.bind(this),extractIdentifiersFromRationale:s=this.defaultExtractIdentifiersFromRationale.bind(this),extractIdentifierFromCandidateDocument:c=this.defaultExtractIdentifierFromCandidateDocument.bind(this),candidateIdentifierField:u=void 0,getSummarisableSubstringIndices:f}={}){if(!Array.isArray(e)||e.length<=0)throw Error(`No candidates provided`);if(i=d.default.toSafeInteger(i),i<=0||(a=d.default.toSafeInteger(a),a<=0))throw Error(`Candidates for initial selection must be a positive integer bigger than 0`);if(i<a)throw Error(`Candidates for initial selection must be equal or more than candidates for final selection`);if(i>e.length)throw Error(`There are `+i+`candidates for initial selection which is more than the total number of candidates of `+e.length);if(a>e.length)throw Error(`There are `+a+`candidates for initial selection which is more than the total number of candidates of `+e.length);if(!u&&(u=Object.keys(e[0])[0],!u))throw Error(`No candidate identifier field`);let p=``,m=[];if(await this.checkEmbedderLoaded(),!this.embedder)return;let h=e.map((e,t)=>r({candidate:e,index:t})),g=await l.MemoryVectorStore.fromTexts(d.default.cloneDeep(h),h.map((e,t)=>t),this),_=this.generatePromptTemplate(n(t));if(this.DEBUG&&console.log(`Formatted search areas prompt: `+_),await this.checkTokeniserLoaded(),!this.tokeniser)return;if(this.tokeniser.encode(_).length>this.tokeniser.model_max_length)throw Error(`Search areas instruction prompt is too long for the tokeniser model`);if(await this.checkGeneratorLoaded(),!this.generator)return;let v=this.tokeniser.pad_token_id??this.tokeniser.sep_token_id??0,y=this.tokeniser.sep_token_id??2,b=await this.generator(_,{max_new_tokens:this.generateSearchAreasMaxNewTokens,temperature:this.generateSearchAreasTemperature,repetition_penalty:this.generateSearchAreasRepetitionPenalty,pad_token_id:v,eos_token_id:y}),x=Array.isArray(b?.[0])?b?.[0]?.[0]:b?.[0];if(!x.generated_text)throw Error(`No generated text for search areas`);this.DEBUG&&console.log(`Generated search areas response: `+x.generated_text);let S=x.generated_text.toString().indexOf(`### Response:`);S>=0?S+=13:S=0;let C=x.generated_text.toString().substring(S).trim();this.DEBUG&&console.log(`Vector search query: `+C);let w=await g.similaritySearch(C,i);this.DEBUG&&console.log(`Vector search results: `,w);let T=[];if(w.some(e=>e.pageContent.trim().split(/\s+/).length>this.targetSummarisedStringTokenCount)){if(await this.checkSummariserLoaded(),!this.summariser)return;T=(await Promise.allSettled(w.map(async e=>{if(!e.pageContent||typeof e.pageContent!=`string`)return``;if(e.pageContent.trim().split(/\s+/).length<=this.targetSummarisedStringTokenCount)return e.pageContent;let t={start:0,end:e.pageContent.length};f&&Object.assign(t,f(e.pageContent)),t.start=d.default.clamp(d.default.toSafeInteger(t.start),0,e.pageContent.length),t.end=d.default.clamp(d.default.toSafeInteger(t.end),0,e.pageContent.length);let n=e.pageContent.substring(t.start,t.end),r=e.pageContent.substring(0,t.start),i=e.pageContent.substring(t.end),a=r.split(/s+/).length+i.split(/s+/).length,o=Math.max(1,420-a),s=await this.summariser?.(n,{max_length:o}),c=r+((Array.isArray(s?.[0])?s?.[0]?.[0]:s?.[0])?.summary_text??``).split(/s+/).slice(o).join(` `)+i;return this.DEBUG&&console.log(`Summarised candidate: `+c),c}))).filter(e=>e.status===`fulfilled`&&e.value).map(e=>e.value)}else T=w.map(e=>e.pageContent);let E=this.generatePromptTemplate(o({problemDescription:t,summaries:T,candidatesForFinalSelection:a,candidateIdentifierField:String(u)}));if(this.DEBUG&&console.log(`Formatted ranking prompt: `+E),this.tokeniser.encode(E).length>this.tokeniser.model_max_length)throw Error(`Ranking instruction prompt is too long for the tokeniser model`);let D=await this.generator(E,{max_new_tokens:this.rankingMaxNewTokens,temperature:this.rankingTemperature,repetition_penalty:this.rankingRepetitionPenalty,pad_token_id:v,eos_token_id:y});p=(Array.isArray(D?.[0])?D?.[0]?.[0]:D[0]).generated_text.toString().trim().replace(/(\*\*)|(<\/?s>)|(\[.*?\])\s*/g,``),this.DEBUG&&console.log(`Generated rationale: `+p);let O=p.indexOf(`### Response:`);if(O>=0?O+=13:O=0,p=p.substring(O),p){let t=s(p);t.length>a&&(t=t.slice(0,a)),m=d.default.compact(t.map(t=>{let n=e.find(e=>String(e[u]).toLowerCase()===t.toLowerCase());return n||(n=e.find(e=>String(e[u]).toLowerCase().includes(t.toLowerCase())),n)||(n=e.find(e=>t.toLowerCase().includes(String(e[u]).toLowerCase())),n)?n:null}))}return(!Array.isArray(m)||m.length<=0)&&(m=d.default.uniq(d.default.compact(w.map(t=>{let n=c({candidateDocument:t.pageContent,candidateIdentifierField:String(u)});this.DEBUG&&console.log(`Extracted identifier from candidate document: `+n);let r=e.find(e=>String(e[u]).toLowerCase()===n.toLowerCase());return r||(r=e.find(e=>String(e[u]).toLowerCase().includes(n.toLowerCase())),r)||(r=e.find(e=>n.toLowerCase().includes(String(e[u]).toLowerCase())),r)?r:null}))).slice(a)),this.DEBUG&&console.log(`Selected candidates`,m),{rationale:p,selectedCandidates:m}}};(function(e){})(p||={});var m=p;Object.defineProperty(exports,`AICompareCandidates`,{enumerable:!0,get:function(){return p}}),exports.default=m;
24
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["Embeddings","env","AutoTokenizer","matches:string[]","match:RegExpExecArray|null","selectedCandidates:Candidate[]","MemoryVectorStore","summaries:string[]","summarisableSubstringIndices:AICompareCandidates.SummarisableSubstringIndices"],"sources":["../src/index.ts"],"sourcesContent":["import{\n\tenv,\n\tpipeline,\n\tAutoTokenizer,\n\tTextGenerationPipeline,\n\tProgressInfo,\n\tProgressCallback,\n\tSummarizationPipeline,\n\tFeatureExtractionPipeline,\n\tPreTrainedTokenizer,\n\tTextGenerationConfig\n}from '@huggingface/transformers';\nimport {MemoryVectorStore} from '@langchain/classic/vectorstores/memory';\nimport {Embeddings} from '@langchain/core/embeddings';\nimport lodash from 'lodash';\nimport jsan from 'jsan';\n\nexport class AICompareCandidates extends Embeddings{\n\treadonly env=env;\n\tDEBUG=true;\n\n\tgenerator:TextGenerationPipeline|null=null;\n\tgeneratorModelName='Xenova/LaMini-GPT-774M';\n\tgeneratorPromise:Promise<TextGenerationPipeline>|null=null;\n\tgeneratorProgressInfo:ProgressInfo=<ProgressInfo>{};\n\tgeneratorProgressCallback:ProgressCallback|null=null;\n\n\tsummariser:SummarizationPipeline|null=null;\n\tsummariserModelName='Xenova/distilbart-cnn-12-6';\n\tsummariserPromise:Promise<SummarizationPipeline>|null=null;\n\tsummariserProgressInfo:ProgressInfo=<ProgressInfo>{};\n\tsummariserProgressCallback:ProgressCallback|null=null;\n\n\tembedder:FeatureExtractionPipeline|null=null;\n\tembedderModelName='Xenova/all-MiniLM-L12-v2';\n\tembedderPromise:Promise<FeatureExtractionPipeline>|null=null;\n\tembedderProgressInfo:ProgressInfo=<ProgressInfo>{};\n\tembedderProgressCallback:ProgressCallback|null=null;\n\n\ttokeniser:PreTrainedTokenizer|null=null;\n\ttokeniserModelName=this.generatorModelName;\n\ttokeniserPromise:Promise<PreTrainedTokenizer>|null=null;\n\ttokeniserProgressInfo:ProgressInfo=<ProgressInfo>{};\n\ttokeniserProgressCallback:ProgressCallback|null=null;\n\n\tgenerateSearchAreasMaxNewTokens=64;\n\tgenerateSearchAreasTemperature=0.35;\n\tgenerateSearchAreasRepetitionPenalty=1.5;\n\n\trankingMaxNewTokens=64;\n\trankingTemperature=0.35;\n\trankingRepetitionPenalty=1.5;\n\n\ttargetSummarisedStringTokenCount=420;\n\n\tstatic{\n\t\tenv.localModelPath='';\n\t\tenv.allowRemoteModels=true;\n\t\tenv.allowLocalModels=false;\n\t}\n\n\tconstructor(){\n\t\tsuper({});\n\t}\n\t\n\tasync loadGenerator({\n\t\tprogressCallback,\n\t\tmodelName=''\n\t}:AICompareCandidates.LoadArguments=<AICompareCandidates.LoadArguments>{}){\n\t\tif(typeof modelName==='string'&&modelName)this.generatorModelName=modelName;\n\t\tif(!this.generatorModelName)throw new Error('Invalid generator model name');\n\t\tif(progressCallback)this.generatorProgressCallback=progressCallback;\n\t\t//ts-ignore is needed due to frequent error TS2590: Expression produces a union type that is too complex to represent.\n\t\t//@ts-ignore\n\t\tthis.generatorPromise=pipeline('text-generation',this.generatorModelName,{\n\t\t\tdevice:'webgpu',\n\t\t\tprogress_callback:progressInfo=>{\n\t\t\t\tif(this.DEBUG)console.log(jsan.stringify(progressInfo));\n\t\t\t\tObject.assign(this.generatorProgressInfo,progressInfo);\n\t\t\t\treturn this.generatorProgressCallback?.(progressInfo);\n\t\t\t}\n\t\t});\n\t\tthis.generator=await this.generatorPromise;\n\t\treturn this.generator;\n\t}\n\n\tasync checkGeneratorLoaded(){\n\t\tif(!this.generatorPromise)this.loadGenerator();\n\t\tif(!this.generator)await this.generatorPromise;\n\t\tif(!this.generator)throw new Error('Unable to load generator');\n\t}\n\n\tasync loadSummariser({\n\t\tprogressCallback,\n\t\tmodelName=''\n\t}:AICompareCandidates.LoadArguments=<AICompareCandidates.LoadArguments>{}){\n\t\tif(typeof modelName==='string'&&modelName)this.summariserModelName=modelName;\n\t\tif(!this.summariserModelName)throw new Error('Invalid summariser model name');\n\t\tif(progressCallback)this.summariserProgressCallback=progressCallback;\n\t\t//ts-ignore is needed due to frequent error TS2590: Expression produces a union type that is too complex to represent.\n\t\t//@ts-ignore\n\t\tthis.summariserPromise=pipeline('summarization',this.summariserModelName,{\n\t\t\tdevice:'webgpu',\n\t\t\tprogress_callback:progressInfo=>{\n\t\t\t\tif(this.DEBUG)console.log(jsan.stringify(progressInfo));\n\t\t\t\tObject.assign(this.summariserProgressInfo,progressInfo);\n\t\t\t\treturn this.summariserProgressCallback?.(progressInfo);\n\t\t\t}\n\t\t});\n\t\tthis.summariser=await this.summariserPromise;\n\t\treturn this.summariser;\n\t}\n\n\tasync checkSummariserLoaded(){\n\t\tif(!this.summariserPromise)this.loadSummariser();\n\t\tif(!this.summariser)await this.summariserPromise;\n\t\tif(!this.summariser)throw new Error('Unable to load summariser');\n\t}\n\n\tasync loadEmbedder({\n\t\tprogressCallback,\n\t\tmodelName=''\n\t}:AICompareCandidates.LoadArguments=<AICompareCandidates.LoadArguments>{}){\n\t\tif(typeof modelName==='string'&&modelName)this.embedderModelName=modelName;\n\t\tif(!this.embedderModelName)throw new Error('Invalid embedder model name');\n\t\tif(progressCallback)this.embedderProgressCallback=progressCallback;\n\t\t//ts-ignore is needed due to frequent error TS2590: Expression produces a union type that is too complex to represent.\n\t\t//@ts-ignore\n\t\tthis.embedderPromise=pipeline('feature-extraction',this.embedderModelName,{\n\t\t\tdevice:'webgpu',\n\t\t\tprogress_callback:progressInfo=>{\n\t\t\t\tif(this.DEBUG)console.log(jsan.stringify(progressInfo));\n\t\t\t\tObject.assign(this.embedderProgressInfo,progressInfo);\n\t\t\t\treturn this.embedderProgressCallback?.(progressInfo);\n\t\t\t}\n\t\t});\n\t\tthis.embedder=await this.embedderPromise;\n\t\treturn this.embedder;\n\t}\n\n\tasync checkEmbedderLoaded(){\n\t\tif(!this.embedderPromise)this.loadEmbedder();\n\t\tif(!this.embedder)await this.embedderPromise;\n\t\tif(!this.embedder)throw new Error('Unable to load embedder');\n\t}\n\n\tasync loadTokeniser({\n\t\tprogressCallback,\n\t\tmodelName=''\n\t}:AICompareCandidates.LoadArguments=<AICompareCandidates.LoadArguments>{}){\n\t\tif(typeof modelName==='string'&&modelName)this.tokeniserModelName=modelName;\n\t\tif(!this.tokeniserModelName)throw new Error('Invalid tokeniser model name');\n\t\tif(progressCallback)this.tokeniserProgressCallback=progressCallback;\n\t\t//ts-ignore is needed due to frequent error TS2590: Expression produces a union type that is too complex to represent.\n\t\t//@ts-ignore\n\t\tthis.tokeniserPromise=AutoTokenizer.from_pretrained(this.tokeniserModelName,{\n\t\t\tprogress_callback:progressInfo=>{\n\t\t\t\tif(this.DEBUG)console.log(jsan.stringify(progressInfo));\n\t\t\t\tObject.assign(this.tokeniserProgressInfo,progressInfo);\n\t\t\t\treturn this.tokeniserProgressCallback?.(progressInfo);\n\t\t\t}\n\t\t})\n\t\tthis.tokeniser=await this.tokeniserPromise;\n\t\treturn this.tokeniser;\n\t}\n\n\tasync checkTokeniserLoaded(){\n\t\tif(!this.tokeniserPromise)this.loadTokeniser();\n\t\tif(!this.tokeniser)await this.tokeniserPromise;\n\t\tif(!this.tokeniser)throw new Error('Unable to load tokeniser');\n\t}\n\n\tasync embedQuery(text:string):Promise<number[]>{\n\t\tawait this.checkEmbedderLoaded();\n\t\treturn Array.from((await this.embedder?.(text,{\n\t\t\tpooling:'mean',\n\t\t\tnormalize:true\n\t\t}))?.data);\n\t}\n\n\tasync embedDocuments(texts:string[]):Promise<number[][]>{\n\t\treturn Promise.all(texts.map(text=>this.embedQuery(text)));\n\t}\n\n\tgeneratePromptTemplate(prompt:string){\n\t\treturn 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n'+\n\t\t\t'### Instruction:\\n'+\n\t\t\tprompt+\n\t\t\t'\\n\\n### Response:';\n\t}\n\n\tdefaultGenerateSearchAreasInstruction(problemDescription:string){\n\t\treturn 'List the relevant subject areas for the following issues. Limit your response to 100 words.\\nIssues: \"'+problemDescription+'\"';\n\t}\n\n\tdefaultConvertCandidateToDocument<Candidate>({\n\t\tcandidate,\n\t\tindex\n\t}:AICompareCandidates.ConvertCandidateToDocumentArguments<Candidate>=<AICompareCandidates.ConvertCandidateToDocumentArguments<Candidate>>{}){\n\t\tlet document='Start of Candidate #'+index;\n\t\tfor(let i in candidate)document+='\\n'+lodash.startCase(i)+': '+(typeof candidate[i]==='object'?jsan.stringify(candidate[i]):String(candidate[i]));\n\t\tdocument+='\\nEnd of Candidate #'+index;\n\t\treturn document;\n\t}\n\n\tdefaultGenerateRankingInstruction({\n\t\tproblemDescription,\n\t\tsummaries,\n\t\tcandidatesForFinalSelection,\n\t\tcandidateIdentifierField\n\t}:AICompareCandidates.GenerateRankingInstructionArguments=<AICompareCandidates.GenerateRankingInstructionArguments>{}){\n\t\treturn 'Strictly follow these rules:\\n'+\n\t\t\t'1. Rank ONLY the top '+candidatesForFinalSelection+' candidates with one 15-word sentence explaining why\\n'+\n\t\t\t'2. Rank the candidates based on \"'+problemDescription.replace(/(\\r|\\n)/g,' ')+'\"\\n'+\n\t\t\t'3. If unclear, say \"Insufficient information to determine\"\\n\\n'+\n\t\t\t'Candidates:\\n\\n'+summaries.join('\\n\\n')+'\\n\\n'+\n\t\t\t'Format exactly:\\n'+\n\t\t\t'#1. \"Full '+lodash.startCase(candidateIdentifierField)+'\": 15-word explanation\\n'+\n\t\t\t'#2. ...';\n\t}\n\n\tregexIndexOf(text:string,regex:RegExp,startIndex:number){\n \tlet indexInSuffix=text.slice(startIndex).search(regex);\n \treturn indexInSuffix<0?indexInSuffix:indexInSuffix+startIndex;\n\t}\n\n\tdefaultExtractIdentifierFromCandidateDocument({\n\t\tcandidateDocument,\n\t\tcandidateIdentifierField\n\t}:AICompareCandidates.ExtractIdentifierFromCandidateDocumentArguments=<AICompareCandidates.ExtractIdentifierFromCandidateDocumentArguments>{}){\n\t\tif(this.DEBUG)console.log(candidateDocument,candidateIdentifierField);\n\t\tlet startCase=lodash.startCase(candidateIdentifierField);\n\t\tlet startIndex=candidateDocument.indexOf(startCase);\n\t\tif(startIndex<0)startIndex=candidateDocument.toLowerCase().indexOf(startCase.toLowerCase());\n\t\tif(this.DEBUG)console.log(startIndex);\n\t\tif(startIndex>=0)startIndex+=startCase.length;\n\t\tif(startIndex<0){\n\t\t\tstartIndex=candidateDocument.toLowerCase().indexOf(candidateIdentifierField.toLowerCase());\n\t\t\tif(startIndex>=0)startIndex+=candidateIdentifierField.length;\n\t\t}\n\t\tif(this.DEBUG)console.log(startIndex);\n\t\telse return '';\n\t\tstartIndex=candidateDocument.indexOf(':',startIndex);\n\t\tif(this.DEBUG)console.log(startIndex);\n\t\tif(startIndex<0)startIndex=this.regexIndexOf(candidateDocument,/\\s+/,startIndex);\n\t\tif(this.DEBUG)console.log(startIndex);\n\t\tif(startIndex<0)return '';\n\t\tlet endIndex=candidateDocument.indexOf('\\n',startIndex);\n\t\tif(endIndex<0)endIndex=candidateDocument.length;\n\t\tif(this.DEBUG)console.log(endIndex);\n\t\treturn candidateDocument.substring(startIndex,endIndex).trim();\n\t}\n\n\tdefaultExtractIdentifiersFromRationale(rationale:string){\n\t\tlet regex=/^\\s*#\\s*\\d+\\s*\\.?\\s*\"([^\"]+)\"/gm;\n\t\tlet matches:string[]=[];\n\t\tfor(let match:RegExpExecArray|null;Array.isArray(match=regex.exec(rationale));)if(match[1])matches.push(match[1]);\n\t\treturn matches;\n\t}\n\n\tasync compareCandidates<Candidate>({\n\t\tcandidates,\n\t\tproblemDescription='',\n\t\tgenerateSearchAreasInstruction=this.defaultGenerateSearchAreasInstruction.bind(this),\n\t\tconvertCandidateToDocument=this.defaultConvertCandidateToDocument.bind(this),\n\t\tcandidatesForInitialSelection=2,\n\t\tcandidatesForFinalSelection=1,\n\t\tgenerateRankingInstruction=this.defaultGenerateRankingInstruction.bind(this),\n\t\textractIdentifiersFromRationale=this.defaultExtractIdentifiersFromRationale.bind(this),\n\t\textractIdentifierFromCandidateDocument=this.defaultExtractIdentifierFromCandidateDocument.bind(this),\n\t\tcandidateIdentifierField=undefined,\n\t\tgetSummarisableSubstringIndices\n\t}:AICompareCandidates.CompareArguments<Candidate>=<AICompareCandidates.CompareArguments<Candidate>>{}):Promise<AICompareCandidates.CompareCandidatesReturn<Candidate>|void>{\n\t\tif(!Array.isArray(candidates)||candidates.length<=0)throw new Error('No candidates provided');\n\t\tcandidatesForInitialSelection=lodash.toSafeInteger(candidatesForInitialSelection);\n\t\tif(candidatesForInitialSelection<=0)throw new Error('Candidates for initial selection must be a positive integer bigger than 0');\n\t\tcandidatesForFinalSelection=lodash.toSafeInteger(candidatesForFinalSelection);\n\t\tif(candidatesForFinalSelection<=0)throw new Error('Candidates for initial selection must be a positive integer bigger than 0');\n\t\tif(candidatesForInitialSelection<candidatesForFinalSelection)throw new Error('Candidates for initial selection must be equal or more than candidates for final selection');\n\t\tif(candidatesForInitialSelection>candidates.length)throw new Error('There are '+candidatesForInitialSelection+'candidates for initial selection which is more than the total number of candidates of '+candidates.length);\n\t\tif(candidatesForFinalSelection>candidates.length)throw new Error('There are '+candidatesForFinalSelection+'candidates for initial selection which is more than the total number of candidates of '+candidates.length);\n\t\tif(!candidateIdentifierField){\n\t\t\tcandidateIdentifierField=Object.keys(candidates[0] as object)[0] as keyof Candidate;\n\t\t\tif(!candidateIdentifierField)throw new Error('No candidate identifier field');\n\t\t}\n\n\t\tlet rationale='';\n\t\tlet selectedCandidates:Candidate[]=[];\n\n\t\tawait this.checkEmbedderLoaded();\n\t\tif(!this.embedder)return;\n\t\tlet candidateDocuments=candidates.map((candidate,index)=>convertCandidateToDocument({\n\t\t\tcandidate,\n\t\t\tindex\n\t\t}));\n\t\tlet vectorStore=await MemoryVectorStore.fromTexts(\n\t\t\tlodash.cloneDeep(candidateDocuments),\n\t\t\tcandidateDocuments.map((document,index)=>index),\n\t\t\tthis\n\t\t);\n\n\t\tlet searchAreasPromptTemplate=this.generatePromptTemplate(generateSearchAreasInstruction(problemDescription));\n\t\tif(this.DEBUG)console.log('Formatted search areas prompt: '+searchAreasPromptTemplate);\n\t\tawait this.checkTokeniserLoaded();\n\t\tif(!this.tokeniser)return;\n\t\tlet searchAreasPromptTokens=this.tokeniser.encode(searchAreasPromptTemplate);\n\t\tif(searchAreasPromptTokens.length>this.tokeniser.model_max_length)throw new Error('Search areas instruction prompt is too long for the tokeniser model');\n\n\t\tawait this.checkGeneratorLoaded();\n\t\tif(!this.generator)return;\n\t\tlet pad_token_id=this.tokeniser.pad_token_id??this.tokeniser.sep_token_id??0;\n\t\tlet eos_token_id=this.tokeniser.sep_token_id??2;\n\t\tlet searchAreasReplyArray=await this.generator(searchAreasPromptTemplate,{\n\t\t\tmax_new_tokens:this.generateSearchAreasMaxNewTokens,\n\t\t\ttemperature:this.generateSearchAreasTemperature,\n\t\t\trepetition_penalty:this.generateSearchAreasRepetitionPenalty,\n\t\t\tpad_token_id,\n\t\t\teos_token_id\n\t\t});\n\t\tlet searchAreasReply=Array.isArray(searchAreasReplyArray?.[0])?searchAreasReplyArray?.[0]?.[0]:searchAreasReplyArray?.[0];\n\t\tif(!searchAreasReply.generated_text)throw new Error('No generated text for search areas');\n\t\tif(this.DEBUG)console.log('Generated search areas response: '+searchAreasReply.generated_text);\n\t\tlet searchAreasResponseIndex=searchAreasReply.generated_text.toString().indexOf('### Response:');\n\t\tif(searchAreasResponseIndex>=0)searchAreasResponseIndex+='### Response:'.length;\n\t\telse searchAreasResponseIndex=0;\n\n\t\tlet vectorSearchQuery=searchAreasReply.generated_text.toString().substring(searchAreasResponseIndex).trim();\n\t\t//generally the first sentence has the greatest relevance to the actual prompt\n\t\t//if(vectorSearchQuery.includes('.'))vectorSearchQuery=vectorSearchQuery.split('.')[0].trim();\n\t\tif(this.DEBUG)console.log('Vector search query: '+vectorSearchQuery);\n\t\tlet queryResult=await vectorStore.similaritySearch(vectorSearchQuery,candidatesForInitialSelection);\n\t\tif(this.DEBUG)console.log('Vector search results: ',queryResult);\n\n\t\tlet summaries:string[]=[];\n\t\t//only bother doing summarisation if there are candidates which exceed the token count\n\t\tif(queryResult.some(result=>result.pageContent.trim().split(/\\s+/).length>this.targetSummarisedStringTokenCount)){\n\t\t\tawait this.checkSummariserLoaded();\n\t\t\tif(!this.summariser)return;\n\t\t\tsummaries=(await Promise.allSettled(queryResult.map(async result=>{\n\t\t\t\tif(!result.pageContent||typeof result.pageContent!=='string')return '';\n\t\t\t\tif(result.pageContent.trim().split(/\\s+/).length<=this.targetSummarisedStringTokenCount)return result.pageContent;\n\t\t\t\tlet summarisableSubstringIndices:AICompareCandidates.SummarisableSubstringIndices={\n\t\t\t\t\tstart:0,\n\t\t\t\t\tend:result.pageContent.length\n\t\t\t\t};\n\t\t\t\tif(getSummarisableSubstringIndices)Object.assign(summarisableSubstringIndices,getSummarisableSubstringIndices(result.pageContent));\n\t\t\t\tsummarisableSubstringIndices.start=lodash.clamp(lodash.toSafeInteger(summarisableSubstringIndices.start),0,result.pageContent.length);\n\t\t\t\tsummarisableSubstringIndices.end=lodash.clamp(lodash.toSafeInteger(summarisableSubstringIndices.end),0,result.pageContent.length);\n\t\t\t\tlet summarisableSubstring=result.pageContent.substring(summarisableSubstringIndices.start,summarisableSubstringIndices.end);\n\t\t\t\tlet contentBefore=result.pageContent.substring(0,summarisableSubstringIndices.start);\n\t\t\t\tlet contentAfter=result.pageContent.substring(summarisableSubstringIndices.end);\n\t\t\t\tlet wordsWithoutSummarisable=contentBefore.split(/s+/).length+contentAfter.split(/s+/).length;\n\t\t\t\tlet targetSummarisedSubstringTokenCount=Math.max(1,420-wordsWithoutSummarisable);\n\t\t\t\tlet summarisedSubstringArray=await this.summariser?.(summarisableSubstring,<TextGenerationConfig>{\n\t\t\t\t\tmax_length:targetSummarisedSubstringTokenCount\n\t\t\t\t});\n\t\t\t\tlet summarisedSubstring=Array.isArray(summarisedSubstringArray?.[0])?summarisedSubstringArray?.[0]?.[0]:summarisedSubstringArray?.[0];\n\t\t\t\tlet summarisedString=contentBefore+(summarisedSubstring?.summary_text??'').split(/s+/).slice(targetSummarisedSubstringTokenCount).join(' ')+contentAfter;\n\t\t\t\tif(this.DEBUG)console.log('Summarised candidate: '+summarisedString);\n\t\t\t\treturn summarisedString;\n\t\t\t}))).filter(result=>result.status==='fulfilled'&&result.value).map(result=>(result as PromiseFulfilledResult<string>).value);\n\t\t}else{\n\t\t\tsummaries=queryResult.map(result=>result.pageContent);\n\t\t}\n\n\t\tlet rankingPromptTemplate=this.generatePromptTemplate(generateRankingInstruction({\n\t\t\tproblemDescription,\n\t\t\tsummaries,\n\t\t\tcandidatesForFinalSelection,\n\t\t\tcandidateIdentifierField:String(candidateIdentifierField)\n\t\t}));\n\t\tif(this.DEBUG)console.log('Formatted ranking prompt: '+rankingPromptTemplate);\n\t\tlet rankingPromptTokens=this.tokeniser.encode(rankingPromptTemplate);\n\t\tif(rankingPromptTokens.length>this.tokeniser.model_max_length)throw new Error('Ranking instruction prompt is too long for the tokeniser model');\n\t\tlet rankingArray=await this.generator(rankingPromptTemplate,{\n\t\t\tmax_new_tokens:this.rankingMaxNewTokens,\n\t\t\ttemperature:this.rankingTemperature,\n\t\t\trepetition_penalty:this.rankingRepetitionPenalty,\n\t\t\tpad_token_id,\n\t\t\teos_token_id\n\t\t});\n\t\tlet ranking=Array.isArray(rankingArray?.[0])?rankingArray?.[0]?.[0]:rankingArray[0];\n\t\trationale=ranking.generated_text.toString().trim().replace(/(\\*\\*)|(<\\/?s>)|(\\[.*?\\])\\s*/g, '');\n\t\tif(this.DEBUG)console.log('Generated rationale: '+rationale);\n\t\tlet rationaleResponseIndex=rationale.indexOf('### Response:');\n\t\tif(rationaleResponseIndex>=0)rationaleResponseIndex+='### Response:'.length;\n\t\telse rationaleResponseIndex=0;\n\t\trationale=rationale.substring(rationaleResponseIndex);\n\t\t//if(!rationale)throw new Error('No rationale generated');\n\n\t\tif(rationale){\n\t\t\tlet identifiers=extractIdentifiersFromRationale(rationale);\n\t\t\tif(identifiers.length>candidatesForFinalSelection)identifiers=identifiers.slice(0,candidatesForFinalSelection);\n\t\t\tselectedCandidates=lodash.compact(identifiers.map(identifier=>{\n\t\t\t\tlet selectedCandidate=candidates.find(candidate=>String(candidate[candidateIdentifierField]).toLowerCase()===identifier.toLowerCase());\n\t\t\t\tif(selectedCandidate)return selectedCandidate;\n\t\t\t\tselectedCandidate=candidates.find(candidate=>String(candidate[candidateIdentifierField]).toLowerCase().includes(identifier.toLowerCase()));\n\t\t\t\tif(selectedCandidate)return selectedCandidate;\n\t\t\t\tselectedCandidate=candidates.find(candidate=>identifier.toLowerCase().includes(String(candidate[candidateIdentifierField]).toLowerCase()));\n\t\t\t\tif(selectedCandidate)return selectedCandidate;\n\t\t\t\treturn null;\n\t\t\t}));\n\t\t}\n\n\t\tif(!Array.isArray(selectedCandidates)||selectedCandidates.length<=0){\n\t\t\tselectedCandidates=lodash.uniq(lodash.compact(queryResult.map(result=>{\n\t\t\t\tlet identifier=extractIdentifierFromCandidateDocument({\n\t\t\t\t\tcandidateDocument:result.pageContent,\n\t\t\t\t\tcandidateIdentifierField:String(candidateIdentifierField)\n\t\t\t\t});\n\t\t\t\tif(this.DEBUG)console.log('Extracted identifier from candidate document: '+identifier);\n\t\t\t\tlet selectedCandidate=candidates.find(candidate=>String(candidate[candidateIdentifierField]).toLowerCase()===identifier.toLowerCase());\n\t\t\t\tif(selectedCandidate)return selectedCandidate;\n\t\t\t\tselectedCandidate=candidates.find(candidate=>String(candidate[candidateIdentifierField]).toLowerCase().includes(identifier.toLowerCase()));\n\t\t\t\tif(selectedCandidate)return selectedCandidate;\n\t\t\t\tselectedCandidate=candidates.find(candidate=>identifier.toLowerCase().includes(String(candidate[candidateIdentifierField]).toLowerCase()));\n\t\t\t\tif(selectedCandidate)return selectedCandidate;\n\t\t\t\treturn null;\n\t\t\t}))).slice(candidatesForFinalSelection);\n\t\t}\n\t\tif(this.DEBUG)console.log('Selected candidates',selectedCandidates);\n\n\t\treturn{\n\t\t\trationale,\n\t\t\tselectedCandidates\n\t\t};\n\t}\n};\n\nexport namespace AICompareCandidates{\n\texport interface LoadArguments{\n\t\tprogressCallback?:ProgressCallback;\n\t\tmodelName:string;\n\t};\n\n\texport interface SummarisableSubstringIndices{\n\t\tstart:number;\n\t\tend:number;\n\t};\n\n\texport interface CompareArguments<Candidate>{\n\t\tcandidates:Candidate[];\n\t\tproblemDescription:string;\n\t\tgenerateSearchAreasInstruction?:(problemDescription:string)=>string;\n\t\tconvertCandidateToDocument?:(convertCandidateToDocumentArguments:ConvertCandidateToDocumentArguments<Candidate>)=>string;\n\t\tcandidatesForInitialSelection?:number;\n\t\tcandidatesForFinalSelection?:number;\n\t\tgenerateRankingInstruction?:(generateRankingInstructionArguments:GenerateRankingInstructionArguments)=>string;\n\t\textractIdentifiersFromRationale?:(rationale:string)=>string[];\n\t\textractIdentifierFromCandidateDocument?:(extractIdentifierFromCandidateDocumentArguments:ExtractIdentifierFromCandidateDocumentArguments)=>string;\n\t\tcandidateIdentifierField?:keyof Candidate;\n\t\tgetSummarisableSubstringIndices?:(candidateDocument:string)=>SummarisableSubstringIndices;\n\t};\n\n\texport interface ConvertCandidateToDocumentArguments<Candidate>{\n\t\tcandidate:Candidate;\n\t\tindex:number;\n\t};\n\n\texport interface ExtractIdentifierFromCandidateDocumentArguments{\n\t\tcandidateDocument:string;\n\t\tcandidateIdentifierField:string;\n\t};\n\n\texport interface GenerateRankingInstructionArguments{\n\t\tproblemDescription:string;\n\t\tsummaries:string[];\n\t\tcandidatesForFinalSelection:number;\n\t\tcandidateIdentifierField:string;\n\t};\n\n\texport interface CompareCandidatesReturn<Candidate>{\n\t\tselectedCandidates:Candidate[],\n\t\trationale:string\n\t};\n};\n\nexport default AICompareCandidates;"],"mappings":"otBAiBA,IAAa,EAAb,cAAyCA,EAAAA,UAAU,CAClD,IAAaC,EAAAA,IACb,MAAM,GAEN,UAAsC,KACtC,mBAAmB,yBACnB,iBAAsD,KACtD,sBAAiD,EAAE,CACnD,0BAAgD,KAEhD,WAAsC,KACtC,oBAAoB,6BACpB,kBAAsD,KACtD,uBAAkD,EAAE,CACpD,2BAAiD,KAEjD,SAAwC,KACxC,kBAAkB,2BAClB,gBAAwD,KACxD,qBAAgD,EAAE,CAClD,yBAA+C,KAE/C,UAAmC,KACnC,mBAAmB,KAAK,mBACxB,iBAAmD,KACnD,sBAAiD,EAAE,CACnD,0B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MAAM,EAA4B,EAErC,KAAK,OAAM,QAAQ,IAAI,sBAAsB,EAAmB,CAE7D,CACL,YACA,qBACA,2BAoDH,IAAA,EAAe"}
@@ -0,0 +1,134 @@
1
+ import * as _huggingface_transformers_types_env0 from "@huggingface/transformers/types/env";
2
+ import { FeatureExtractionPipeline, PreTrainedTokenizer, ProgressCallback, ProgressInfo, SummarizationPipeline, TextGenerationPipeline } from "@huggingface/transformers";
3
+ import { Embeddings } from "@langchain/core/embeddings";
4
+
5
+ //#region src/index.d.ts
6
+ declare class AICompareCandidates extends Embeddings {
7
+ readonly env: _huggingface_transformers_types_env0.TransformersEnvironment;
8
+ DEBUG: boolean;
9
+ generator: TextGenerationPipeline | null;
10
+ generatorModelName: string;
11
+ generatorPromise: Promise<TextGenerationPipeline> | null;
12
+ generatorProgressInfo: ProgressInfo;
13
+ generatorProgressCallback: ProgressCallback | null;
14
+ summariser: SummarizationPipeline | null;
15
+ summariserModelName: string;
16
+ summariserPromise: Promise<SummarizationPipeline> | null;
17
+ summariserProgressInfo: ProgressInfo;
18
+ summariserProgressCallback: ProgressCallback | null;
19
+ embedder: FeatureExtractionPipeline | null;
20
+ embedderModelName: string;
21
+ embedderPromise: Promise<FeatureExtractionPipeline> | null;
22
+ embedderProgressInfo: ProgressInfo;
23
+ embedderProgressCallback: ProgressCallback | null;
24
+ tokeniser: PreTrainedTokenizer | null;
25
+ tokeniserModelName: string;
26
+ tokeniserPromise: Promise<PreTrainedTokenizer> | null;
27
+ tokeniserProgressInfo: ProgressInfo;
28
+ tokeniserProgressCallback: ProgressCallback | null;
29
+ generateSearchAreasMaxNewTokens: number;
30
+ generateSearchAreasTemperature: number;
31
+ generateSearchAreasRepetitionPenalty: number;
32
+ rankingMaxNewTokens: number;
33
+ rankingTemperature: number;
34
+ rankingRepetitionPenalty: number;
35
+ targetSummarisedStringTokenCount: number;
36
+ constructor();
37
+ loadGenerator({
38
+ progressCallback,
39
+ modelName
40
+ }?: AICompareCandidates.LoadArguments): Promise<TextGenerationPipeline>;
41
+ checkGeneratorLoaded(): Promise<void>;
42
+ loadSummariser({
43
+ progressCallback,
44
+ modelName
45
+ }?: AICompareCandidates.LoadArguments): Promise<SummarizationPipeline>;
46
+ checkSummariserLoaded(): Promise<void>;
47
+ loadEmbedder({
48
+ progressCallback,
49
+ modelName
50
+ }?: AICompareCandidates.LoadArguments): Promise<FeatureExtractionPipeline>;
51
+ checkEmbedderLoaded(): Promise<void>;
52
+ loadTokeniser({
53
+ progressCallback,
54
+ modelName
55
+ }?: AICompareCandidates.LoadArguments): Promise<PreTrainedTokenizer>;
56
+ checkTokeniserLoaded(): Promise<void>;
57
+ embedQuery(text: string): Promise<number[]>;
58
+ embedDocuments(texts: string[]): Promise<number[][]>;
59
+ generatePromptTemplate(prompt: string): string;
60
+ defaultGenerateSearchAreasInstruction(problemDescription: string): string;
61
+ defaultConvertCandidateToDocument<Candidate>({
62
+ candidate,
63
+ index
64
+ }?: AICompareCandidates.ConvertCandidateToDocumentArguments<Candidate>): string;
65
+ defaultGenerateRankingInstruction({
66
+ problemDescription,
67
+ summaries,
68
+ candidatesForFinalSelection,
69
+ candidateIdentifierField
70
+ }?: AICompareCandidates.GenerateRankingInstructionArguments): string;
71
+ regexIndexOf(text: string, regex: RegExp, startIndex: number): number;
72
+ defaultExtractIdentifierFromCandidateDocument({
73
+ candidateDocument,
74
+ candidateIdentifierField
75
+ }?: AICompareCandidates.ExtractIdentifierFromCandidateDocumentArguments): string;
76
+ defaultExtractIdentifiersFromRationale(rationale: string): string[];
77
+ compareCandidates<Candidate>({
78
+ candidates,
79
+ problemDescription,
80
+ generateSearchAreasInstruction,
81
+ convertCandidateToDocument,
82
+ candidatesForInitialSelection,
83
+ candidatesForFinalSelection,
84
+ generateRankingInstruction,
85
+ extractIdentifiersFromRationale,
86
+ extractIdentifierFromCandidateDocument,
87
+ candidateIdentifierField,
88
+ getSummarisableSubstringIndices
89
+ }?: AICompareCandidates.CompareArguments<Candidate>): Promise<AICompareCandidates.CompareCandidatesReturn<Candidate> | void>;
90
+ }
91
+ declare namespace AICompareCandidates {
92
+ interface LoadArguments {
93
+ progressCallback?: ProgressCallback;
94
+ modelName: string;
95
+ }
96
+ interface SummarisableSubstringIndices {
97
+ start: number;
98
+ end: number;
99
+ }
100
+ interface CompareArguments<Candidate> {
101
+ candidates: Candidate[];
102
+ problemDescription: string;
103
+ generateSearchAreasInstruction?: (problemDescription: string) => string;
104
+ convertCandidateToDocument?: (convertCandidateToDocumentArguments: ConvertCandidateToDocumentArguments<Candidate>) => string;
105
+ candidatesForInitialSelection?: number;
106
+ candidatesForFinalSelection?: number;
107
+ generateRankingInstruction?: (generateRankingInstructionArguments: GenerateRankingInstructionArguments) => string;
108
+ extractIdentifiersFromRationale?: (rationale: string) => string[];
109
+ extractIdentifierFromCandidateDocument?: (extractIdentifierFromCandidateDocumentArguments: ExtractIdentifierFromCandidateDocumentArguments) => string;
110
+ candidateIdentifierField?: keyof Candidate;
111
+ getSummarisableSubstringIndices?: (candidateDocument: string) => SummarisableSubstringIndices;
112
+ }
113
+ interface ConvertCandidateToDocumentArguments<Candidate> {
114
+ candidate: Candidate;
115
+ index: number;
116
+ }
117
+ interface ExtractIdentifierFromCandidateDocumentArguments {
118
+ candidateDocument: string;
119
+ candidateIdentifierField: string;
120
+ }
121
+ interface GenerateRankingInstructionArguments {
122
+ problemDescription: string;
123
+ summaries: string[];
124
+ candidatesForFinalSelection: number;
125
+ candidateIdentifierField: string;
126
+ }
127
+ interface CompareCandidatesReturn<Candidate> {
128
+ selectedCandidates: Candidate[];
129
+ rationale: string;
130
+ }
131
+ }
132
+ //#endregion
133
+ export { AICompareCandidates, AICompareCandidates as default };
134
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;cAiBa,mBAAA,SAA4B,UAAA;gBAAR,oCAAA,CACpB;;EADA,SAAA,EAIF,sBAJsB,G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ACA,UAAA,mCAAA,CAAA,SAAA,CAAA,CAAA;IACA,SAAA,EAyLU,SAzLV;IACA,KAAA,EAAA,MAAA;EACsC;EAArC,UAAA,+CAAoB,CAAA;IAAqI,iBAAA,EAAA,MAAA;IAA5C,wBAAoB,EAAA,MAAA;EAA5B;EA/P/D,UAAA,mCAAA,CAAA;IAAU,kBAAA,EAAA,MAAA;IA4ZlC,SAAA,EAAA,MAAA,EAAmB;IAEhB,2BAAA,EAAA,MAAA;IAUP,wBAAA,EAAA,MAAA;EAG0F;EAApC,UAAA,uBAAA,CAAA,SAAA,CAAA,CAAA;IAGA,kBAAA,EAyB9C,SAzB8C,EAAA;IAEwB,SAAA,EAAA,MAAA;EACzD"}
@@ -0,0 +1,134 @@
1
+ import { FeatureExtractionPipeline, PreTrainedTokenizer, ProgressCallback, ProgressInfo, SummarizationPipeline, TextGenerationPipeline } from "@huggingface/transformers";
2
+ import { Embeddings } from "@langchain/core/embeddings";
3
+ import * as _huggingface_transformers_types_env0 from "@huggingface/transformers/types/env";
4
+
5
+ //#region src/index.d.ts
6
+ declare class AICompareCandidates extends Embeddings {
7
+ readonly env: _huggingface_transformers_types_env0.TransformersEnvironment;
8
+ DEBUG: boolean;
9
+ generator: TextGenerationPipeline | null;
10
+ generatorModelName: string;
11
+ generatorPromise: Promise<TextGenerationPipeline> | null;
12
+ generatorProgressInfo: ProgressInfo;
13
+ generatorProgressCallback: ProgressCallback | null;
14
+ summariser: SummarizationPipeline | null;
15
+ summariserModelName: string;
16
+ summariserPromise: Promise<SummarizationPipeline> | null;
17
+ summariserProgressInfo: ProgressInfo;
18
+ summariserProgressCallback: ProgressCallback | null;
19
+ embedder: FeatureExtractionPipeline | null;
20
+ embedderModelName: string;
21
+ embedderPromise: Promise<FeatureExtractionPipeline> | null;
22
+ embedderProgressInfo: ProgressInfo;
23
+ embedderProgressCallback: ProgressCallback | null;
24
+ tokeniser: PreTrainedTokenizer | null;
25
+ tokeniserModelName: string;
26
+ tokeniserPromise: Promise<PreTrainedTokenizer> | null;
27
+ tokeniserProgressInfo: ProgressInfo;
28
+ tokeniserProgressCallback: ProgressCallback | null;
29
+ generateSearchAreasMaxNewTokens: number;
30
+ generateSearchAreasTemperature: number;
31
+ generateSearchAreasRepetitionPenalty: number;
32
+ rankingMaxNewTokens: number;
33
+ rankingTemperature: number;
34
+ rankingRepetitionPenalty: number;
35
+ targetSummarisedStringTokenCount: number;
36
+ constructor();
37
+ loadGenerator({
38
+ progressCallback,
39
+ modelName
40
+ }?: AICompareCandidates.LoadArguments): Promise<TextGenerationPipeline>;
41
+ checkGeneratorLoaded(): Promise<void>;
42
+ loadSummariser({
43
+ progressCallback,
44
+ modelName
45
+ }?: AICompareCandidates.LoadArguments): Promise<SummarizationPipeline>;
46
+ checkSummariserLoaded(): Promise<void>;
47
+ loadEmbedder({
48
+ progressCallback,
49
+ modelName
50
+ }?: AICompareCandidates.LoadArguments): Promise<FeatureExtractionPipeline>;
51
+ checkEmbedderLoaded(): Promise<void>;
52
+ loadTokeniser({
53
+ progressCallback,
54
+ modelName
55
+ }?: AICompareCandidates.LoadArguments): Promise<PreTrainedTokenizer>;
56
+ checkTokeniserLoaded(): Promise<void>;
57
+ embedQuery(text: string): Promise<number[]>;
58
+ embedDocuments(texts: string[]): Promise<number[][]>;
59
+ generatePromptTemplate(prompt: string): string;
60
+ defaultGenerateSearchAreasInstruction(problemDescription: string): string;
61
+ defaultConvertCandidateToDocument<Candidate>({
62
+ candidate,
63
+ index
64
+ }?: AICompareCandidates.ConvertCandidateToDocumentArguments<Candidate>): string;
65
+ defaultGenerateRankingInstruction({
66
+ problemDescription,
67
+ summaries,
68
+ candidatesForFinalSelection,
69
+ candidateIdentifierField
70
+ }?: AICompareCandidates.GenerateRankingInstructionArguments): string;
71
+ regexIndexOf(text: string, regex: RegExp, startIndex: number): number;
72
+ defaultExtractIdentifierFromCandidateDocument({
73
+ candidateDocument,
74
+ candidateIdentifierField
75
+ }?: AICompareCandidates.ExtractIdentifierFromCandidateDocumentArguments): string;
76
+ defaultExtractIdentifiersFromRationale(rationale: string): string[];
77
+ compareCandidates<Candidate>({
78
+ candidates,
79
+ problemDescription,
80
+ generateSearchAreasInstruction,
81
+ convertCandidateToDocument,
82
+ candidatesForInitialSelection,
83
+ candidatesForFinalSelection,
84
+ generateRankingInstruction,
85
+ extractIdentifiersFromRationale,
86
+ extractIdentifierFromCandidateDocument,
87
+ candidateIdentifierField,
88
+ getSummarisableSubstringIndices
89
+ }?: AICompareCandidates.CompareArguments<Candidate>): Promise<AICompareCandidates.CompareCandidatesReturn<Candidate> | void>;
90
+ }
91
+ declare namespace AICompareCandidates {
92
+ interface LoadArguments {
93
+ progressCallback?: ProgressCallback;
94
+ modelName: string;
95
+ }
96
+ interface SummarisableSubstringIndices {
97
+ start: number;
98
+ end: number;
99
+ }
100
+ interface CompareArguments<Candidate> {
101
+ candidates: Candidate[];
102
+ problemDescription: string;
103
+ generateSearchAreasInstruction?: (problemDescription: string) => string;
104
+ convertCandidateToDocument?: (convertCandidateToDocumentArguments: ConvertCandidateToDocumentArguments<Candidate>) => string;
105
+ candidatesForInitialSelection?: number;
106
+ candidatesForFinalSelection?: number;
107
+ generateRankingInstruction?: (generateRankingInstructionArguments: GenerateRankingInstructionArguments) => string;
108
+ extractIdentifiersFromRationale?: (rationale: string) => string[];
109
+ extractIdentifierFromCandidateDocument?: (extractIdentifierFromCandidateDocumentArguments: ExtractIdentifierFromCandidateDocumentArguments) => string;
110
+ candidateIdentifierField?: keyof Candidate;
111
+ getSummarisableSubstringIndices?: (candidateDocument: string) => SummarisableSubstringIndices;
112
+ }
113
+ interface ConvertCandidateToDocumentArguments<Candidate> {
114
+ candidate: Candidate;
115
+ index: number;
116
+ }
117
+ interface ExtractIdentifierFromCandidateDocumentArguments {
118
+ candidateDocument: string;
119
+ candidateIdentifierField: string;
120
+ }
121
+ interface GenerateRankingInstructionArguments {
122
+ problemDescription: string;
123
+ summaries: string[];
124
+ candidatesForFinalSelection: number;
125
+ candidateIdentifierField: string;
126
+ }
127
+ interface CompareCandidatesReturn<Candidate> {
128
+ selectedCandidates: Candidate[];
129
+ rationale: string;
130
+ }
131
+ }
132
+ //#endregion
133
+ export { AICompareCandidates, AICompareCandidates as default };
134
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;cAiBa,mBAAA,SAA4B,UAAA;gBAAR,oCAAA,CACpB;;EADA,SAAA,EAIF,sBAJsB,GAAA,IAAA;EAAA,kBAAA,EAAA,MAAA;EAItB,gBAAA,EAEO,OAFP,CAEe,sBAFf,CAAA,GAAA,IAAA;EAEe,qBAAA,EACH,YADG;EAAR,yBAAA,EAES,gBAFT,GAAA,IAAA;EACK,UAAA,EAGX,qBAHW,GAAA,IAAA;EACI,mBAAA,EAAA,MAAA;EAEf,iBAAA,EAEO,OAFP,CAEe,qBAFf,CAAA,GAAA,IAAA;EAEe,sBAAA,EACH,YADG;EAAR,0BAAA,EAES,gBAFT,GAAA,IAAA;EACK,QAAA,EAGd,yBAHc,GAAA,IAAA;EACI,iBAAA,EAAA,MAAA;EAElB,eAAA,EAEO,OAFP,CAEe,yBAFf,CAAA,GAAA,IAAA;EAEe,oBAAA,EACH,YADG;EAAR,wBAAA,EAES,gBAFT,GAAA,IAAA;EACK,SAAA,EAGX,mBAHW,GAAA,IAAA;EACI,kBAAA,EAAA,MAAA;EAEf,gBAAA,EAEO,OAFP,CAEe,mBAFf,CAAA,GAAA,IAAA;EAEe,qBAAA,EACH,YADG;EAAR,yBAAA,EAES,gBAFT,GAAA,IAAA;EACK,+BAAA,EAAA,MAAA;EACI,8BAAA,EAAA,MAAA;EAuBzB,oCAAA,EAAA,MAAA;EACA,mBAAA,EAAA,MAAA;EACC,kBAAoB,EAAA,MAAA;EAAmD,wBAAA,EAAA,MAAA;EAAA,gCAAA,EAAA,MAAA;EAkB/C,WAAA,CAAA;EAOzB,aAAA,CA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A,0BAAA,CAAA,EAAA,CAAA,mCAAA,EAwLiE,mCAxLjE,EAAA,GAAA,MAAA;IACA,+BAAA,CAAA,EAAA,CAAA,SAAA,EAAA,MAAA,EAAA,GAAA,MAAA,EAAA;IACA,sCAAA,CAAA,EAAA,CAAA,+CAAA,EAwLyF,+CAxLzF,EAAA,GAAA,MAAA;IACA,wBAAA,CAAA,EAAA,MAwLgC,SAxLhC;IACA,+BAAA,CAAA,EAAA,CAAA,iBAAA,EAAA,MAAA,EAAA,GAwL6D,4BAxL7D;EACA;EACA,UAAA,mCAAA,CAAA,SAAA,CAAA,CAAA;IACA,SAAA,EAyLU,SAzLV;IACA,KAAA,EAAA,MAAA;EACsC;EAArC,UAAA,+CAAoB,CAAA;IAAqI,iBAAA,EAAA,MAAA;IAA5C,wBAAoB,EAAA,MAAA;EAA5B;EA/P/D,UAAA,mCAAA,CAAA;IAAU,kBAAA,EAAA,MAAA;IA4ZlC,SAAA,EAAA,MAAA,EAAmB;IAEhB,2BAAA,EAAA,MAAA;IAUP,wBAAA,EAAA,MAAA;EAG0F;EAApC,UAAA,uBAAA,CAAA,SAAA,CAAA,CAAA;IAGA,kBAAA,EAyB9C,SAzB8C,EAAA;IAEwB,SAAA,EAAA,MAAA;EACzD"}
package/dist/index.mjs ADDED
@@ -0,0 +1,24 @@
1
+ import{AutoTokenizer as e,env as t,pipeline as n}from"@huggingface/transformers";import{MemoryVectorStore as r}from"@langchain/classic/vectorstores/memory";import{Embeddings as i}from"@langchain/core/embeddings";import a from"lodash";import o from"jsan";var s=class extends i{env=t;DEBUG=!0;generator=null;generatorModelName=`Xenova/LaMini-GPT-774M`;generatorPromise=null;generatorProgressInfo={};generatorProgressCallback=null;summariser=null;summariserModelName=`Xenova/distilbart-cnn-12-6`;summariserPromise=null;summariserProgressInfo={};summariserProgressCallback=null;embedder=null;embedderModelName=`Xenova/all-MiniLM-L12-v2`;embedderPromise=null;embedderProgressInfo={};embedderProgressCallback=null;tokeniser=null;tokeniserModelName=this.generatorModelName;tokeniserPromise=null;tokeniserProgressInfo={};tokeniserProgressCallback=null;generateSearchAreasMaxNewTokens=64;generateSearchAreasTemperature=.35;generateSearchAreasRepetitionPenalty=1.5;rankingMaxNewTokens=64;rankingTemperature=.35;rankingRepetitionPenalty=1.5;targetSummarisedStringTokenCount=420;static{t.localModelPath=``,t.allowRemoteModels=!0,t.allowLocalModels=!1}constructor(){super({})}async loadGenerator({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.generatorModelName=t),!this.generatorModelName)throw Error(`Invalid generator model name`);return e&&(this.generatorProgressCallback=e),this.generatorPromise=n(`text-generation`,this.generatorModelName,{device:`webgpu`,progress_callback:e=>(this.DEBUG&&console.log(o.stringify(e)),Object.assign(this.generatorProgressInfo,e),this.generatorProgressCallback?.(e))}),this.generator=await this.generatorPromise,this.generator}async checkGeneratorLoaded(){if(this.generatorPromise||this.loadGenerator(),this.generator||await this.generatorPromise,!this.generator)throw Error(`Unable to load generator`)}async loadSummariser({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.summariserModelName=t),!this.summariserModelName)throw Error(`Invalid summariser model name`);return e&&(this.summariserProgressCallback=e),this.summariserPromise=n(`summarization`,this.summariserModelName,{device:`webgpu`,progress_callback:e=>(this.DEBUG&&console.log(o.stringify(e)),Object.assign(this.summariserProgressInfo,e),this.summariserProgressCallback?.(e))}),this.summariser=await this.summariserPromise,this.summariser}async checkSummariserLoaded(){if(this.summariserPromise||this.loadSummariser(),this.summariser||await this.summariserPromise,!this.summariser)throw Error(`Unable to load summariser`)}async loadEmbedder({progressCallback:e,modelName:t=``}={}){if(typeof t==`string`&&t&&(this.embedderModelName=t),!this.embedderModelName)throw Error(`Invalid embedder model name`);return e&&(this.embedderProgressCallback=e),this.embedderPromise=n(`feature-extraction`,this.embedderModelName,{device:`webgpu`,progress_callback:e=>(this.DEBUG&&console.log(o.stringify(e)),Object.assign(this.embedderProgressInfo,e),this.embedderProgressCallback?.(e))}),this.embedder=await this.embedderPromise,this.embedder}async checkEmbedderLoaded(){if(this.embedderPromise||this.loadEmbedder(),this.embedder||await this.embedderPromise,!this.embedder)throw Error(`Unable to load embedder`)}async loadTokeniser({progressCallback:t,modelName:n=``}={}){if(typeof n==`string`&&n&&(this.tokeniserModelName=n),!this.tokeniserModelName)throw Error(`Invalid tokeniser model name`);return t&&(this.tokeniserProgressCallback=t),this.tokeniserPromise=e.from_pretrained(this.tokeniserModelName,{progress_callback:e=>(this.DEBUG&&console.log(o.stringify(e)),Object.assign(this.tokeniserProgressInfo,e),this.tokeniserProgressCallback?.(e))}),this.tokeniser=await this.tokeniserPromise,this.tokeniser}async checkTokeniserLoaded(){if(this.tokeniserPromise||this.loadTokeniser(),this.tokeniser||await this.tokeniserPromise,!this.tokeniser)throw Error(`Unable to load tokeniser`)}async embedQuery(e){return await this.checkEmbedderLoaded(),Array.from((await this.embedder?.(e,{pooling:`mean`,normalize:!0}))?.data)}async embedDocuments(e){return Promise.all(e.map(e=>this.embedQuery(e)))}generatePromptTemplate(e){return`Below is an instruction that describes a task. Write a response that appropriately completes the request.
2
+
3
+ ### Instruction:
4
+ `+e+`
5
+
6
+ ### Response:`}defaultGenerateSearchAreasInstruction(e){return`List the relevant subject areas for the following issues. Limit your response to 100 words.
7
+ Issues: "`+e+`"`}defaultConvertCandidateToDocument({candidate:e,index:t}={}){let n=`Start of Candidate #`+t;for(let t in e)n+=`
8
+ `+a.startCase(t)+`: `+(typeof e[t]==`object`?o.stringify(e[t]):String(e[t]));return n+=`
9
+ End of Candidate #`+t,n}defaultGenerateRankingInstruction({problemDescription:e,summaries:t,candidatesForFinalSelection:n,candidateIdentifierField:r}={}){return`Strictly follow these rules:
10
+ 1. Rank ONLY the top `+n+` candidates with one 15-word sentence explaining why
11
+ 2. Rank the candidates based on "`+e.replace(/(\r|\n)/g,` `)+`"
12
+ 3. If unclear, say "Insufficient information to determine"
13
+
14
+ Candidates:
15
+
16
+ `+t.join(`
17
+
18
+ `)+`
19
+
20
+ Format exactly:
21
+ #1. "Full `+a.startCase(r)+`": 15-word explanation
22
+ #2. ...`}regexIndexOf(e,t,n){let r=e.slice(n).search(t);return r<0?r:r+n}defaultExtractIdentifierFromCandidateDocument({candidateDocument:e,candidateIdentifierField:t}={}){this.DEBUG&&console.log(e,t);let n=a.startCase(t),r=e.indexOf(n);if(r<0&&(r=e.toLowerCase().indexOf(n.toLowerCase())),this.DEBUG&&console.log(r),r>=0&&(r+=n.length),r<0&&(r=e.toLowerCase().indexOf(t.toLowerCase()),r>=0&&(r+=t.length)),this.DEBUG)console.log(r);else return``;if(r=e.indexOf(`:`,r),this.DEBUG&&console.log(r),r<0&&(r=this.regexIndexOf(e,/\s+/,r)),this.DEBUG&&console.log(r),r<0)return``;let i=e.indexOf(`
23
+ `,r);return i<0&&(i=e.length),this.DEBUG&&console.log(i),e.substring(r,i).trim()}defaultExtractIdentifiersFromRationale(e){let t=/^\s*#\s*\d+\s*\.?\s*"([^"]+)"/gm,n=[];for(let r;Array.isArray(r=t.exec(e));)r[1]&&n.push(r[1]);return n}async compareCandidates({candidates:e,problemDescription:t=``,generateSearchAreasInstruction:n=this.defaultGenerateSearchAreasInstruction.bind(this),convertCandidateToDocument:i=this.defaultConvertCandidateToDocument.bind(this),candidatesForInitialSelection:o=2,candidatesForFinalSelection:s=1,generateRankingInstruction:c=this.defaultGenerateRankingInstruction.bind(this),extractIdentifiersFromRationale:l=this.defaultExtractIdentifiersFromRationale.bind(this),extractIdentifierFromCandidateDocument:u=this.defaultExtractIdentifierFromCandidateDocument.bind(this),candidateIdentifierField:d=void 0,getSummarisableSubstringIndices:f}={}){if(!Array.isArray(e)||e.length<=0)throw Error(`No candidates provided`);if(o=a.toSafeInteger(o),o<=0||(s=a.toSafeInteger(s),s<=0))throw Error(`Candidates for initial selection must be a positive integer bigger than 0`);if(o<s)throw Error(`Candidates for initial selection must be equal or more than candidates for final selection`);if(o>e.length)throw Error(`There are `+o+`candidates for initial selection which is more than the total number of candidates of `+e.length);if(s>e.length)throw Error(`There are `+s+`candidates for initial selection which is more than the total number of candidates of `+e.length);if(!d&&(d=Object.keys(e[0])[0],!d))throw Error(`No candidate identifier field`);let p=``,m=[];if(await this.checkEmbedderLoaded(),!this.embedder)return;let h=e.map((e,t)=>i({candidate:e,index:t})),g=await r.fromTexts(a.cloneDeep(h),h.map((e,t)=>t),this),_=this.generatePromptTemplate(n(t));if(this.DEBUG&&console.log(`Formatted search areas prompt: `+_),await this.checkTokeniserLoaded(),!this.tokeniser)return;if(this.tokeniser.encode(_).length>this.tokeniser.model_max_length)throw Error(`Search areas instruction prompt is too long for the tokeniser model`);if(await this.checkGeneratorLoaded(),!this.generator)return;let v=this.tokeniser.pad_token_id??this.tokeniser.sep_token_id??0,y=this.tokeniser.sep_token_id??2,b=await this.generator(_,{max_new_tokens:this.generateSearchAreasMaxNewTokens,temperature:this.generateSearchAreasTemperature,repetition_penalty:this.generateSearchAreasRepetitionPenalty,pad_token_id:v,eos_token_id:y}),x=Array.isArray(b?.[0])?b?.[0]?.[0]:b?.[0];if(!x.generated_text)throw Error(`No generated text for search areas`);this.DEBUG&&console.log(`Generated search areas response: `+x.generated_text);let S=x.generated_text.toString().indexOf(`### Response:`);S>=0?S+=13:S=0;let C=x.generated_text.toString().substring(S).trim();this.DEBUG&&console.log(`Vector search query: `+C);let w=await g.similaritySearch(C,o);this.DEBUG&&console.log(`Vector search results: `,w);let T=[];if(w.some(e=>e.pageContent.trim().split(/\s+/).length>this.targetSummarisedStringTokenCount)){if(await this.checkSummariserLoaded(),!this.summariser)return;T=(await Promise.allSettled(w.map(async e=>{if(!e.pageContent||typeof e.pageContent!=`string`)return``;if(e.pageContent.trim().split(/\s+/).length<=this.targetSummarisedStringTokenCount)return e.pageContent;let t={start:0,end:e.pageContent.length};f&&Object.assign(t,f(e.pageContent)),t.start=a.clamp(a.toSafeInteger(t.start),0,e.pageContent.length),t.end=a.clamp(a.toSafeInteger(t.end),0,e.pageContent.length);let n=e.pageContent.substring(t.start,t.end),r=e.pageContent.substring(0,t.start),i=e.pageContent.substring(t.end),o=r.split(/s+/).length+i.split(/s+/).length,s=Math.max(1,420-o),c=await this.summariser?.(n,{max_length:s}),l=r+((Array.isArray(c?.[0])?c?.[0]?.[0]:c?.[0])?.summary_text??``).split(/s+/).slice(s).join(` `)+i;return this.DEBUG&&console.log(`Summarised candidate: `+l),l}))).filter(e=>e.status===`fulfilled`&&e.value).map(e=>e.value)}else T=w.map(e=>e.pageContent);let E=this.generatePromptTemplate(c({problemDescription:t,summaries:T,candidatesForFinalSelection:s,candidateIdentifierField:String(d)}));if(this.DEBUG&&console.log(`Formatted ranking prompt: `+E),this.tokeniser.encode(E).length>this.tokeniser.model_max_length)throw Error(`Ranking instruction prompt is too long for the tokeniser model`);let D=await this.generator(E,{max_new_tokens:this.rankingMaxNewTokens,temperature:this.rankingTemperature,repetition_penalty:this.rankingRepetitionPenalty,pad_token_id:v,eos_token_id:y});p=(Array.isArray(D?.[0])?D?.[0]?.[0]:D[0]).generated_text.toString().trim().replace(/(\*\*)|(<\/?s>)|(\[.*?\])\s*/g,``),this.DEBUG&&console.log(`Generated rationale: `+p);let O=p.indexOf(`### Response:`);if(O>=0?O+=13:O=0,p=p.substring(O),p){let t=l(p);t.length>s&&(t=t.slice(0,s)),m=a.compact(t.map(t=>{let n=e.find(e=>String(e[d]).toLowerCase()===t.toLowerCase());return n||(n=e.find(e=>String(e[d]).toLowerCase().includes(t.toLowerCase())),n)||(n=e.find(e=>t.toLowerCase().includes(String(e[d]).toLowerCase())),n)?n:null}))}return(!Array.isArray(m)||m.length<=0)&&(m=a.uniq(a.compact(w.map(t=>{let n=u({candidateDocument:t.pageContent,candidateIdentifierField:String(d)});this.DEBUG&&console.log(`Extracted identifier from candidate document: `+n);let r=e.find(e=>String(e[d]).toLowerCase()===n.toLowerCase());return r||(r=e.find(e=>String(e[d]).toLowerCase().includes(n.toLowerCase())),r)||(r=e.find(e=>n.toLowerCase().includes(String(e[d]).toLowerCase())),r)?r:null}))).slice(s)),this.DEBUG&&console.log(`Selected candidates`,m),{rationale:p,selectedCandidates:m}}};(function(e){})(s||={});var c=s;export{s as AICompareCandidates,c as default};
24
+ //# sourceMappingURL=index.mjs.map