File size: 2,364 Bytes
45e2c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from langchain_community.llms import CTransformers
from langchain.prompts import PromptTemplate
from langchain_core.runnables import RunnableSequence
from langchain.chains import RetrievalQA
from langchain_community.embeddings import GPT4AllEmbeddings
from langchain_community.vectorstores import FAISS

from huggingface_hub import hf_hub_download

model_file = hf_hub_download(
    repo_id="Pudding48/TinyLlamaTest",   # Replace with your model repo
    filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
    cache_dir="model"  # Will be created in the Space's environment
)

# Cấu hình
#model_file = "model/tinyllama-1.1b-chat-v1.0.Q8_0.gguf"
vector_dp_path = "vectorstores/db_faiss"

# Load LLM
def load_llm(model_file):
    llm = CTransformers(
        model=model_file,
        model_type="llama",
        temperature=0.01,
        config={'gpu_layers': 0},
        max_new_tokens=128,  
        context_length=512
    )
    return llm

# Tạo prompt template
def creat_prompt(template):
    prompt = PromptTemplate(template=template, input_variables=["context","question"])
    return prompt

# Tạo pipeline chain (thay cho LLMChain)
def create_qa_chain(prompt, llm, db):
    llm_chain = RetrievalQA.from_chain_type(
        llm = llm,
        chain_type = "stuff",
        retriever =db.as_retriever(search_kwargs = {"k":1}),
        return_source_documents = False,
        chain_type_kwargs={'prompt':prompt}
    )
    return llm_chain

def read_vector_db():
    embedding_model = GPT4AllEmbeddings(model_file = "model/all-minilm-l6-v2-q4_0.gguf") 
    db = FAISS.load_local(vector_dp_path, embedding_model,allow_dangerous_deserialization=True)
    return db

db = read_vector_db()
llm = load_llm(model_file)
# Mẫu prompt
template = """<|im_start|>system\nSử dụng thông tin sau đây để trả lời câu hỏi. Nếu bạn không biết câu trả lời, hãy nói không biết, đừng cố tạo ra câu trả lời\n

    {context}<|im_end|>\n<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant"""

# Khởi tạo các thành phần
prompt = creat_prompt(template)
llm_chain  =create_qa_chain(prompt, llm, db)

# Chạy thử chain
question = "Khoa công nghệ thông tin thành lập năm nào ?"
response = llm_chain.invoke({"query": question})
print(response)