-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsimplechain.py
More file actions
46 lines (35 loc) · 1.08 KB
/
Copy pathsimplechain.py
File metadata and controls
46 lines (35 loc) · 1.08 KB
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
from langchain_community.llms import CTransformers
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# Cau hinh
model_file = "models/vinallama-7b-chat_q5_0.gguf"
# Load LLM
def load_llm(model_file):
llm = CTransformers(
model=model_file,
model_type="llama",
max_new_tokens=1024,
temperature=0.01
)
return llm
# Tao prompt template
def creat_prompt(template):
prompt = PromptTemplate(template = template, input_variables=["question"])
return prompt
# Tao simple chain
def create_simple_chain(prompt, llm):
llm_chain = LLMChain(prompt=prompt, llm=llm)
return llm_chain
# Chay thu chain
template = """<|im_start|>system
Bạn là một trợ lí AI hữu ích. Hãy trả lời người dùng một cách chính xác.
<|im_end|>
<|im_start|>user
{question}<|im_end|>
<|im_start|>assistant"""
prompt = creat_prompt(template)
llm = load_llm(model_file)
llm_chain = create_simple_chain(prompt, llm)
question = "Một cộng một bằng mấy?"
response = llm_chain.invoke({"question":question})
print(response)