ext_embeddings.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const EMB_REGEX = /<(?!l:|h:)[^,> ]*>?/g;
  2. const EMB_TRIGGER = () => CFG.useEmbeddings && tagword.match(EMB_REGEX);
  3. class EmbeddingParser extends BaseTagParser {
  4. parse() {
  5. // Show embeddings
  6. let tempResults = [];
  7. if (tagword !== "<" && tagword !== "<e:") {
  8. let searchTerm = tagword.replace("<e:", "").replace("<", "");
  9. let versionString;
  10. if (searchTerm.startsWith("v1") || searchTerm.startsWith("v2")) {
  11. versionString = searchTerm.slice(0, 2);
  12. searchTerm = searchTerm.slice(2);
  13. }
  14. let filterCondition = x => x[0].toLowerCase().includes(searchTerm) || x[0].toLowerCase().replaceAll(" ", "_").includes(searchTerm);
  15. if (versionString)
  16. tempResults = embeddings.filter(x => filterCondition(x) && x[1] && x[1] === versionString); // Filter by tagword
  17. else
  18. tempResults = embeddings.filter(x => filterCondition(x)); // Filter by tagword
  19. } else {
  20. tempResults = embeddings;
  21. }
  22. // Add final results
  23. let finalResults = [];
  24. tempResults.forEach(t => {
  25. let result = new AutocompleteResult(t[0].trim(), ResultType.embedding)
  26. result.meta = t[1] + " Embedding";
  27. finalResults.push(result);
  28. });
  29. return finalResults;
  30. }
  31. }
  32. async function load() {
  33. if (embeddings.length === 0) {
  34. try {
  35. embeddings = (await readFile(`${tagBasePath}/temp/emb.txt`)).split("\n")
  36. .filter(x => x.trim().length > 0) // Remove empty lines
  37. .map(x => x.trim().split(",")); // Split into name, version type pairs
  38. } catch (e) {
  39. console.error("Error loading embeddings.txt: " + e);
  40. }
  41. }
  42. }
  43. function sanitize(tagType, text) {
  44. if (tagType === ResultType.embedding) {
  45. return text.replace(/^.*?: /g, "");
  46. }
  47. return null;
  48. }
  49. PARSERS.push(new EmbeddingParser(EMB_TRIGGER));
  50. // Add our utility functions to their respective queues
  51. QUEUE_FILE_LOAD.push(load);
  52. QUEUE_SANITIZE.push(sanitize);