cactus-react-native 0.0.1 → 0.1.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 (189) hide show
  1. package/LICENSE.txt +20 -0
  2. package/README.md +3 -1
  3. package/android/src/main/CMakeLists.txt +58 -23
  4. package/android/src/main/java/com/cactus/Cactus.java +484 -16
  5. package/android/src/main/java/com/cactus/LlamaContext.java +199 -0
  6. package/android/src/main/jni.cpp +325 -10
  7. package/android/src/main/jniLibs/arm64-v8a/libcactus.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/libcactus_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/libcactus.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/libcactus_x86_64.so +0 -0
  15. package/android/src/newarch/java/com/cactus/CactusModule.java +79 -7
  16. package/android/src/oldarch/java/com/cactus/CactusModule.java +70 -0
  17. package/cactus-react-native.podspec +0 -3
  18. package/ios/CMakeLists.txt +58 -36
  19. package/ios/Cactus.mm +243 -2
  20. package/ios/CactusContext.h +22 -0
  21. package/ios/CactusContext.mm +176 -1
  22. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +92 -5
  23. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +268 -0
  24. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/chat.h +2 -0
  25. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/common.h +42 -51
  26. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-backend.h +4 -4
  27. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-common.h +12 -6
  28. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpp.h +1 -1
  29. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpu.h +5 -0
  30. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-impl.h +52 -18
  31. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-metal-impl.h +106 -14
  32. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-opt.h +49 -28
  33. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml.h +87 -106
  34. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-arch.h +16 -0
  35. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-batch.h +2 -1
  36. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-chat.h +7 -2
  37. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-context.h +44 -33
  38. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-cparams.h +1 -0
  39. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-graph.h +83 -17
  40. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-hparams.h +44 -2
  41. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-kv-cache.h +407 -179
  42. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-memory.h +13 -2
  43. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-model-loader.h +5 -3
  44. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-model-saver.h +37 -0
  45. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-model.h +24 -2
  46. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama-vocab.h +6 -0
  47. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/llama.h +102 -142
  48. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/minja/chat-template.hpp +23 -11
  49. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/minja/minja.hpp +186 -127
  50. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Info.plist +0 -0
  51. package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
  52. package/ios/cactus.xcframework/ios-arm64/cactus.framework/ggml-llama.metallib +0 -0
  53. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +92 -5
  54. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/cactus_ffi.h +268 -0
  55. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/chat.h +2 -0
  56. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/common.h +42 -51
  57. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-backend.h +4 -4
  58. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-common.h +12 -6
  59. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpp.h +1 -1
  60. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu.h +5 -0
  61. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-impl.h +52 -18
  62. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-metal-impl.h +106 -14
  63. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-opt.h +49 -28
  64. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml.h +87 -106
  65. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-arch.h +16 -0
  66. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-batch.h +2 -1
  67. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-chat.h +7 -2
  68. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-context.h +44 -33
  69. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-cparams.h +1 -0
  70. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-graph.h +83 -17
  71. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-hparams.h +44 -2
  72. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-kv-cache.h +407 -179
  73. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-memory.h +13 -2
  74. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-loader.h +5 -3
  75. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-saver.h +37 -0
  76. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-model.h +24 -2
  77. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama-vocab.h +6 -0
  78. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/llama.h +102 -142
  79. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/minja/chat-template.hpp +23 -11
  80. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/minja/minja.hpp +186 -127
  81. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Info.plist +0 -0
  82. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/_CodeSignature/CodeResources +1 -1
  83. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
  84. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/ggml-llama-sim.metallib +0 -0
  85. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus.h +92 -5
  86. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/cactus_ffi.h +268 -0
  87. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/chat.h +2 -0
  88. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/common.h +42 -51
  89. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-backend.h +4 -4
  90. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-common.h +12 -6
  91. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpp.h +1 -1
  92. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpu.h +5 -0
  93. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-impl.h +52 -18
  94. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-metal-impl.h +106 -14
  95. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-opt.h +49 -28
  96. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml.h +87 -106
  97. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-arch.h +16 -0
  98. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-batch.h +2 -1
  99. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-chat.h +7 -2
  100. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-context.h +44 -33
  101. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-cparams.h +1 -0
  102. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-graph.h +83 -17
  103. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-hparams.h +44 -2
  104. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-kv-cache.h +407 -179
  105. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-memory.h +13 -2
  106. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-model-loader.h +5 -3
  107. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-model-saver.h +37 -0
  108. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-model.h +24 -2
  109. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama-vocab.h +6 -0
  110. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/llama.h +102 -142
  111. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/minja/chat-template.hpp +23 -11
  112. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/minja/minja.hpp +186 -127
  113. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Info.plist +0 -0
  114. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/cactus +0 -0
  115. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/ggml-llama.metallib +0 -0
  116. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus.h +92 -5
  117. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/cactus_ffi.h +268 -0
  118. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/chat.h +2 -0
  119. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/common.h +42 -51
  120. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-backend.h +4 -4
  121. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-common.h +12 -6
  122. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpp.h +1 -1
  123. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu.h +5 -0
  124. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-impl.h +52 -18
  125. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-metal-impl.h +106 -14
  126. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-opt.h +49 -28
  127. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml.h +87 -106
  128. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-arch.h +16 -0
  129. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-batch.h +2 -1
  130. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-chat.h +7 -2
  131. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-context.h +44 -33
  132. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-cparams.h +1 -0
  133. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-graph.h +83 -17
  134. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-hparams.h +44 -2
  135. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-kv-cache.h +407 -179
  136. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-memory.h +13 -2
  137. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-loader.h +5 -3
  138. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-model-saver.h +37 -0
  139. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-model.h +24 -2
  140. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama-vocab.h +6 -0
  141. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/llama.h +102 -142
  142. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/minja/chat-template.hpp +23 -11
  143. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/minja/minja.hpp +186 -127
  144. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Info.plist +0 -0
  145. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/_CodeSignature/CodeResources +1 -1
  146. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/cactus +0 -0
  147. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/ggml-llama-sim.metallib +0 -0
  148. package/lib/commonjs/NativeCactus.js +1 -0
  149. package/lib/commonjs/NativeCactus.js.map +1 -1
  150. package/lib/commonjs/index.js +112 -0
  151. package/lib/commonjs/index.js.map +1 -1
  152. package/lib/commonjs/tools.js +118 -0
  153. package/lib/commonjs/tools.js.map +1 -0
  154. package/lib/module/NativeCactus.js +3 -0
  155. package/lib/module/NativeCactus.js.map +1 -1
  156. package/lib/module/index.js +87 -1
  157. package/lib/module/index.js.map +1 -1
  158. package/lib/module/tools.js +110 -0
  159. package/lib/module/tools.js.map +1 -0
  160. package/lib/typescript/NativeCactus.d.ts +30 -1
  161. package/lib/typescript/NativeCactus.d.ts.map +1 -1
  162. package/lib/typescript/index.d.ts +21 -2
  163. package/lib/typescript/index.d.ts.map +1 -1
  164. package/lib/typescript/tools.d.ts +38 -0
  165. package/lib/typescript/tools.d.ts.map +1 -0
  166. package/package.json +6 -3
  167. package/src/NativeCactus.ts +62 -1
  168. package/src/index.ts +113 -2
  169. package/src/tools.ts +127 -0
  170. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpu-aarch64.h +0 -8
  171. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpu-impl.h +0 -531
  172. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpu-quants.h +0 -63
  173. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/ggml-cpu-traits.h +0 -38
  174. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/sgemm.h +0 -14
  175. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-aarch64.h +0 -8
  176. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-impl.h +0 -531
  177. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-quants.h +0 -63
  178. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-traits.h +0 -38
  179. package/ios/cactus.xcframework/ios-arm64_x86_64-simulator/cactus.framework/Headers/sgemm.h +0 -14
  180. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpu-aarch64.h +0 -8
  181. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpu-impl.h +0 -531
  182. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpu-quants.h +0 -63
  183. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/ggml-cpu-traits.h +0 -38
  184. package/ios/cactus.xcframework/tvos-arm64/cactus.framework/Headers/sgemm.h +0 -14
  185. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-aarch64.h +0 -8
  186. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-impl.h +0 -531
  187. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-quants.h +0 -63
  188. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/ggml-cpu-traits.h +0 -38
  189. package/ios/cactus.xcframework/tvos-arm64_x86_64-simulator/cactus.framework/Headers/sgemm.h +0 -14
package/src/tools.ts ADDED
@@ -0,0 +1,127 @@
1
+ import type { CactusOAICompatibleMessage } from "./chat";
2
+ import type { NativeCompletionResult } from "./NativeCactus";
3
+
4
+ interface Parameter {
5
+ type: string,
6
+ description: string,
7
+ required?: boolean // parameter is optional if not specified
8
+ }
9
+
10
+ interface Tool {
11
+ func: Function,
12
+ description: string,
13
+ parameters: {[key: string]: Parameter},
14
+ required: string[]
15
+ }
16
+
17
+ export class Tools {
18
+ private tools = new Map<string, Tool>();
19
+
20
+ add(
21
+ func: Function,
22
+ description: string,
23
+ parameters: {[key: string]: Parameter},
24
+ ) {
25
+ this.tools.set(func.name, {
26
+ func,
27
+ description,
28
+ parameters,
29
+ required: Object.entries(parameters)
30
+ .filter(([_, param]) => param.required)
31
+ .map(([key, _]) => key)
32
+ });
33
+ return func;
34
+ }
35
+
36
+ getSchemas() {
37
+ return Array.from(this.tools.entries()).map(([name, { description, parameters, required }]) => ({
38
+ type: "function",
39
+ function: {
40
+ name,
41
+ description,
42
+ parameters: {
43
+ type: "object",
44
+ properties: parameters,
45
+ required
46
+ }
47
+ }
48
+ }));
49
+ }
50
+
51
+ async execute(name: string, args: any) {
52
+ const tool = this.tools.get(name);
53
+ if (!tool) throw new Error(`Tool ${name} not found`);
54
+ return await tool.func(...Object.values(args));
55
+ }
56
+ }
57
+
58
+ export function injectToolsIntoMessages(messages: CactusOAICompatibleMessage[], tools: Tools): CactusOAICompatibleMessage[] {
59
+ const newMessages = [...messages];
60
+ const toolsSchemas = tools.getSchemas();
61
+ const promptToolInjection = `You have access to the following functions. Use them if required -
62
+ ${JSON.stringify(toolsSchemas, null, 2)}
63
+ Only use an available tool if needed. If a tool is chosen, respond ONLY with a JSON object matching the following schema:
64
+ \`\`\`json
65
+ {
66
+ "tool_name": "<name of the tool>",
67
+ "tool_input": {
68
+ "<parameter_name>": "<parameter_value>",
69
+ ...
70
+ }
71
+ }
72
+ \`\`\`
73
+ Remember, if you are calling a tool, you must respond with the JSON object and the JSON object ONLY!
74
+ If no tool is needed, respond normally.
75
+ `;
76
+
77
+ const systemMessage = newMessages.find(m => m.role === 'system');
78
+ if (!systemMessage) {
79
+ newMessages.unshift({
80
+ role: 'system',
81
+ content: promptToolInjection
82
+ });
83
+ } else {
84
+ systemMessage.content = `${systemMessage.content}\n\n${promptToolInjection}`;
85
+ }
86
+
87
+ return newMessages;
88
+ }
89
+
90
+ export async function parseAndExecuteTool(result: NativeCompletionResult, tools: Tools): Promise<{toolCalled: boolean, toolName?: string, toolInput?: any, toolOutput?: any}> {
91
+ const match = result.content.match(/```json\s*([\s\S]*?)\s*```/);
92
+
93
+ if (!match || !match[1]) return {toolCalled: false};
94
+
95
+ try {
96
+ const jsonContent = JSON.parse(match[1]);
97
+ const { tool_name, tool_input } = jsonContent;
98
+ // console.log('Calling tool:', tool_name, tool_input);
99
+ const toolOutput = await tools.execute(tool_name, tool_input) || true;
100
+ // console.log('Tool called result:', toolOutput);
101
+
102
+ return {
103
+ toolCalled: true,
104
+ toolName: tool_name,
105
+ toolInput: tool_input,
106
+ toolOutput
107
+ };
108
+ } catch (error) {
109
+ // console.error('Error parsing JSON:', match, error);
110
+ return {toolCalled: false};
111
+ }
112
+ }
113
+
114
+ export function updateMessagesWithToolCall(messages: CactusOAICompatibleMessage[], toolName: string, toolInput: any, toolOutput: any): CactusOAICompatibleMessage[] {
115
+ const newMessages = [...messages];
116
+
117
+ newMessages.push({
118
+ role: 'function-call',
119
+ content: JSON.stringify({name: toolName, arguments: toolInput}, null, 2)
120
+ })
121
+ newMessages.push({
122
+ role: 'function-response',
123
+ content: JSON.stringify(toolOutput, null, 2)
124
+ })
125
+
126
+ return newMessages;
127
+ }
@@ -1,8 +0,0 @@
1
- #pragma once
2
-
3
- #include "ggml-cpu-traits.h"
4
- #include "ggml.h"
5
-
6
- // GGML internal header
7
-
8
- lm_ggml_backend_buffer_type_t lm_ggml_backend_cpu_aarch64_buffer_type(void);
@@ -1,531 +0,0 @@
1
- #pragma once
2
-
3
- // GGML CPU internal header
4
-
5
- #include "ggml.h"
6
- #include "ggml-impl.h"
7
- #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
8
- //#include <stddef.h>
9
- #include <stdbool.h>
10
- #include <string.h> // memcpy
11
- #include <math.h> // fabsf
12
-
13
-
14
- #ifdef __cplusplus
15
- extern "C" {
16
- #endif
17
-
18
- struct lm_ggml_compute_params {
19
- // ith = thread index, nth = number of threads
20
- int ith, nth;
21
-
22
- // work buffer for all threads
23
- size_t wsize;
24
- void * wdata;
25
-
26
- struct lm_ggml_threadpool * threadpool;
27
- };
28
-
29
-
30
- #if defined(_MSC_VER)
31
-
32
- #define m512bh(p) p
33
- #define m512i(p) p
34
-
35
- #else
36
-
37
- #define m512bh(p) (__m512bh)(p)
38
- #define m512i(p) (__m512i)(p)
39
-
40
- #endif
41
-
42
- // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
43
- #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
44
- #ifndef __FMA__
45
- #define __FMA__
46
- #endif
47
- #ifndef __F16C__
48
- #define __F16C__
49
- #endif
50
- #endif
51
-
52
- // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
53
- #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
54
- #ifndef __SSE3__
55
- #define __SSE3__
56
- #endif
57
- #ifndef __SSSE3__
58
- #define __SSSE3__
59
- #endif
60
- #endif
61
-
62
- #if defined(__s390x__) && defined(__VEC__)
63
- #ifndef __VXE__
64
- #define __VXE__
65
- #endif
66
- #ifndef __VXE2__
67
- #define __VXE2__
68
- #endif
69
- #endif
70
-
71
- #if defined(__ARM_FEATURE_SVE)
72
- #include <arm_sve.h>
73
- #include <sys/prctl.h>
74
- #endif
75
-
76
- // 16-bit float
77
- // on Arm, we use __fp16
78
- // on x86, we use uint16_t
79
- #if defined(__ARM_NEON)
80
-
81
- // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
82
- //
83
- // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
84
- //
85
- #include <arm_neon.h>
86
-
87
- #ifdef _MSC_VER
88
-
89
- typedef uint16_t lm_ggml_fp16_internal_t;
90
-
91
- #define lm_ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
92
-
93
- #else
94
-
95
- typedef __fp16 lm_ggml_fp16_internal_t;
96
-
97
- #define lm_ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
98
-
99
- #endif // _MSC_VER
100
-
101
- #if !defined(__aarch64__)
102
-
103
- // 32-bit ARM compatibility
104
-
105
- // vaddlvq_s16
106
- // vpaddq_s16
107
- // vpaddq_s32
108
- // vaddvq_s32
109
- // vaddvq_f32
110
- // vmaxvq_f32
111
- // vcvtnq_s32_f32
112
- // vzip1_u8
113
- // vzip2_u8
114
-
115
- inline static int32_t vaddlvq_s16(int16x8_t v) {
116
- int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
117
- return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
118
- }
119
-
120
- inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
121
- int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
122
- int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
123
- return vcombine_s16(a0, b0);
124
- }
125
-
126
- inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
127
- int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
128
- int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
129
- return vcombine_s32(a0, b0);
130
- }
131
-
132
- inline static int32_t vaddvq_s32(int32x4_t v) {
133
- return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
134
- }
135
-
136
- inline static float vaddvq_f32(float32x4_t v) {
137
- return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
138
- }
139
-
140
- inline static float vmaxvq_f32(float32x4_t v) {
141
- return
142
- MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
143
- MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
144
- }
145
-
146
- inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
147
- int32x4_t res;
148
-
149
- res[0] = roundf(vgetq_lane_f32(v, 0));
150
- res[1] = roundf(vgetq_lane_f32(v, 1));
151
- res[2] = roundf(vgetq_lane_f32(v, 2));
152
- res[3] = roundf(vgetq_lane_f32(v, 3));
153
-
154
- return res;
155
- }
156
-
157
- inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
158
- uint8x8_t res;
159
-
160
- res[0] = a[0]; res[1] = b[0];
161
- res[2] = a[1]; res[3] = b[1];
162
- res[4] = a[2]; res[5] = b[2];
163
- res[6] = a[3]; res[7] = b[3];
164
-
165
- return res;
166
- }
167
-
168
- inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
169
- uint8x8_t res;
170
-
171
- res[0] = a[4]; res[1] = b[4];
172
- res[2] = a[5]; res[3] = b[5];
173
- res[4] = a[6]; res[5] = b[6];
174
- res[6] = a[7]; res[7] = b[7];
175
-
176
- return res;
177
- }
178
-
179
- // vld1q_s16_x2
180
- // vld1q_u8_x2
181
- // vld1q_u8_x4
182
- // vld1q_s8_x2
183
- // vld1q_s8_x4
184
- // TODO: double-check these work correctly
185
-
186
- typedef struct lm_ggml_int16x8x2_t {
187
- int16x8_t val[2];
188
- } lm_ggml_int16x8x2_t;
189
-
190
- inline static lm_ggml_int16x8x2_t lm_ggml_vld1q_s16_x2(const int16_t * ptr) {
191
- lm_ggml_int16x8x2_t res;
192
-
193
- res.val[0] = vld1q_s16(ptr + 0);
194
- res.val[1] = vld1q_s16(ptr + 8);
195
-
196
- return res;
197
- }
198
-
199
- typedef struct lm_ggml_uint8x16x2_t {
200
- uint8x16_t val[2];
201
- } lm_ggml_uint8x16x2_t;
202
-
203
- inline static lm_ggml_uint8x16x2_t lm_ggml_vld1q_u8_x2(const uint8_t * ptr) {
204
- lm_ggml_uint8x16x2_t res;
205
-
206
- res.val[0] = vld1q_u8(ptr + 0);
207
- res.val[1] = vld1q_u8(ptr + 16);
208
-
209
- return res;
210
- }
211
-
212
- typedef struct lm_ggml_uint8x16x4_t {
213
- uint8x16_t val[4];
214
- } lm_ggml_uint8x16x4_t;
215
-
216
- inline static lm_ggml_uint8x16x4_t lm_ggml_vld1q_u8_x4(const uint8_t * ptr) {
217
- lm_ggml_uint8x16x4_t res;
218
-
219
- res.val[0] = vld1q_u8(ptr + 0);
220
- res.val[1] = vld1q_u8(ptr + 16);
221
- res.val[2] = vld1q_u8(ptr + 32);
222
- res.val[3] = vld1q_u8(ptr + 48);
223
-
224
- return res;
225
- }
226
-
227
- typedef struct lm_ggml_int8x16x2_t {
228
- int8x16_t val[2];
229
- } lm_ggml_int8x16x2_t;
230
-
231
- inline static lm_ggml_int8x16x2_t lm_ggml_vld1q_s8_x2(const int8_t * ptr) {
232
- lm_ggml_int8x16x2_t res;
233
-
234
- res.val[0] = vld1q_s8(ptr + 0);
235
- res.val[1] = vld1q_s8(ptr + 16);
236
-
237
- return res;
238
- }
239
-
240
- typedef struct lm_ggml_int8x16x4_t {
241
- int8x16_t val[4];
242
- } lm_ggml_int8x16x4_t;
243
-
244
- inline static lm_ggml_int8x16x4_t lm_ggml_vld1q_s8_x4(const int8_t * ptr) {
245
- lm_ggml_int8x16x4_t res;
246
-
247
- res.val[0] = vld1q_s8(ptr + 0);
248
- res.val[1] = vld1q_s8(ptr + 16);
249
- res.val[2] = vld1q_s8(ptr + 32);
250
- res.val[3] = vld1q_s8(ptr + 48);
251
-
252
- return res;
253
- }
254
-
255
- // NOTE: not tested
256
- inline static int8x16_t lm_ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
257
- int8x16_t res;
258
-
259
- res[ 0] = a[b[ 0]];
260
- res[ 1] = a[b[ 1]];
261
- res[ 2] = a[b[ 2]];
262
- res[ 3] = a[b[ 3]];
263
- res[ 4] = a[b[ 4]];
264
- res[ 5] = a[b[ 5]];
265
- res[ 6] = a[b[ 6]];
266
- res[ 7] = a[b[ 7]];
267
- res[ 8] = a[b[ 8]];
268
- res[ 9] = a[b[ 9]];
269
- res[10] = a[b[10]];
270
- res[11] = a[b[11]];
271
- res[12] = a[b[12]];
272
- res[13] = a[b[13]];
273
- res[14] = a[b[14]];
274
- res[15] = a[b[15]];
275
-
276
- return res;
277
- }
278
-
279
- // NOTE: not tested
280
- inline static uint8x16_t lm_ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
281
- uint8x16_t res;
282
-
283
- res[ 0] = a[b[ 0]];
284
- res[ 1] = a[b[ 1]];
285
- res[ 2] = a[b[ 2]];
286
- res[ 3] = a[b[ 3]];
287
- res[ 4] = a[b[ 4]];
288
- res[ 5] = a[b[ 5]];
289
- res[ 6] = a[b[ 6]];
290
- res[ 7] = a[b[ 7]];
291
- res[ 8] = a[b[ 8]];
292
- res[ 9] = a[b[ 9]];
293
- res[10] = a[b[10]];
294
- res[11] = a[b[11]];
295
- res[12] = a[b[12]];
296
- res[13] = a[b[13]];
297
- res[14] = a[b[14]];
298
- res[15] = a[b[15]];
299
-
300
- return res;
301
- }
302
-
303
- #else
304
-
305
- #define lm_ggml_int16x8x2_t int16x8x2_t
306
- #define lm_ggml_uint8x16x2_t uint8x16x2_t
307
- #define lm_ggml_uint8x16x4_t uint8x16x4_t
308
- #define lm_ggml_int8x16x2_t int8x16x2_t
309
- #define lm_ggml_int8x16x4_t int8x16x4_t
310
-
311
- #define lm_ggml_vld1q_s16_x2 vld1q_s16_x2
312
- #define lm_ggml_vld1q_u8_x2 vld1q_u8_x2
313
- #define lm_ggml_vld1q_u8_x4 vld1q_u8_x4
314
- #define lm_ggml_vld1q_s8_x2 vld1q_s8_x2
315
- #define lm_ggml_vld1q_s8_x4 vld1q_s8_x4
316
- #define lm_ggml_vqtbl1q_s8 vqtbl1q_s8
317
- #define lm_ggml_vqtbl1q_u8 vqtbl1q_u8
318
-
319
- #endif // !defined(__aarch64__)
320
-
321
- #if !defined(__ARM_FEATURE_DOTPROD)
322
-
323
- inline static int32x4_t lm_ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
324
- const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
325
- const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
326
-
327
- return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
328
- }
329
-
330
- #else
331
-
332
- #define lm_ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
333
-
334
- #endif // !defined(__ARM_FEATURE_DOTPROD)
335
-
336
- #endif // defined(__ARM_NEON)
337
-
338
- #ifdef __wasm_simd128__
339
- #include <wasm_simd128.h>
340
- #else
341
- #ifdef __POWER9_VECTOR__
342
- #include <altivec.h>
343
- #undef bool
344
- #define bool _Bool
345
- #else
346
- #if defined(_MSC_VER) || defined(__MINGW32__)
347
- #include <intrin.h>
348
- #else
349
- #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
350
- #if !defined(__riscv)
351
- #include <immintrin.h>
352
- #endif
353
- #endif
354
- #endif
355
- #endif
356
- #endif
357
-
358
- #ifdef __riscv_v_intrinsic
359
- #include <riscv_vector.h>
360
- #endif
361
-
362
- #if defined(__loongarch64)
363
- #if defined(__loongarch_asx)
364
- #include <lasxintrin.h>
365
- #endif
366
- #if defined(__loongarch_sx)
367
- #include <lsxintrin.h>
368
- #endif
369
- #endif
370
-
371
- #if defined(__VXE__) || defined(__VXE2__)
372
- #include <vecintrin.h>
373
-
374
- #define vec_neg(a) (-(a)) // Vector Negate
375
- #define vec_add(a, b) ((a) + (b)) // Vector Add
376
- #define vec_sub(a, b) ((a) - (b)) // Vector Subtract
377
- #define vec_mul(a, b) ((a) * (b)) // Vector Multiply
378
- #define vec_div(a, b) ((a) / (b)) // Vector Divide
379
- #define vec_sl(a, b) ((a) << (b)) // Vector Shift Left
380
- #define vec_sra(a, b) ((a) >> (b)) // Vector Shift Right
381
- #define vec_sr(a, b) ((a) >> (b)) // Vector Shift Right Algebraic
382
- #define vec_slo(a, b) vec_slb(a, (b) << 64) // Vector Shift Left by Octet
383
- #define vec_sro(a, b) vec_srb(a, (b) << 64) // Vector Shift Right by Octet
384
-
385
- #ifndef vec_and
386
- #define vec_and(a, b) ((a) & (b)) // Vector AND
387
- #endif
388
-
389
- #ifndef vec_or
390
- #define vec_or(a, b) ((a) | (b)) // Vector OR
391
- #endif
392
-
393
- #ifndef vec_xor
394
- #define vec_xor(a, b) ((a) ^ (b)) // Vector XOR
395
- #endif
396
-
397
- typedef signed char char8x16_t __attribute__((vector_size(16)));
398
- typedef unsigned char uchar8x16_t __attribute__((vector_size(16)));
399
-
400
- typedef int8_t int8x16_t __attribute__((vector_size(16)));
401
- typedef int16_t int16x8_t __attribute__((vector_size(16)));
402
- typedef int32_t int32x4_t __attribute__((vector_size(16)));
403
-
404
- typedef uint8_t uint8x16_t __attribute__((vector_size(16)));
405
- typedef uint16_t uint16x8_t __attribute__((vector_size(16)));
406
- typedef uint32_t uint32x4_t __attribute__((vector_size(16)));
407
-
408
- typedef float float32x4_t __attribute__((vector_size(16)));
409
- typedef double double64x2_t __attribute((vector_size(16)));
410
-
411
- typedef signed long long long64x2_t __attribute((vector_size(16)));
412
- typedef unsigned long long ulong64x2_t __attribute__((vector_size(16)));
413
-
414
- typedef struct lm_ggml_uint8x16x2_t {
415
- uint8x16_t val[2];
416
- } lm_ggml_uint8x16x2_t;
417
-
418
- inline static lm_ggml_uint8x16x2_t lm_ggml_vec_xl_u8x2(const uint8_t * ptr) {
419
- lm_ggml_uint8x16x2_t res;
420
-
421
- res.val[0] = vec_xl( 0, ptr);
422
- res.val[1] = vec_xl(16, ptr);
423
-
424
- return res;
425
- }
426
-
427
- typedef struct lm_ggml_uint8x16x4_t {
428
- uint8x16_t val[4];
429
- } lm_ggml_uint8x16x4_t;
430
-
431
- inline static lm_ggml_uint8x16x4_t lm_ggml_vec_xl_u8x4(const uint8_t * ptr) {
432
- lm_ggml_uint8x16x4_t res;
433
-
434
- res.val[0] = vec_xl( 0, ptr);
435
- res.val[1] = vec_xl(16, ptr);
436
- res.val[2] = vec_xl(32, ptr);
437
- res.val[3] = vec_xl(48, ptr);
438
-
439
- return res;
440
- }
441
-
442
- typedef struct lm_ggml_int8x16x4_t {
443
- int8x16_t val[4];
444
- } lm_ggml_int8x16x4_t;
445
-
446
- inline static lm_ggml_int8x16x4_t lm_ggml_vec_xl_s8x4(const int8_t * ptr) {
447
- lm_ggml_int8x16x4_t res;
448
-
449
- res.val[0] = vec_xl( 0, ptr);
450
- res.val[1] = vec_xl(16, ptr);
451
- res.val[2] = vec_xl(32, ptr);
452
- res.val[3] = vec_xl(48, ptr);
453
-
454
- return res;
455
- }
456
-
457
- typedef struct lm_ggml_int16x8x2_t {
458
- int16x8_t val[2];
459
- } lm_ggml_int16x8x2_t;
460
-
461
- inline static lm_ggml_int16x8x2_t lm_ggml_vec_xl_s16x2(const int16_t * ptr) {
462
- lm_ggml_int16x8x2_t res;
463
-
464
- res.val[0] = vec_xl( 0, ptr);
465
- res.val[1] = vec_xl(16, ptr);
466
-
467
- return res;
468
- }
469
-
470
- /*
471
- ! WARNING: Very slow. Use vec_perm if possible. Refer to iq4_xs
472
- ! or iq4_nl for example implementation.
473
- */
474
- inline static int8x16_t lm_ggml_vec_tbl(int8x16_t a, uint8x16_t b) {
475
- int8x16_t res;
476
-
477
- res[ 0] = a[b[ 0]];
478
- res[ 1] = a[b[ 1]];
479
- res[ 2] = a[b[ 2]];
480
- res[ 3] = a[b[ 3]];
481
- res[ 4] = a[b[ 4]];
482
- res[ 5] = a[b[ 5]];
483
- res[ 6] = a[b[ 6]];
484
- res[ 7] = a[b[ 7]];
485
- res[ 8] = a[b[ 8]];
486
- res[ 9] = a[b[ 9]];
487
- res[10] = a[b[10]];
488
- res[11] = a[b[11]];
489
- res[12] = a[b[12]];
490
- res[13] = a[b[13]];
491
- res[14] = a[b[14]];
492
- res[15] = a[b[15]];
493
-
494
- return res;
495
- }
496
-
497
- inline static int16x8_t vec_padd_s16(int16x8_t a, int16x8_t b) {
498
- const uchar8x16_t v_maske = { 0, 1, 4, 5, 8, 9, 12, 13,
499
- 16, 17, 20, 21, 24, 25, 28, 29 };
500
-
501
- const int16x8_t v_abo = vec_pack((int32x4_t)a, (int32x4_t)b);
502
- const int16x8_t v_abe = vec_perm(a, b, v_maske);
503
- return v_abo + v_abe;
504
- }
505
-
506
- inline static int32x4_t lm_ggml_vec_dot(int32x4_t acc, int8x16_t a, int8x16_t b) {
507
- const int16x8_t p = vec_mule(a, b) + vec_mulo(a, b);
508
- return acc + (vec_unpackh(p) + vec_unpackl(p));
509
- }
510
-
511
- #endif
512
-
513
- #if defined(__loongarch_asx)
514
- /* float type data load instructions */
515
- static __m128 __lsx_vreplfr2vr_s(const float val) {
516
- v4f32 res = {val, val, val, val};
517
- return (__m128)res;
518
- }
519
-
520
- static __m256 __lasx_xvreplfr2vr_s(const float val) {
521
- v8f32 res = {val, val, val, val, val, val, val, val};
522
- return (__m256)res;
523
- }
524
- #endif
525
-
526
- // TODO: move to ggml-threading
527
- void lm_ggml_barrier(struct lm_ggml_threadpool * tp);
528
-
529
- #ifdef __cplusplus
530
- }
531
- #endif