Views
No views yet
1from functools import partial
2
3import numpy as np
4from llama_cpp import Llama
5
6max_length = 512
7
8model = Llama.from_pretrained(
9 repo_id="mm/gte-Qwen2-7B-instruct-gguf",
10 filename="*Q4_K.gguf", # Choose from the avaiable formats,
11 embedding=True,
12 n_ctx=max_length,
13 n_batch=max_length,
14 flash_attn=True,
15 verbose=False,
16)
17model.tokenize = partial(model.tokenize, special=True)
18
19
20def calc_emb(s: str):
21 if len(model.tokenize(s.encode())) > max_length - 1:
22 print(
23 "The output will be calculated with truncation because of the length exceeding."
24 )
25 v = model.embed(s, normalize=True, truncate=True)
26 return np.asarray(v[-1])
27
28
29s = "今日の天気は?"
30t = "本日の天候は?"
31
32print(f"cossim({s}, {t}) = {(calc_emb(s) * calc_emb(t)).sum()}")