Delete llm_model.py
Browse files- llm_model.py +0 -92
llm_model.py
DELETED
@@ -1,92 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from langchain.chains import ConversationalRetrievalChain, StuffDocumentsChain
|
3 |
-
from langchain.prompts import PromptTemplate
|
4 |
-
from ipex_llm.langchain.llms import TransformersLLM
|
5 |
-
from langchain.vectorstores import FAISS
|
6 |
-
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
7 |
-
from ipex_llm.langchain.embeddings import TransformersEmbeddings
|
8 |
-
from langchain import LLMChain
|
9 |
-
from utils.utils import new_cd
|
10 |
-
|
11 |
-
parent_dir = os.path.dirname(__file__)
|
12 |
-
|
13 |
-
condense_template = """
|
14 |
-
Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
|
15 |
-
You can assume the discussion is about the video content.
|
16 |
-
REMEMBER: If there is no relevant information within the context, just say "Hmm, I'm \
|
17 |
-
not sure." Don't try to make up an answer. \
|
18 |
-
Chat History:
|
19 |
-
{chat_history}
|
20 |
-
Follow Up Question: {question}
|
21 |
-
Standalone question:
|
22 |
-
"""
|
23 |
-
|
24 |
-
qa_template = """
|
25 |
-
You are an AI assistant designed for answering questions about a meeting.
|
26 |
-
You are given a word records of this meeting.
|
27 |
-
Try to comprehend the dialogs and provide a answer based on it.
|
28 |
-
=========
|
29 |
-
{context}
|
30 |
-
=========
|
31 |
-
Question: {question}
|
32 |
-
Answer:
|
33 |
-
"""
|
34 |
-
# CONDENSE_QUESTION_PROMPT 用于将聊天历史记录和下一个问题压缩为一个独立的问题
|
35 |
-
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(condense_template)
|
36 |
-
# QA_PROMPT为机器人设定基调和目的
|
37 |
-
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
|
38 |
-
# DOC_PROMPT = PromptTemplate.from_template("Video Clip {video_clip}: {page_content}")
|
39 |
-
DOC_PROMPT = PromptTemplate.from_template("{page_content}")
|
40 |
-
|
41 |
-
|
42 |
-
class LlmReasoner():
|
43 |
-
def __init__(self, args):
|
44 |
-
self.history = []
|
45 |
-
self.llm_version = args.llm_version
|
46 |
-
self.embed_version = args.embed_version
|
47 |
-
self.qa_chain = None
|
48 |
-
self.vectorstore = None
|
49 |
-
self.top_k = args.top_k
|
50 |
-
self.qa_max_new_tokens = args.qa_max_new_tokens
|
51 |
-
self.init_model()
|
52 |
-
|
53 |
-
def init_model(self):
|
54 |
-
with new_cd(parent_dir):
|
55 |
-
self.llm = TransformersLLM.from_model_id_low_bit(
|
56 |
-
f"..\\checkpoints\\{self.llm_version}")
|
57 |
-
self.llm.streaming = False
|
58 |
-
self.embeddings = TransformersEmbeddings.from_model_id(
|
59 |
-
model_id=f"..\\checkpoints\\{self.embed_version}")
|
60 |
-
|
61 |
-
def create_qa_chain(self, args, input_log):
|
62 |
-
self.top_k = args.top_k
|
63 |
-
self.qa_max_new_tokens = args.qa_max_new_tokens
|
64 |
-
self.question_generator = LLMChain(llm=self.llm, prompt=CONDENSE_QUESTION_PROMPT)
|
65 |
-
self.answer_generator = LLMChain(llm=self.llm, prompt=QA_PROMPT,
|
66 |
-
llm_kwargs={"max_new_tokens": self.qa_max_new_tokens})
|
67 |
-
self.doc_chain = StuffDocumentsChain(llm_chain=self.answer_generator, document_prompt=DOC_PROMPT,
|
68 |
-
document_variable_name='context')
|
69 |
-
# 拆分查看字符的文本, 创建一个新的文本分割器
|
70 |
-
# self.text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0, keep_separator=True)
|
71 |
-
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=2048, chunk_overlap=0)
|
72 |
-
texts = self.text_splitter.split_text(input_log)
|
73 |
-
self.vectorstore = FAISS.from_texts(texts, self.embeddings,
|
74 |
-
metadatas=[{"video_clip": str(i)} for i in range(len(texts))])
|
75 |
-
retriever = self.vectorstore.as_retriever(search_kwargs={"k": self.top_k})
|
76 |
-
self.qa_chain = ConversationalRetrievalChain(retriever=retriever,
|
77 |
-
question_generator=self.question_generator,
|
78 |
-
combine_docs_chain=self.doc_chain,
|
79 |
-
return_generated_question=True,
|
80 |
-
return_source_documents=True,
|
81 |
-
rephrase_question=False)
|
82 |
-
|
83 |
-
def __call__(self, question):
|
84 |
-
response = self.qa_chain({"question": question, "chat_history": self.history})
|
85 |
-
answer = response["answer"]
|
86 |
-
generated_question = response["generated_question"]
|
87 |
-
source_documents = response["source_documents"]
|
88 |
-
self.history.append([question, answer])
|
89 |
-
return self.history, generated_question, source_documents
|
90 |
-
|
91 |
-
def clean_history(self):
|
92 |
-
self.history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|