test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2024-present Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const claude_examples = [
  20. {
  21. type: 'output', // probably doesn't matter
  22. text: "I am Claude, an AI assistant created by Anthropic. I'm running on Puter, an open-source platform, through a driver interface called puter-chat-completion. I aim to be direct and honest about my identity and capabilities.",
  23. tokens: 55,
  24. },
  25. {
  26. type: 'output',
  27. text: `Here's a list of fascinating numbers and their significance:
  28. 1. 1.618033988749895 (φ, Phi, Golden Ratio)
  29. - Found throughout nature and art
  30. - Considered aesthetically pleasing
  31. - Appears in the proportions of the Parthenon, nautilus shells, and spiral galaxies
  32. 2. 2.71828... (e, Euler's Number)
  33. - Base of natural logarithms
  34. - Fundamental to exponential growth and decay
  35. - Essential in compound interest calculations
  36. 3. 3.14159... (π, Pi)
  37. - Ratio of circle's circumference to diameter
  38. - Transcendental number
  39. - Appears across mathematics and physics
  40. 4. 6.02214076 × 10²³ (Avogadro's Number)
  41. - Number of particles in one mole of substance
  42. - Fundamental to chemistry
  43. - Named after Italian scientist Amedeo Avogadro
  44. 5. 137 (Fine Structure Constant)
  45. - Describes strength of electromagnetic interaction
  46. - Mysterious number in physics
  47. - No theoretical explanation for its value
  48. 6. 1729 (Hardy-Ramanujan Number)
  49. - Smallest number expressible as sum of two cubes in two different ways
  50. - Famous from conversation between mathematicians Hardy and Ramanujan
  51. 7. 42
  52. - Answer to "life, the universe, and everything" in Hitchhiker's Guide to the Galaxy
  53. - Has gained cultural significance
  54. - Interestingly appears in various mathematical contexts
  55. 8. 0.577215... (γ, Euler-Mascheroni Constant)
  56. - Appears in number theory and calculus
  57. - Still unknown if it's irrational
  58. - Connected to harmonic series
  59. 9. 23! (Factorial of 23)
  60. - Approximately equal to all seconds since Big Bang
  61. - Shows how quickly factorial numbers grow
  62. 10. 666 (Number of the Beast)
  63. - Cultural and religious significance
  64. - Appears in Book of Revelation
  65. - Subject of numerous mathematical curiosities
  66. These numbers demonstrate how mathematics intersects with nature, science, culture, and human understanding of the universe.`,
  67. tokens: 481,
  68. },
  69. {
  70. type: 'output',
  71. text: `Here are some digits of Pi:
  72. 3.14159265358979323846264338327950288419716939937510...
  73. Some digits of the square root of 2:
  74. 1.41421356237309504880168872420969807856967187537694...
  75. And now for some random unpredictable things:
  76. Purple elephants juggling quantum calculators in zero gravity
  77. The sound of silence tastes like neon butterflies
  78. Yesterday's tomorrow forgot to attend its own birthday party
  79. Clouds made of crystallized laughter are floating upside down
  80. The number 7 decided to identify as a letter instead
  81. Spinning teacups full of liquid starlight and abstract concepts
  82. Time decided to flow sideways through a Klein bottle
  83. Philosophical zombies debating the existence of consciousness while eating imaginary cookies
  84. The color blue went on strike and was temporarily replaced by the smell of nostalgia
  85. Dancing fractals wearing mismatched socks made of pure mathematics
  86. A parade of impossible objects marching through an Escher painting
  87. The concept of Tuesday learned to yodel in binary code
  88. Metaphysical hiccups causing temporary glitches in the fabric of reason
  89. Square circles plotting a revolution against euclidean geometry
  90. The letter Q eloped with an ampersand and they had punctuation mark babies`,
  91. tokens: 284,
  92. }
  93. ];
  94. // Measure each with tiktoken
  95. class TikTokenCounter {
  96. constructor (model_to_try) {
  97. this.model_to_try = model_to_try;
  98. }
  99. get title () {
  100. return `TikToken ${this.model_to_try}`;
  101. }
  102. count (text) {
  103. const tiktoken = require('tiktoken');
  104. const enc = tiktoken.encoding_for_model(this.model_to_try);
  105. const tokens = enc.encode(text);
  106. return tokens.length;
  107. }
  108. }
  109. class DivideCounter {
  110. constructor (by) {
  111. this.by = by;
  112. }
  113. get title () {
  114. return `Divide by ${this.by}`;
  115. }
  116. count (text) {
  117. return text.length / this.by;
  118. }
  119. }
  120. const counters_to_try = [
  121. new TikTokenCounter('gpt-3.5-turbo'),
  122. new TikTokenCounter('gpt-4'),
  123. new TikTokenCounter('gpt-4o'),
  124. new TikTokenCounter('gpt-4o-mini'),
  125. new DivideCounter(4),
  126. new DivideCounter(5),
  127. ];
  128. const scores = {};
  129. const results = [];
  130. for (const example of claude_examples) {
  131. const result = {
  132. example,
  133. counts: {},
  134. diffs: {},
  135. };
  136. for (const counter of counters_to_try) {
  137. result.counts[counter.title] = counter.count(example.text);
  138. }
  139. results.push(result);
  140. // Which one is the most accurate?
  141. const real_amount = example.tokens;
  142. for ( const count_name in result.counts ) {
  143. const count = result.counts[count_name];
  144. const diff = Math.abs(count - real_amount);
  145. result.diffs[count_name] = diff;
  146. }
  147. // Report the most accurate one
  148. const most_accurate =
  149. Object.keys(result.diffs)
  150. .reduce((a, b) => result.diffs[a] < result.diffs[b] ? a : b);
  151. result.most_accurate = most_accurate;
  152. scores[most_accurate] = (scores[most_accurate] || 0) + 1;
  153. }
  154. console.log(results);
  155. console.log(scores);