Views
No views yet
mlsa-iai-msu-lab/sci-rus-small using llama.cpp.1import numpy as np
2from sentence_transformers import SentenceTransformer
3from sentence_transformers.util import cos_sim
4import openai
5
6# ./llama.cpp/build/bin/llama-server --models-dir sci-rus-small-GGUF/ --embeddings
7openai_client = openai.OpenAI(
8 base_url="http://127.0.0.1:8080/v1",
9 api_key="sk-no-key-required",
10)
11
12# embedding get
13def get_embedding(text: str, limit_tokens: int=2048, model="embedding") -> list[float]:
14 response = openai_client.embeddings.create(
15 input=text[:limit_tokens],
16 model=model,
17 )
18 return response.data[0].embedding
19
20model = SentenceTransformer(
21 "mlsa-iai-msu-lab/sci-rus-small",
22)
23
24text = """Текст для математики. Пусть у нас есть функция f(x) = x^2 + 3x + 2. Найдите производную этой функции и определите ее критические точки."""
25
26embed1 = model.encode(text)
27
28for quant in ["Q8_0", "F16", "F32"]:
29 embed2 = np.array(get_embedding(text, model=f"sci-rus-small-{quant}"), dtype=np.float32)
30 print(f"Cosine Similarity with {quant}: {cos_sim(embed1, embed2).item()}")Cosine Similarity with Q8_0: 0.9999743103981018
Cosine Similarity with F16: 0.9999986290931702
Cosine Similarity with F32: 0.9999989867210388llama.cpp/conversion/base.py to add the new model:1# (after res = "roberta-bpe")
2 if chkhsh == "e56a55f379193cc03402ddce7b2fff655ca2d784ecfb003235a93341e84925bc":
3 # ref: https://huggingface.co/mlsa-iai-msu-lab/sci-rus-small
4 res = "roberta-bpe"1./llama.cpp/convert_hf_to_gguf.py sci-rus-small --outtype q8_0 --outfile sci-rus-small-GGUF/sci-rus-small-Q8_0.gguf
2./llama.cpp/convert_hf_to_gguf.py sci-rus-small --outtype f16 --outfile sci-rus-small-GGUF/sci-rus-small-F16.gguf
3./llama.cpp/convert_hf_to_gguf.py sci-rus-small --outtype f32 --outfile sci-rus-small-GGUF/sci-rus-small-F32.gguf