@dr33m/react-native-litert-lm 0.5.0

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 (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +627 -0
  3. package/android/CMakeLists.txt +32 -0
  4. package/android/build.gradle +104 -0
  5. package/android/src/main/AndroidManifest.xml +19 -0
  6. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +888 -0
  7. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridModelStore.kt +189 -0
  8. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/LiteRTLMRegistry.kt +32 -0
  9. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/MultimodalPartFactories.kt +15 -0
  10. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/StreamingCallbackListener.kt +89 -0
  11. package/android/src/main/java/dev/litert/litertlm/LiteRTLMInitProvider.kt +57 -0
  12. package/android/src/main/java/dev/litert/litertlm/LiteRTLMPackage.kt +43 -0
  13. package/android/src/test/java/com/margelo/nitro/core/Promise.kt +46 -0
  14. package/android/src/test/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMTest.kt +122 -0
  15. package/app.plugin.js +58 -0
  16. package/cpp/cpp-adapter.cpp +21 -0
  17. package/cpp/include/README.md +32 -0
  18. package/cpp/include/litert_lm_engine.h +516 -0
  19. package/cpp/include/stb_image.h +7988 -0
  20. package/ios/HybridLiteRTLM+Execute.swift +287 -0
  21. package/ios/HybridLiteRTLM+Streaming.swift +153 -0
  22. package/ios/HybridLiteRTLM.swift +541 -0
  23. package/ios/HybridModelStore.swift +206 -0
  24. package/ios/MultimodalPart+Factories.swift +22 -0
  25. package/ios/Tests/HybridLiteRTLMTests.swift +187 -0
  26. package/lib/__mocks__/react-native-nitro-modules.d.ts +81 -0
  27. package/lib/__mocks__/react-native-nitro-modules.js +98 -0
  28. package/lib/__tests__/hooks.test.d.ts +1 -0
  29. package/lib/__tests__/hooks.test.js +175 -0
  30. package/lib/__tests__/inferenceRouting.test.d.ts +1 -0
  31. package/lib/__tests__/inferenceRouting.test.js +28 -0
  32. package/lib/__tests__/memoryTracker.test.d.ts +1 -0
  33. package/lib/__tests__/memoryTracker.test.js +74 -0
  34. package/lib/__tests__/modelFactory.test.d.ts +1 -0
  35. package/lib/__tests__/modelFactory.test.js +107 -0
  36. package/lib/__tests__/modelPath.test.d.ts +1 -0
  37. package/lib/__tests__/modelPath.test.js +12 -0
  38. package/lib/__tests__/modelRegistry.test.d.ts +1 -0
  39. package/lib/__tests__/modelRegistry.test.js +61 -0
  40. package/lib/hooks.d.ts +46 -0
  41. package/lib/hooks.js +159 -0
  42. package/lib/index.d.ts +142 -0
  43. package/lib/index.js +184 -0
  44. package/lib/inferenceRouting.d.ts +18 -0
  45. package/lib/inferenceRouting.js +63 -0
  46. package/lib/memoryTracker.d.ts +128 -0
  47. package/lib/memoryTracker.js +155 -0
  48. package/lib/modelFactory.d.ts +24 -0
  49. package/lib/modelFactory.js +128 -0
  50. package/lib/modelPath.d.ts +8 -0
  51. package/lib/modelPath.js +18 -0
  52. package/lib/modelRegistry.d.ts +55 -0
  53. package/lib/modelRegistry.js +82 -0
  54. package/lib/specs/LiteRTLM.nitro.d.ts +380 -0
  55. package/lib/specs/LiteRTLM.nitro.js +2 -0
  56. package/nitrogen/generated/.gitattributes +1 -0
  57. package/nitrogen/generated/android/LiteRTLM+autolinking.cmake +83 -0
  58. package/nitrogen/generated/android/LiteRTLM+autolinking.gradle +27 -0
  59. package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +74 -0
  60. package/nitrogen/generated/android/LiteRTLMOnLoad.hpp +34 -0
  61. package/nitrogen/generated/android/c++/JBackend.hpp +61 -0
  62. package/nitrogen/generated/android/c++/JExecuteResult.hpp +86 -0
  63. package/nitrogen/generated/android/c++/JFunc_void_double.hpp +75 -0
  64. package/nitrogen/generated/android/c++/JFunc_void_std__string_bool.hpp +76 -0
  65. package/nitrogen/generated/android/c++/JGenerationStats.hpp +77 -0
  66. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +371 -0
  67. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +83 -0
  68. package/nitrogen/generated/android/c++/JHybridModelStoreSpec.cpp +101 -0
  69. package/nitrogen/generated/android/c++/JHybridModelStoreSpec.hpp +67 -0
  70. package/nitrogen/generated/android/c++/JLLMConfig.hpp +125 -0
  71. package/nitrogen/generated/android/c++/JMemoryUsage.hpp +69 -0
  72. package/nitrogen/generated/android/c++/JMessage.hpp +63 -0
  73. package/nitrogen/generated/android/c++/JModelFile.hpp +69 -0
  74. package/nitrogen/generated/android/c++/JMultimodalPart.hpp +78 -0
  75. package/nitrogen/generated/android/c++/JPartType.hpp +61 -0
  76. package/nitrogen/generated/android/c++/JRole.hpp +61 -0
  77. package/nitrogen/generated/android/c++/JToolCall.hpp +61 -0
  78. package/nitrogen/generated/android/c++/JToolDefinition.hpp +65 -0
  79. package/nitrogen/generated/android/c++/JToolResponse.hpp +61 -0
  80. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Backend.kt +24 -0
  81. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ExecuteResult.kt +61 -0
  82. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Func_void_double.kt +80 -0
  83. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Func_void_std__string_bool.kt +80 -0
  84. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/GenerationStats.kt +76 -0
  85. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +165 -0
  86. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridModelStoreSpec.kt +76 -0
  87. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LLMConfig.kt +106 -0
  88. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LiteRTLMOnLoad.kt +35 -0
  89. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MemoryUsage.kt +66 -0
  90. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Message.kt +56 -0
  91. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ModelFile.kt +66 -0
  92. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MultimodalPart.kt +71 -0
  93. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/PartType.kt +24 -0
  94. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Role.kt +24 -0
  95. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolCall.kt +56 -0
  96. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolDefinition.kt +61 -0
  97. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolResponse.kt +56 -0
  98. package/nitrogen/generated/ios/LiteRTLM+autolinking.rb +62 -0
  99. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.cpp +98 -0
  100. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.hpp +585 -0
  101. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Umbrella.hpp +93 -0
  102. package/nitrogen/generated/ios/LiteRTLMAutolinking.mm +41 -0
  103. package/nitrogen/generated/ios/LiteRTLMAutolinking.swift +38 -0
  104. package/nitrogen/generated/ios/c++/HybridLiteRTLMSpecSwift.cpp +11 -0
  105. package/nitrogen/generated/ios/c++/HybridLiteRTLMSpecSwift.hpp +279 -0
  106. package/nitrogen/generated/ios/c++/HybridModelStoreSpecSwift.cpp +11 -0
  107. package/nitrogen/generated/ios/c++/HybridModelStoreSpecSwift.hpp +117 -0
  108. package/nitrogen/generated/ios/swift/Backend.swift +44 -0
  109. package/nitrogen/generated/ios/swift/ExecuteResult.swift +45 -0
  110. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  111. package/nitrogen/generated/ios/swift/Func_void_ExecuteResult.swift +46 -0
  112. package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
  113. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  114. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
  115. package/nitrogen/generated/ios/swift/Func_void_std__string_bool.swift +46 -0
  116. package/nitrogen/generated/ios/swift/GenerationStats.swift +54 -0
  117. package/nitrogen/generated/ios/swift/HybridLiteRTLMSpec.swift +75 -0
  118. package/nitrogen/generated/ios/swift/HybridLiteRTLMSpec_cxx.swift +516 -0
  119. package/nitrogen/generated/ios/swift/HybridModelStoreSpec.swift +59 -0
  120. package/nitrogen/generated/ios/swift/HybridModelStoreSpec_cxx.swift +203 -0
  121. package/nitrogen/generated/ios/swift/LLMConfig.swift +239 -0
  122. package/nitrogen/generated/ios/swift/MemoryUsage.swift +44 -0
  123. package/nitrogen/generated/ios/swift/Message.swift +34 -0
  124. package/nitrogen/generated/ios/swift/ModelFile.swift +44 -0
  125. package/nitrogen/generated/ios/swift/MultimodalPart.swift +101 -0
  126. package/nitrogen/generated/ios/swift/PartType.swift +44 -0
  127. package/nitrogen/generated/ios/swift/Role.swift +44 -0
  128. package/nitrogen/generated/ios/swift/ToolCall.swift +34 -0
  129. package/nitrogen/generated/ios/swift/ToolDefinition.swift +39 -0
  130. package/nitrogen/generated/ios/swift/ToolResponse.swift +34 -0
  131. package/nitrogen/generated/shared/c++/Backend.hpp +80 -0
  132. package/nitrogen/generated/shared/c++/ExecuteResult.hpp +94 -0
  133. package/nitrogen/generated/shared/c++/GenerationStats.hpp +103 -0
  134. package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp +41 -0
  135. package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +109 -0
  136. package/nitrogen/generated/shared/c++/HybridModelStoreSpec.cpp +25 -0
  137. package/nitrogen/generated/shared/c++/HybridModelStoreSpec.hpp +71 -0
  138. package/nitrogen/generated/shared/c++/LLMConfig.hpp +134 -0
  139. package/nitrogen/generated/shared/c++/MemoryUsage.hpp +95 -0
  140. package/nitrogen/generated/shared/c++/Message.hpp +89 -0
  141. package/nitrogen/generated/shared/c++/ModelFile.hpp +95 -0
  142. package/nitrogen/generated/shared/c++/MultimodalPart.hpp +103 -0
  143. package/nitrogen/generated/shared/c++/PartType.hpp +80 -0
  144. package/nitrogen/generated/shared/c++/Role.hpp +80 -0
  145. package/nitrogen/generated/shared/c++/ToolCall.hpp +87 -0
  146. package/nitrogen/generated/shared/c++/ToolDefinition.hpp +91 -0
  147. package/nitrogen/generated/shared/c++/ToolResponse.hpp +87 -0
  148. package/package.json +115 -0
  149. package/react-native-litert-lm.podspec +62 -0
  150. package/react-native.config.js +16 -0
  151. package/scripts/download-ios-frameworks.sh +39 -0
  152. package/scripts/framework-source.js +46 -0
  153. package/scripts/postinstall.js +145 -0
  154. package/src/__mocks__/react-native-nitro-modules.ts +132 -0
  155. package/src/__tests__/hooks.test.ts +216 -0
  156. package/src/__tests__/inferenceRouting.test.ts +36 -0
  157. package/src/__tests__/memoryTracker.test.ts +87 -0
  158. package/src/__tests__/modelFactory.test.ts +153 -0
  159. package/src/__tests__/modelPath.test.ts +14 -0
  160. package/src/__tests__/modelRegistry.test.ts +73 -0
  161. package/src/hooks.ts +234 -0
  162. package/src/index.ts +214 -0
  163. package/src/inferenceRouting.ts +80 -0
  164. package/src/memoryTracker.ts +268 -0
  165. package/src/modelFactory.ts +163 -0
  166. package/src/modelPath.ts +16 -0
  167. package/src/modelRegistry.ts +104 -0
  168. package/src/specs/LiteRTLM.nitro.ts +451 -0
@@ -0,0 +1,32 @@
1
+ # LiteRT-LM Headers Fallback
2
+
3
+ This directory contains the LiteRT-LM C API header (`litert_lm_engine.h`) used by the iOS C++ implementation.
4
+
5
+ ## If Headers Are Missing
6
+
7
+ If you get compilation errors like `litert_lm_engine.h: No such file or directory`, you need to manually copy the LiteRT-LM C API header here:
8
+
9
+ 1. Clone LiteRT-LM repository:
10
+
11
+ ```bash
12
+ git clone https://github.com/google-ai-edge/LiteRT-LM.git /tmp/LiteRT-LM
13
+ cd /tmp/LiteRT-LM && git checkout v0.10.2
14
+ ```
15
+
16
+ 2. Copy the header:
17
+ ```bash
18
+ cp /tmp/LiteRT-LM/c/litert_lm_engine.h ./
19
+ ```
20
+
21
+ The expected directory structure after copying:
22
+
23
+ ```
24
+ cpp/include/
25
+ ├── litert_lm_engine.h # LiteRT-LM C API header
26
+ ├── stb_image.h # Image loading for multimodal
27
+ └── README.md
28
+ ```
29
+
30
+ ## Note
31
+
32
+ On **Android**, headers are provided by the `litertlm-android` AAR via Prefab — this directory is only needed for the **iOS** build which uses the raw C API via the prebuilt XCFramework.
@@ -0,0 +1,516 @@
1
+ // Copyright 2025 The ODML Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef THIRD_PARTY_ODML_LITERT_LM_C_ENGINE_H_
16
+ #define THIRD_PARTY_ODML_LITERT_LM_C_ENGINE_H_
17
+
18
+ #include <stdbool.h>
19
+ #include <stddef.h>
20
+ #include <stdint.h>
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ // For Windows, __declspec( dllexport ) is required to export function in .dll.
27
+ // https://learn.microsoft.com/en-us/cpp/cpp/using-dllimport-and-dllexport-in-cpp-classes?view=msvc-170
28
+ //
29
+ // _WIN32 is defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM,
30
+ // x86, x64, or ARM64EC. Otherwise, undefined.
31
+ // https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros
32
+ #if defined(_WIN32)
33
+ #define LITERT_LM_C_API_EXPORT __declspec(dllexport)
34
+ #else
35
+ #define LITERT_LM_C_API_EXPORT
36
+ #endif
37
+
38
+ // Opaque pointer for the LiteRT LM Engine.
39
+ typedef struct LiteRtLmEngine LiteRtLmEngine;
40
+
41
+ // Opaque pointer for the LiteRT LM Session.
42
+ typedef struct LiteRtLmSession LiteRtLmSession;
43
+
44
+ // Opaque pointer for the LiteRT LM Responses.
45
+ typedef struct LiteRtLmResponses LiteRtLmResponses;
46
+
47
+ // Opaque pointer for the LiteRT LM Engine Settings.
48
+ typedef struct LiteRtLmEngineSettings LiteRtLmEngineSettings;
49
+
50
+ // Opaque pointer for the LiteRT LM Benchmark Info.
51
+ typedef struct LiteRtLmBenchmarkInfo LiteRtLmBenchmarkInfo;
52
+
53
+ // Opaque pointer for the LiteRT LM Conversation.
54
+ typedef struct LiteRtLmConversation LiteRtLmConversation;
55
+
56
+ // Opaque pointer for a JSON response.
57
+ typedef struct LiteRtLmJsonResponse LiteRtLmJsonResponse;
58
+
59
+ // Opaque pointer for LiteRT LM Session Config.
60
+ typedef struct LiteRtLmSessionConfig LiteRtLmSessionConfig;
61
+
62
+ // Opaque pointer for LiteRT LM Conversation Config.
63
+ typedef struct LiteRtLmConversationConfig LiteRtLmConversationConfig;
64
+
65
+ // Represents the type of sampler.
66
+ typedef enum {
67
+ kTypeUnspecified = 0,
68
+ // Probabilistically pick among the top k tokens.
69
+ kTopK = 1,
70
+ // Probabilistically pick among the tokens such that the sum is greater
71
+ // than or equal to p tokens after first performing top-k sampling.
72
+ kTopP = 2,
73
+ // Pick the token with maximum logit (i.e., argmax).
74
+ kGreedy = 3,
75
+ } Type;
76
+
77
+ // Parameters for the sampler.
78
+ typedef struct {
79
+ Type type;
80
+ int32_t top_k;
81
+ float top_p;
82
+ float temperature;
83
+ int32_t seed;
84
+ } LiteRtLmSamplerParams;
85
+
86
+ // Creates a LiteRT LM Session Config.
87
+ // The caller is responsible for destroying the config using
88
+ // `litert_lm_session_config_delete`.
89
+ // @return A pointer to the created config, or NULL on failure.
90
+ LITERT_LM_C_API_EXPORT
91
+ LiteRtLmSessionConfig* litert_lm_session_config_create();
92
+
93
+ // Sets the maximum number of output tokens per decode step for this session.
94
+ // @param config The config to modify.
95
+ // @param max_output_tokens The maximum number of output tokens.
96
+ LITERT_LM_C_API_EXPORT
97
+ void litert_lm_session_config_set_max_output_tokens(
98
+ LiteRtLmSessionConfig* config, int max_output_tokens);
99
+
100
+ // Sets the sampler parameters for this session config.
101
+ // @param config The config to modify.
102
+ // @param sampler_params The sampler parameters to use.
103
+ LITERT_LM_C_API_EXPORT
104
+ void litert_lm_session_config_set_sampler_params(
105
+ LiteRtLmSessionConfig* config, const LiteRtLmSamplerParams* sampler_params);
106
+
107
+ // Destroys a LiteRT LM Session Config.
108
+ // @param config The config to destroy.
109
+ LITERT_LM_C_API_EXPORT
110
+ void litert_lm_session_config_delete(LiteRtLmSessionConfig* config);
111
+
112
+ // Creates a LiteRT LM Conversation Config.
113
+ // The caller is responsible for destroying the config using
114
+ // `litert_lm_conversation_config_delete`.
115
+ // @param engine The engine to use.
116
+ // @param session_config The session config to use. If NULL, default
117
+ // session config will be used.
118
+ // @param system_message_json The system message in JSON format.
119
+ // @param tools_json The tools description in JSON array format.
120
+ // @param enable_constrained_decoding Whether to enable constrained decoding.
121
+ // @return A pointer to the created config, or NULL on failure.
122
+ LITERT_LM_C_API_EXPORT
123
+ LiteRtLmConversationConfig* litert_lm_conversation_config_create(
124
+ LiteRtLmEngine* engine, const LiteRtLmSessionConfig* session_config,
125
+ const char* system_message_json, const char* tools_json,
126
+ const char* messages_json, bool enable_constrained_decoding);
127
+
128
+ // Destroys a LiteRT LM Conversation Config.
129
+ // @param config The config to destroy.
130
+ LITERT_LM_C_API_EXPORT
131
+ void litert_lm_conversation_config_delete(LiteRtLmConversationConfig* config);
132
+
133
+ // Sets the minimum log level for the LiteRT LM library.
134
+ // Log levels are: 0=INFO, 1=WARNING, 2=ERROR, 3=FATAL.
135
+ LITERT_LM_C_API_EXPORT
136
+ void litert_lm_set_min_log_level(int level);
137
+
138
+ // Represents the type of input data.
139
+ typedef enum {
140
+ kInputText,
141
+ kInputImage,
142
+ kInputImageEnd,
143
+ kInputAudio,
144
+ kInputAudioEnd,
145
+ } InputDataType;
146
+
147
+ // Represents a single piece of input data.
148
+ typedef struct {
149
+ InputDataType type;
150
+ // The data pointer. The interpretation depends on the `type`.
151
+ // For kInputText, it's a UTF-8 string.
152
+ // For kInputImage and kInputAudio, it's a pointer to the raw bytes.
153
+ const void* data;
154
+ // The size of the data in bytes.
155
+ size_t size;
156
+ } InputData;
157
+
158
+ // Creates LiteRT LM Engine Settings. The caller is responsible for destroying
159
+ // the settings using `litert_lm_engine_settings_delete`.
160
+ //
161
+ // @param model_path The path to the model file.
162
+ // @param backend_str The backend to use (e.g., "cpu", "gpu").
163
+ // @param vision_backend_str The vision backend to use, or NULL if not set.
164
+ // @param audio_backend_str The audio backend to use, or NULL if not set.
165
+ // @return A pointer to the created settings, or NULL on failure.
166
+ LITERT_LM_C_API_EXPORT
167
+ LiteRtLmEngineSettings* litert_lm_engine_settings_create(
168
+ const char* model_path, const char* backend_str,
169
+ const char* vision_backend_str, const char* audio_backend_str);
170
+
171
+ // Destroys LiteRT LM Engine Settings.
172
+ //
173
+ // @param settings The settings to destroy.
174
+ LITERT_LM_C_API_EXPORT
175
+ void litert_lm_engine_settings_delete(LiteRtLmEngineSettings* settings);
176
+
177
+ // Sets the maximum number of tokens for the engine.
178
+ //
179
+ // @param settings The engine settings.
180
+ // @param max_num_tokens The maximum number of tokens.
181
+ LITERT_LM_C_API_EXPORT
182
+ void litert_lm_engine_settings_set_max_num_tokens(
183
+ LiteRtLmEngineSettings* settings, int max_num_tokens);
184
+
185
+ // Sets whether the engine should load different sections of the litertlm file
186
+ // in parallel. Defaults to true.
187
+ //
188
+ // @param settings The engine settings.
189
+ // @param parallel_file_section_loading Whether to load in parallel.
190
+ LITERT_LM_C_API_EXPORT
191
+ void litert_lm_engine_settings_set_parallel_file_section_loading(
192
+ LiteRtLmEngineSettings* settings, bool parallel_file_section_loading);
193
+
194
+ // Sets the cache directory for the engine.
195
+ //
196
+ // @param settings The engine settings.
197
+ // @param cache_dir The cache directory.
198
+ LITERT_LM_C_API_EXPORT
199
+ void litert_lm_engine_settings_set_cache_dir(LiteRtLmEngineSettings* settings,
200
+ const char* cache_dir);
201
+
202
+ // Sets the activation data type.
203
+ //
204
+ // @param settings The engine settings.
205
+ // @param activation_data_type_int The activation data type. See
206
+ // `ActivationDataType` in executor_settings_base.h for the possible values
207
+ // (e.g., 0 for F32, 1 for F16, 2 for I16, 3 for I8).
208
+ LITERT_LM_C_API_EXPORT
209
+ void litert_lm_engine_settings_set_activation_data_type(
210
+ LiteRtLmEngineSettings* settings, int activation_data_type_int);
211
+
212
+ // Sets the prefill chunk size for the engine. Only applicable for CPU backend
213
+ // with dynamic models.
214
+ //
215
+ // @param settings The engine settings.
216
+ // @param prefill_chunk_size The prefill chunk size.
217
+ LITERT_LM_C_API_EXPORT
218
+ void litert_lm_engine_settings_set_prefill_chunk_size(
219
+ LiteRtLmEngineSettings* settings, int prefill_chunk_size);
220
+
221
+ // Enables benchmarking for the engine.
222
+ //
223
+ // @param settings The engine settings.
224
+ LITERT_LM_C_API_EXPORT
225
+ void litert_lm_engine_settings_enable_benchmark(
226
+ LiteRtLmEngineSettings* settings);
227
+
228
+ // Sets the number of prefill tokens for benchmarking.
229
+ //
230
+ // @param settings The engine settings.
231
+ // @param num_prefill_tokens The number of prefill tokens.
232
+ LITERT_LM_C_API_EXPORT
233
+ void litert_lm_engine_settings_set_num_prefill_tokens(
234
+ LiteRtLmEngineSettings* settings, int num_prefill_tokens);
235
+
236
+ // Sets the number of decode tokens for benchmarking.
237
+ //
238
+ // @param settings The engine settings.
239
+ // @param num_decode_tokens The number of decode tokens.
240
+ LITERT_LM_C_API_EXPORT
241
+ void litert_lm_engine_settings_set_num_decode_tokens(
242
+ LiteRtLmEngineSettings* settings, int num_decode_tokens);
243
+
244
+ // Returns the last error message from a failed C API call.
245
+ // Returns an empty string if no error has occurred.
246
+ // The returned pointer is valid until the next C API call on the same thread.
247
+ LITERT_LM_C_API_EXPORT
248
+
249
+ // Creates a LiteRT LM Engine from the given settings. The caller is responsible
250
+ // for destroying the engine using `litert_lm_engine_delete`.
251
+ //
252
+ // @param settings The engine settings.
253
+ // @return A pointer to the created engine, or NULL on failure.
254
+ LITERT_LM_C_API_EXPORT
255
+ LiteRtLmEngine* litert_lm_engine_create(const LiteRtLmEngineSettings* settings);
256
+
257
+ // Destroys a LiteRT LM Engine.
258
+ //
259
+ // @param engine The engine to destroy.
260
+ LITERT_LM_C_API_EXPORT
261
+ void litert_lm_engine_delete(LiteRtLmEngine* engine);
262
+
263
+ // Creates a LiteRT LM Session. The caller is responsible for destroying the
264
+ // session using `litert_lm_session_delete`.
265
+ //
266
+ // @param engine The engine to create the session from.
267
+ // @param config The session config of the session. If NULL, use the default
268
+ // session config.
269
+ // @return A pointer to the created session, or NULL on failure.
270
+ LITERT_LM_C_API_EXPORT
271
+ LiteRtLmSession* litert_lm_engine_create_session(LiteRtLmEngine* engine,
272
+ LiteRtLmSessionConfig* config);
273
+
274
+ // Destroys a LiteRT LM Session.
275
+ //
276
+ // @param session The session to destroy.
277
+ LITERT_LM_C_API_EXPORT
278
+ void litert_lm_session_delete(LiteRtLmSession* session);
279
+
280
+ // Generates content from the input prompt.
281
+ //
282
+ // @param session The session to use for generation.
283
+ // @param inputs An array of InputData structs representing the multimodal
284
+ // input.
285
+ // @param num_inputs The number of InputData structs in the array.
286
+ // @return A pointer to the responses, or NULL on failure. The caller is
287
+ // responsible for deleting the responses using `litert_lm_responses_delete`.
288
+ LITERT_LM_C_API_EXPORT
289
+ LiteRtLmResponses* litert_lm_session_generate_content(LiteRtLmSession* session,
290
+ const InputData* inputs,
291
+ size_t num_inputs);
292
+ // Destroys a LiteRT LM Responses object.
293
+ //
294
+ // @param responses The responses to destroy.
295
+ LITERT_LM_C_API_EXPORT
296
+ void litert_lm_responses_delete(LiteRtLmResponses* responses);
297
+
298
+ // Returns the number of response candidates.
299
+ //
300
+ // @param responses The responses object.
301
+ // @return The number of candidates.
302
+ LITERT_LM_C_API_EXPORT
303
+ int litert_lm_responses_get_num_candidates(const LiteRtLmResponses* responses);
304
+
305
+ // Returns the response text at a given index.
306
+ //
307
+ // @param responses The responses object.
308
+ // @param index The index of the response.
309
+ // @return The response text. The returned string is owned by the `responses`
310
+ // object and is valid only for its lifetime. Returns NULL if index is out of
311
+ // bounds.
312
+ LITERT_LM_C_API_EXPORT
313
+ const char* litert_lm_responses_get_response_text_at(
314
+ const LiteRtLmResponses* responses, int index);
315
+
316
+ // Retrieves the benchmark information from the session. The caller is
317
+ // responsible for destroying the benchmark info using
318
+ // `litert_lm_benchmark_info_delete`.
319
+ //
320
+ // @param session The session to get the benchmark info from.
321
+ // @return A pointer to the benchmark info, or NULL on failure.
322
+ LITERT_LM_C_API_EXPORT
323
+ LiteRtLmBenchmarkInfo* litert_lm_session_get_benchmark_info(
324
+ LiteRtLmSession* session);
325
+
326
+ // Destroys a LiteRT LM Benchmark Info object.
327
+ //
328
+ // @param benchmark_info The benchmark info to destroy.
329
+ LITERT_LM_C_API_EXPORT
330
+ void litert_lm_benchmark_info_delete(LiteRtLmBenchmarkInfo* benchmark_info);
331
+
332
+ // Returns the time to the first token in seconds.
333
+ //
334
+ // Note that the first time to token doesn't include the time for
335
+ // initialization. It is the sum of the prefill time for the first turn and
336
+ // the time spent for decoding the first token.
337
+ //
338
+ // @param benchmark_info The benchmark info object.
339
+ // @return The time to the first token in seconds.
340
+ LITERT_LM_C_API_EXPORT
341
+ double litert_lm_benchmark_info_get_time_to_first_token(
342
+ const LiteRtLmBenchmarkInfo* benchmark_info);
343
+
344
+ // Returns the total initialization time in seconds.
345
+ //
346
+ // @param benchmark_info The benchmark info object.
347
+ // @return The total initialization time in seconds.
348
+ LITERT_LM_C_API_EXPORT
349
+ double litert_lm_benchmark_info_get_total_init_time_in_second(
350
+ const LiteRtLmBenchmarkInfo* benchmark_info);
351
+
352
+ // Returns the number of prefill turns.
353
+ //
354
+ // @param benchmark_info The benchmark info object.
355
+ // @return The number of prefill turns.
356
+ LITERT_LM_C_API_EXPORT
357
+ int litert_lm_benchmark_info_get_num_prefill_turns(
358
+ const LiteRtLmBenchmarkInfo* benchmark_info);
359
+
360
+ // Returns the number of decode turns.
361
+ //
362
+ // @param benchmark_info The benchmark info object.
363
+ // @return The number of decode turns.
364
+ LITERT_LM_C_API_EXPORT
365
+ int litert_lm_benchmark_info_get_num_decode_turns(
366
+ const LiteRtLmBenchmarkInfo* benchmark_info);
367
+
368
+ // Returns the prefill token count at a given turn index.
369
+ //
370
+ // @param benchmark_info The benchmark info object.
371
+ // @param index The index of the prefill turn.
372
+ // @return The prefill token count.
373
+ LITERT_LM_C_API_EXPORT
374
+ int litert_lm_benchmark_info_get_prefill_token_count_at(
375
+ const LiteRtLmBenchmarkInfo* benchmark_info, int index);
376
+
377
+ // Returns the decode token count at a given turn index.
378
+ //
379
+ // @param benchmark_info The benchmark info object.
380
+ // @param index The index of the decode turn.
381
+ // @return The decode token count.
382
+ LITERT_LM_C_API_EXPORT
383
+ int litert_lm_benchmark_info_get_decode_token_count_at(
384
+ const LiteRtLmBenchmarkInfo* benchmark_info, int index);
385
+
386
+ // Returns the prefill tokens per second at a given turn index.
387
+ //
388
+ // @param benchmark_info The benchmark info object.
389
+ // @param index The index of the prefill turn.
390
+ // @return The prefill tokens per second.
391
+ LITERT_LM_C_API_EXPORT
392
+ double litert_lm_benchmark_info_get_prefill_tokens_per_sec_at(
393
+ const LiteRtLmBenchmarkInfo* benchmark_info, int index);
394
+
395
+ // Returns the decode tokens per second at a given turn index.
396
+ //
397
+ // @param benchmark_info The benchmark info object.
398
+ // @param index The index of the decode turn.
399
+ // @return The decode tokens per second.
400
+ LITERT_LM_C_API_EXPORT
401
+ double litert_lm_benchmark_info_get_decode_tokens_per_sec_at(
402
+ const LiteRtLmBenchmarkInfo* benchmark_info, int index);
403
+
404
+ // Callback for streaming responses.
405
+ // `callback_data` is a pointer to user-defined data passed to the stream
406
+ // function. `chunk` is the piece of text from the stream. It's only valid for
407
+ // the duration of the call. `is_final` is true if this is the last chunk in the
408
+ // stream. `error_msg` is a null-terminated string with an error message, or
409
+ // NULL on success.
410
+ typedef void (*LiteRtLmStreamCallback)(void* callback_data, const char* chunk,
411
+ bool is_final, const char* error_msg);
412
+
413
+ // Generates content from the input prompt and streams the response via a
414
+ // callback. This is a non-blocking call that will invoke the callback from a
415
+ // background thread for each chunk.
416
+ //
417
+ // @param session The session to use for generation.
418
+ // @param inputs An array of InputData structs representing the multimodal
419
+ // input.
420
+ // @param num_inputs The number of InputData structs in the array.
421
+ // @param callback The callback function to receive response chunks.
422
+ // @param callback_data A pointer to user data that will be passed to the
423
+ // callback.
424
+ // @return 0 on success, non-zero on failure to start the stream.
425
+ LITERT_LM_C_API_EXPORT
426
+ int litert_lm_session_generate_content_stream(LiteRtLmSession* session,
427
+ const InputData* inputs,
428
+ size_t num_inputs,
429
+ LiteRtLmStreamCallback callback,
430
+ void* callback_data);
431
+
432
+ // Creates a LiteRT LM Conversation. The caller is responsible for destroying
433
+ // the conversation using `litert_lm_conversation_delete`.
434
+ //
435
+ // @param engine The engine to create the conversation from.
436
+ // @param config The conversation config to use. If NULL, the default config
437
+ // will be used.
438
+ // @return A pointer to the created conversation, or NULL on failure.
439
+ LITERT_LM_C_API_EXPORT
440
+ LiteRtLmConversation* litert_lm_conversation_create(
441
+ LiteRtLmEngine* engine, LiteRtLmConversationConfig* config);
442
+
443
+ // Destroys a LiteRT LM Conversation.
444
+ //
445
+ // @param conversation The conversation to destroy.
446
+ LITERT_LM_C_API_EXPORT
447
+ void litert_lm_conversation_delete(LiteRtLmConversation* conversation);
448
+
449
+ // Sends a message to the conversation and returns the response.
450
+ // This is a blocking call.
451
+ //
452
+ // @param conversation The conversation to use.
453
+ // @param message_json A JSON string representing the message to send.
454
+ // @param extra_context A JSON string representing the extra context to use.
455
+ // @return A pointer to the JSON response, or NULL on failure. The caller is
456
+ // responsible for deleting the response using
457
+ // `litert_lm_json_response_delete`.
458
+ LITERT_LM_C_API_EXPORT
459
+ LiteRtLmJsonResponse* litert_lm_conversation_send_message(
460
+ LiteRtLmConversation* conversation, const char* message_json,
461
+ const char* extra_context);
462
+
463
+ // Destroys a LiteRT LM Json Response object.
464
+ //
465
+ // @param response The response to destroy.
466
+ LITERT_LM_C_API_EXPORT
467
+ void litert_lm_json_response_delete(LiteRtLmJsonResponse* response);
468
+
469
+ // Returns the JSON response string from a response object.
470
+ //
471
+ // @param response The response object.
472
+ // @return The response JSON string. The returned string is owned by the
473
+ // `response` object and is valid only for its lifetime. Returns NULL if
474
+ // response is NULL.
475
+ LITERT_LM_C_API_EXPORT
476
+ const char* litert_lm_json_response_get_string(
477
+ const LiteRtLmJsonResponse* response);
478
+
479
+ // Sends a message to the conversation and streams the response via a
480
+ // callback. This is a non-blocking call that will invoke the callback from a
481
+ // background thread for each chunk.
482
+ //
483
+ // @param conversation The conversation to use.
484
+ // @param message_json A JSON string representing the message to send.
485
+ // @param extra_context A JSON string representing the extra context to use.
486
+ // @param callback The callback function to receive response chunks.
487
+ // @param callback_data A pointer to user data that will be passed to the
488
+ // callback.
489
+ // @return 0 on success, non-zero on failure to start the stream.
490
+ LITERT_LM_C_API_EXPORT
491
+ int litert_lm_conversation_send_message_stream(
492
+ LiteRtLmConversation* conversation, const char* message_json,
493
+ const char* extra_context, LiteRtLmStreamCallback callback,
494
+ void* callback_data);
495
+
496
+ // Cancels the ongoing inference process, for asynchronous inference.
497
+ //
498
+ // @param conversation The conversation to cancel the inference for.
499
+ LITERT_LM_C_API_EXPORT
500
+ void litert_lm_conversation_cancel_process(LiteRtLmConversation* conversation);
501
+
502
+ // Retrieves the benchmark information from the conversation. The caller is
503
+ // responsible for destroying the benchmark info using
504
+ // `litert_lm_benchmark_info_delete`.
505
+ //
506
+ // @param conversation The conversation to get the benchmark info from.
507
+ // @return A pointer to the benchmark info, or NULL on failure.
508
+ LITERT_LM_C_API_EXPORT
509
+ LiteRtLmBenchmarkInfo* litert_lm_conversation_get_benchmark_info(
510
+ LiteRtLmConversation* conversation);
511
+
512
+ #ifdef __cplusplus
513
+ } // extern "C"
514
+ #endif
515
+
516
+ #endif // THIRD_PARTY_ODML_LITERT_LM_C_ENGINE_H_