1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| #include "llama.h"
#include <iostream>
int main() {
llama_model_params model_params = llama_model_default_params();
llama_context_params ctx_params = llama_context_default_params();
llama_model *model = llama_load_model_from_file("tinyllama-q4.gguf", model_params);
llama_context *ctx = llama_new_context_with_model(model, ctx_params);
std::string prompt = "ユーザーがExcelのデータを読み込んでフィルタして保存したいと言っています。ノード構成は?";
llama_batch batch = llama_batch_init(512, 0, 1);
llama_token BOS = llama_token_bos(model);
batch.token[0] = BOS;
// トークナイズ
std::vector<llama_token> tokens(prompt.size() + 8);
int n = llama_tokenize(model, prompt.c_str(), tokens.data(), tokens.size(), true);
tokens.resize(n);
for (size_t i = 0; i < tokens.size(); ++i) {
batch.token[i + 1] = tokens[i];
}
batch.n_tokens = tokens.size() + 1;
llama_decode(ctx, batch);
// 推論結果取得
for (int i = 0; i < 50; ++i) {
llama_token next = llama_sample_token(ctx, nullptr);
std::cout << llama_token_to_str(model, next);
llama_batch next_batch = llama_batch_init(1, 0, 1);
next_batch.token[0] = next;
next_batch.n_tokens = 1;
llama_decode(ctx, next_batch);
}
llama_free(ctx);
llama_free_model(model);
return 0;
}
|