# -*- coding: utf-8 -*- import gradio as gr import os from langchain.retrievers import EnsembleRetriever from utils import * import requests from pyvi import ViTokenizer, ViPosTagger import time from transformers import AutoTokenizer, AutoModelForQuestionAnswering import torch from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.chains import create_history_aware_retriever, create_retrieval_chain from langchain_community.chat_message_histories import ChatMessageHistory retriever = load_the_embedding_retrieve(is_ready=False, k=10) bm25_retriever = load_the_bm25_retrieve(k=1) ensemble_retriever = EnsembleRetriever( retrievers=[bm25_retriever, retriever], weights=[0.1, 0.9] ) tokenizer = AutoTokenizer.from_pretrained("ShynBui/vie_qa", token=os.environ.get("HF_TOKEN")) model = AutoModelForQuestionAnswering.from_pretrained("ShynBui/vie_qa", token=os.environ.get("HF_TOKEN")) llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=1, openai_api_key=os.environ["OPENAI_API_KEY"]) def greet3(quote, history): # print(history) demo_ephemeral_chat_history = ChatMessageHistory() if history == '': history = [("Bạn có thể giải thích về quy chế và quyền của sinh viên tại trường này không?", '''Quy chế và quyền của sinh viên tại trường Đại học Mở TP.HCM được quy định rõ trong các điều khoản sau: 1. Hiệu trưởng Trường có quyền ra quyết định thành lập và quy định cụ thể về chức năng, nhiệm vụ, tổ chức và hoạt động của Hội đồng khen thưởng và kỷ luật sinh viên. 2. Sinh viên có quyền khiếu nại về khen thưởng, kỷ luật. Khi có vi phạm kỷ luật, sinh viên có quyền được phân tích và đề nghị hình thức kỷ luật thông qua việc họp với các tổ chức sinh viên và gửi biên bản họp đến phòng Công tác sinh viên để trình Hội đồng. 3. Sinh viên có quyền đề đạt nguyện vọng và khiếu nại lên Hiệu trưởng Trường để giải quyết các vấn đề có liên quan đến quyền, lợi ích chính đáng của sinh viên. 4. Sinh viên được hỗ trợ giới thiệu nhà trọ theo quy định của trường. Các chủ đề liên quan mà bạn có thể muốn tìm hiểu thêm: - Quy chế và quyền của sinh viên tại các trường đại học khác. - Hệ thống hỗ trợ sinh viên tại trường Đại học Mở TP.HCM. - Quy trình khiếu nại và giải quyết tranh chấp sinh viên tại trường Đại học Mở TP.HCM. '''), ("Chào.", "Chào. Chúng ta vừa bắt đầu câu chuyện thôi.")] for user, assistant in history[-1:]: demo_ephemeral_chat_history.add_user_message(user) demo_ephemeral_chat_history.add_ai_message(assistant) else: for user, assistant in eval(history)[-1:]: demo_ephemeral_chat_history.add_user_message(user) demo_ephemeral_chat_history.add_ai_message(assistant) # Summary the message chat_history = summarize_messages(demo_ephemeral_chat_history=demo_ephemeral_chat_history, llm=llm).messages # print("Chat history:", chat_history) # Get the new question new_question = get_question_from_summarize(chat_history[0].content, quote, llm) # Retrieve documents_query = ensemble_retriever.invoke(new_question) # print(documents_query) context = '' for i in documents_query: context += i.page_content + '\n' # print(context) # Get answer answer = get_final_answer(question=new_question, context=context, prompt=os.environ['PROMPT'], llm=llm) return new_question, answer if __name__ == "__main__": quote = "Địa chỉ nhà trường?" iface = gr.Interface(fn=greet3, inputs=["text", "text"], outputs=["text", "text"]) iface.launch(share=True) #Những cái đã làm tốt hơn những gì - Đóng góp gì # 1. Dataset - Xu lý # 2. Tăng ngữ cảnh # 3. Tăng khả năng truy vết # 4.