Skip to content

Commit

Permalink
fix: check file exists before load/save session
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Oct 20, 2023
1 parent 80adc19 commit dacc66f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions android/src/main/java/com/rnllama/LlamaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ void onPartialCompletion(WritableMap tokenResult) {
}

public WritableMap loadSession(String path) {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("File path is empty");
}
File file = new File(path);
if (!file.exists()) {
throw new IllegalArgumentException("File does not exist: " + path);
}
WritableMap result = loadSession(this.context, path);
if (result.hasKey("error")) {
throw new IllegalStateException(result.getString("error"));
Expand All @@ -104,6 +111,9 @@ public WritableMap loadSession(String path) {
}

public int saveSession(String path, int size) {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("File path is empty");
}
return saveSession(this.context, path, size);
}

Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PODS:
- hermes-engine/Pre-built (= 0.72.3)
- hermes-engine/Pre-built (0.72.3)
- libevent (2.1.12)
- llama-rn (0.3.0-rc.1):
- llama-rn (0.3.0-rc.2):
- RCT-Folly
- RCTRequired
- RCTTypeSafety
Expand Down Expand Up @@ -1242,7 +1242,7 @@ SPEC CHECKSUMS:
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 10fbd3f62405c41ea07e71973ea61e1878d07322
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
llama-rn: 9d97cb43a97a1315dd6853226165291f0c661ea5
llama-rn: bd839b6cef0d2d94fd90bab43fed46efe1f4f961
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: a2faf4bad4e438ca37b2040cb8f7799baa065c18
RCTTypeSafety: cb09f3e4747b6d18331a15eb05271de7441ca0b3
Expand Down
10 changes: 10 additions & 0 deletions ios/RNLlamaContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ - (NSArray *)embedding:(NSString *)text {
}

- (NSDictionary *)loadSession:(NSString *)path {
if (!path || [path length] == 0) {
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Session path is empty" userInfo:nil];
}
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Session file does not exist" userInfo:nil];
}

size_t n_token_count_out = 0;
llama->embd.resize(llama->params.n_ctx);
if (!llama_load_session_file(llama->ctx, [path UTF8String], llama->embd.data(), llama->embd.capacity(), &n_token_count_out)) {
Expand All @@ -355,6 +362,9 @@ - (NSDictionary *)loadSession:(NSString *)path {
}

- (int)saveSession:(NSString *)path size:(int)size {
if (!path || [path length] == 0) {
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Session path is empty" userInfo:nil];
}
std::vector<llama_token> session_tokens = llama->embd;
int default_size = session_tokens.size();
int save_size = size > 0 && size <= default_size ? size : default_size;
Expand Down

0 comments on commit dacc66f

Please sign in to comment.