ColossalAI/applications/ColossalQA/tests/test_memory.py
YeAnbang e53e729d8e
[Feature] Add document retrieval QA (#5020)
* add langchain

* add langchain

* Add files via upload

* add langchain

* fix style

* fix style: remove extra space

* add pytest; modified retriever

* add pytest; modified retriever

* add tests to build_on_pr.yml

* fix build_on_pr.yml

* fix build on pr; fix environ vars

* seperate unit tests for colossalqa from build from pr

* fix container setting; fix environ vars

* commented dev code

* add incremental update

* remove stale code

* fix style

* change to sha3 224

* fix retriever; fix style; add unit test for document loader

* fix ci workflow config

* fix ci workflow config

* add set cuda visible device script in ci

* fix doc string

* fix style; update readme; refactored

* add force log info

* change build on pr, ignore colossalqa

* fix docstring, captitalize all initial letters

* fix indexing; fix text-splitter

* remove debug code, update reference

* reset previous commit

* update LICENSE update README add key-value mode, fix bugs

* add files back

* revert force push

* remove junk file

* add test files

* fix retriever bug, add intent classification

* change conversation chain design

* rewrite prompt and conversation chain

* add ui v1

* ui v1

* fix atavar

* add header

* Refactor the RAG Code and support Pangu

* Refactor the ColossalQA chain to Object-Oriented Programming and the UI demo.

* resolved conversation. tested scripts under examples. web demo still buggy

* fix ci tests

* Some modifications to add ChatGPT api

* modify llm.py and remove unnecessary files

* Delete applications/ColossalQA/examples/ui/test_frontend_input.json

* Remove OpenAI api key

* add colossalqa

* move files

* move files

* move files

* move files

* fix style

* Add Readme and fix some bugs.

* Add something to readme and modify some code

* modify a directory name for clarity

* remove redundant directory

* Correct a type in  llm.py

* fix AI prefix

* fix test_memory.py

* fix conversation

* fix some erros and typos

* Fix a missing import in RAG_ChatBot.py

* add colossalcloud LLM wrapper, correct issues in code review

---------

Co-authored-by: YeAnbang <anbangy2@outlook.com>
Co-authored-by: Orion-Zheng <zheng_zian@u.nus.edu>
Co-authored-by: Zian(Andy) Zheng <62330719+Orion-Zheng@users.noreply.github.com>
Co-authored-by: Orion-Zheng <zhengzian@u.nus.edu>
2023-11-23 10:33:48 +08:00

118 lines
4.6 KiB
Python

import os
from colossalqa.data_loader.document_loader import DocumentLoader
from colossalqa.local.llm import ColossalAPI, ColossalLLM
from colossalqa.memory import ConversationBufferWithSummary
from colossalqa.prompt.prompt import PROMPT_RETRIEVAL_QA_ZH
from colossalqa.retriever import CustomRetriever
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
def test_memory_long():
model_path = os.environ.get("EN_MODEL_PATH")
data_path = os.environ.get("TEST_DATA_PATH_EN")
model_name = os.environ.get("EN_MODEL_NAME")
sql_file_path = os.environ.get("SQL_FILE_PATH")
if not os.path.exists(sql_file_path):
os.makedirs(sql_file_path)
colossal_api = ColossalAPI.get_api(model_name, model_path)
llm = ColossalLLM(n=4, api=colossal_api)
memory = ConversationBufferWithSummary(
llm=llm, max_tokens=600, llm_kwargs={"max_new_tokens": 50, "temperature": 0.6, "do_sample": True}
)
retriever_data = DocumentLoader([[data_path, "company information"]]).all_data
# Split
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
splits = text_splitter.split_documents(retriever_data)
embedding = HuggingFaceEmbeddings(
model_name="moka-ai/m3e-base", model_kwargs={"device": "cpu"}, encode_kwargs={"normalize_embeddings": False}
)
# Create retriever
information_retriever = CustomRetriever(k=3, sql_file_path=sql_file_path)
information_retriever.add_documents(docs=splits, cleanup="incremental", mode="by_source", embedding=embedding)
memory.initiate_document_retrieval_chain(
llm,
PROMPT_RETRIEVAL_QA_ZH,
information_retriever,
chain_type_kwargs={
"chat_history": "",
},
)
# This keep the prompt length excluding dialogues the same
docs = information_retriever.get_relevant_documents("this is a test input.")
prompt_length = memory.chain.prompt_length(docs, **{"question": "this is a test input.", "chat_history": ""})
remain = 600 - prompt_length
have_summarization_flag = False
for i in range(40):
chat_history = memory.load_memory_variables({"question": "this is a test input.", "input_documents": docs})[
"chat_history"
]
assert memory.get_conversation_length() <= remain
memory.save_context({"question": "this is a test input."}, {"output": "this is a test output."})
if "A summarization of historical conversation:" in chat_history:
have_summarization_flag = True
assert have_summarization_flag == True
def test_memory_short():
model_path = os.environ.get("EN_MODEL_PATH")
data_path = os.environ.get("TEST_DATA_PATH_EN")
model_name = os.environ.get("EN_MODEL_NAME")
sql_file_path = os.environ.get("SQL_FILE_PATH")
if not os.path.exists(sql_file_path):
os.makedirs(sql_file_path)
colossal_api = ColossalAPI.get_api(model_name, model_path)
llm = ColossalLLM(n=4, api=colossal_api)
memory = ConversationBufferWithSummary(
llm=llm, llm_kwargs={"max_new_tokens": 50, "temperature": 0.6, "do_sample": True}
)
retriever_data = DocumentLoader([[data_path, "company information"]]).all_data
# Split
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
splits = text_splitter.split_documents(retriever_data)
embedding = HuggingFaceEmbeddings(
model_name="moka-ai/m3e-base", model_kwargs={"device": "cpu"}, encode_kwargs={"normalize_embeddings": False}
)
# create retriever
information_retriever = CustomRetriever(k=3, sql_file_path=sql_file_path)
information_retriever.add_documents(docs=splits, cleanup="incremental", mode="by_source", embedding=embedding)
memory.initiate_document_retrieval_chain(
llm,
PROMPT_RETRIEVAL_QA_ZH,
information_retriever,
chain_type_kwargs={
"chat_history": "",
},
)
# This keep the prompt length excluding dialogues the same
docs = information_retriever.get_relevant_documents("this is a test input.", return_scores=True)
for i in range(4):
chat_history = memory.load_memory_variables({"question": "this is a test input.", "input_documents": docs})[
"chat_history"
]
assert chat_history.count("Assistant: this is a test output.") == i
assert chat_history.count("Human: this is a test input.") == i
memory.save_context({"question": "this is a test input."}, {"output": "this is a test output."})
if __name__ == "__main__":
test_memory_short()
test_memory_long()