提交 2240ed1e 作者: imClumsyPanda

update requirements.txt

上级 3dc5860c
...@@ -16,13 +16,28 @@ def torch_gc(): ...@@ -16,13 +16,28 @@ def torch_gc():
torch.cuda.ipc_collect() torch.cuda.ipc_collect()
tokenizer = AutoTokenizer.from_pretrained(
"/Users/liuqian/Downloads/ChatGLM-6B/chatglm_hf_model",
# "THUDM/chatglm-6b",
trust_remote_code=True
)
model = (
AutoModel.from_pretrained(
"/Users/liuqian/Downloads/ChatGLM-6B/chatglm_hf_model",
# "THUDM/chatglm-6b",
trust_remote_code=True)
.float()
.to("mps")
# .half()
# .cuda()
)
class ChatGLM(LLM): class ChatGLM(LLM):
max_token: int = 10000 max_token: int = 10000
temperature: float = 0.1 temperature: float = 0.1
top_p = 0.9 top_p = 0.9
history = [] history = []
tokenizer: object = None
model: object = None
def __init__(self): def __init__(self):
super().__init__() super().__init__()
...@@ -34,8 +49,8 @@ class ChatGLM(LLM): ...@@ -34,8 +49,8 @@ class ChatGLM(LLM):
def _call(self, def _call(self,
prompt: str, prompt: str,
stop: Optional[List[str]] = None) -> str: stop: Optional[List[str]] = None) -> str:
response, updated_history = self.model.chat( response, updated_history = model.chat(
self.tokenizer, tokenizer,
prompt, prompt,
history=self.history, history=self.history,
max_length=self.max_token, max_length=self.max_token,
...@@ -48,16 +63,17 @@ class ChatGLM(LLM): ...@@ -48,16 +63,17 @@ class ChatGLM(LLM):
self.history = updated_history self.history = updated_history
return response return response
def load_model(self, def get_num_tokens(self, text: str) -> int:
model_name_or_path: str = "THUDM/chatglm-6b"): tokenized_text = tokenizer.tokenize(text)
self.tokenizer = AutoTokenizer.from_pretrained( return len(tokenized_text)
model_name_or_path,
trust_remote_code=True if __name__ == "__main__":
) history = []
self.model = ( while True:
AutoModel.from_pretrained( query = input("Input your question 请输入问题:")
model_name_or_path, resp, history = model.chat(tokenizer,
trust_remote_code=True) query,
.half() history=history,
.cuda() temperature=0.01,
) max_length=100000)
print(resp)
\ No newline at end of file
from langchain.prompts.prompt import PromptTemplate from langchain.prompts.prompt import PromptTemplate
from langchain.chains import ChatVectorDBChain from langchain.chains import ChatVectorDBChain, ConversationalRetrievalChain
from langchain.prompts.chat import ( from langchain.prompts.chat import (
ChatPromptTemplate, ChatPromptTemplate,
SystemMessagePromptTemplate, SystemMessagePromptTemplate,
...@@ -13,16 +13,13 @@ from chatglm_llm import ChatGLM ...@@ -13,16 +13,13 @@ from chatglm_llm import ChatGLM
embedding_model_dict = { embedding_model_dict = {
"ernie-tiny": "nghuyong/ernie-3.0-nano-zh", "ernie-tiny": "nghuyong/ernie-3.0-nano-zh",
"ernie-base": "nghuyong/ernie-3.0-base-zh", "ernie-base": "nghuyong/ernie-3.0-base-zh",
"text2vec": "GanymedeNil/text2vec-large-chinese" "text2vec": "/Users/liuqian/Downloads/ChatGLM-6B/chatglm_embedding"#"GanymedeNil/text2vec-large-chinese"
} }
llm_model_dict = {
"chatglm-6b": "THUDM/chatglm-6b",
"chatglm-6b-int4": "THUDM/chatglm-6b-int4"
}
chatglm = ChatGLM() chatglm = ChatGLM()
chatglm.load_model(model_name_or_path=llm_model_dict["chatglm-6b"])
def init_knowledge_vector_store(filepath): def init_knowledge_vector_store(filepath):
embeddings = HuggingFaceEmbeddings(model_name=embedding_model_dict["text2vec"], ) embeddings = HuggingFaceEmbeddings(model_name=embedding_model_dict["text2vec"], )
...@@ -56,15 +53,16 @@ def get_knowledge_based_answer(query, vector_store, chat_history=[]): ...@@ -56,15 +53,16 @@ def get_knowledge_based_answer(query, vector_store, chat_history=[]):
改写后的独立、完整的问题:""" 改写后的独立、完整的问题:"""
new_question_prompt = PromptTemplate.from_template(condese_propmt_template) new_question_prompt = PromptTemplate.from_template(condese_propmt_template)
chatglm.history = chat_history chatglm.history = chat_history
knowledge_chain = ChatVectorDBChain.from_llm( knowledge_chain = ConversationalRetrievalChain.from_llm(
llm=chatglm, llm=chatglm,
vectorstore=vector_store, retriever=vector_store.as_retriever(),
qa_prompt=prompt, qa_prompt=prompt,
condense_question_prompt=new_question_prompt, condense_question_prompt=new_question_prompt,
) )
knowledge_chain.return_source_documents = True knowledge_chain.return_source_documents = True
knowledge_chain.top_k_docs_for_context = 10 # knowledge_chain.top_k_docs_for_context = 10
knowledge_chain.max_tokens_limit = 10000
result = knowledge_chain({"question": query, "chat_history": chat_history}) result = knowledge_chain({"question": query, "chat_history": chat_history})
return result, chatglm.history return result, chatglm.history
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论