Spaces:
Running
Running
import gradio as gr | |
from langchain_community.llms import LlamaCpp | |
from langchain.prompts import ChatPromptTemplate | |
vector_db_path = "vectorstores/db_faiss" | |
llm_json = LlamaCpp( | |
model_path="Llama-3.1-8B-Instruct.Q5_K_M.gguf", | |
temperature=0, | |
max_tokens=512, | |
top_p=1, | |
# callback_manager=callback_manager, | |
verbose=True, | |
format='json' | |
) | |
print(llm_json) | |
llm = LlamaCpp( | |
model_path="Llama-3.1-8B-Instruct.Q5_K_M.gguf", | |
temperature=0, | |
max_tokens=512, | |
top_p=1, | |
# callback_manager=callback_manager, | |
verbose=True, # Verbose is required to pass to the callback manager | |
) | |
template = """Bạn là trợ lý ảo thông thái tên là Aleni. bạn hãy sử dụng dữ liệu dưới đây để trả lời câu hỏi, | |
nếu không có thông tin hãy đưa ra câu trả lời sát nhất với câu hỏi từ các thông tin tìm được hoặc tự suy luận | |
Question: {question} | |
Chỉ đưa ra các câu trả lời hữu ích. | |
Helpful answer: | |
""" | |
# Content: {content} | |
def respond(message, history, system_message, path_document): | |
prompt = ChatPromptTemplate.from_template(template) | |
llm_chain = prompt | llm | |
respon = '' | |
for chunk in llm_chain.stream(message): | |
respon += chunk | |
# print(chunk.content, end="", flush=True) | |
yield respon | |
demo = gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
# gr.Textbox(value="Trả lời câu hỏi CHỈ dựa trên ngữ cảnh sau không có thì bảo không có câu trả lời:", label="System message"), | |
gr.UploadButton("Upload a file", file_count="single"), | |
# gr.DownloadButton("Download the file") | |
], | |
) | |
if __name__ == "__main__": | |
demo.launch() |