File size: 5,508 Bytes
7f9d16c
815128e
c2cb992
208786a
7f9d16c
c2cb992
815128e
7f9d16c
 
815128e
e182c41
2826548
7f9d16c
e182c41
815128e
c2cb992
2826548
4821924
815128e
e182c41
 
815128e
e182c41
 
 
815128e
7f9d16c
 
c2cb992
 
 
7f9d16c
 
815128e
7f9d16c
 
 
 
 
 
815128e
c2cb992
e182c41
 
 
 
 
 
 
 
 
 
 
7d1a192
 
 
e182c41
2826548
4821924
2826548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d1a192
 
 
2826548
e182c41
 
4821924
e182c41
 
 
 
2826548
e182c41
 
 
2826548
 
e182c41
 
 
 
 
2826548
 
 
 
 
 
a905cd1
2826548
e182c41
 
815128e
c2cb992
e182c41
815128e
c2cb992
e182c41
815128e
7f9d16c
e182c41
815128e
7d1a192
 
 
815128e
208786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f9d16c
208786a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# project/test.py

import os
import sys
import unittest
from timeit import default_timer as timer

from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import HumanMessage

from app_modules.init import app_init
from app_modules.llm_chat_chain import ChatChain
from app_modules.llm_loader import LLMLoader
from app_modules.utils import get_device_types, print_llm_response


class TestLLMLoader(unittest.TestCase):
    question = os.environ.get("CHAT_QUESTION")

    def run_test_case(self, llm_model_type, query):
        n_threds = int(os.environ.get("NUMBER_OF_CPU_CORES") or "4")

        hf_embeddings_device_type, hf_pipeline_device_type = get_device_types()
        print(f"hf_embeddings_device_type: {hf_embeddings_device_type}")
        print(f"hf_pipeline_device_type: {hf_pipeline_device_type}")

        llm_loader = LLMLoader(llm_model_type)
        start = timer()
        llm_loader.init(
            n_threds=n_threds, hf_pipeline_device_type=hf_pipeline_device_type
        )
        end = timer()
        print(f"Model loaded in {end - start:.3f}s")

        result = llm_loader.llm(
            [HumanMessage(content=query)] if llm_model_type == "openai" else query
        )
        end2 = timer()
        print(f"Inference completed in {end2 - end:.3f}s")
        print(result)

    def test_openai(self):
        self.run_test_case("openai", self.question)

    def test_llamacpp(self):
        self.run_test_case("llamacpp", self.question)

    def test_gpt4all_j(self):
        self.run_test_case("gpt4all-j", self.question)

    def test_huggingface(self):
        self.run_test_case("huggingface", self.question)

    def test_hftgi(self):
        self.run_test_case("hftgi", self.question)


class TestChatChain(unittest.TestCase):
    question = os.environ.get("CHAT_QUESTION")

    def run_test_case(self, llm_model_type, query):
        n_threds = int(os.environ.get("NUMBER_OF_CPU_CORES") or "4")

        hf_embeddings_device_type, hf_pipeline_device_type = get_device_types()
        print(f"hf_embeddings_device_type: {hf_embeddings_device_type}")
        print(f"hf_pipeline_device_type: {hf_pipeline_device_type}")

        llm_loader = LLMLoader(llm_model_type)
        start = timer()
        llm_loader.init(
            n_threds=n_threds, hf_pipeline_device_type=hf_pipeline_device_type
        )
        chat = ChatChain(llm_loader)
        end = timer()
        print(f"Model loaded in {end - start:.3f}s")

        inputs = {"question": query}
        result = chat.call_chain(inputs, None)
        end2 = timer()
        print(f"Inference completed in {end2 - end:.3f}s")
        print(result)

        inputs = {"question": "how many people?"}
        result = chat.call_chain(inputs, None)
        end3 = timer()
        print(f"Inference completed in {end3 - end2:.3f}s")
        print(result)

    def test_openai(self):
        self.run_test_case("openai", self.question)

    def test_llamacpp(self):
        self.run_test_case("llamacpp", self.question)

    def test_gpt4all_j(self):
        self.run_test_case("gpt4all-j", self.question)

    def test_huggingface(self):
        self.run_test_case("huggingface", self.question)

    def test_hftgi(self):
        self.run_test_case("hftgi", self.question)


class TestQAChain(unittest.TestCase):
    qa_chain: any
    question = os.environ.get("QA_QUESTION")

    def run_test_case(self, llm_model_type, query):
        start = timer()
        os.environ["LLM_MODEL_TYPE"] = llm_model_type
        qa_chain = app_init()[1]
        end = timer()
        print(f"App initialized in {end - start:.3f}s")

        chat_history = []
        inputs = {"question": query, "chat_history": chat_history}
        result = qa_chain.call_chain(inputs, None)
        end2 = timer()
        print(f"Inference completed in {end2 - end:.3f}s")
        print_llm_response(result)

        chat_history.append((query, result["answer"]))

        inputs = {"question": "tell me more", "chat_history": chat_history}
        result = qa_chain.call_chain(inputs, None)
        end3 = timer()
        print(f"Inference completed in {end3 - end2:.3f}s")
        print_llm_response(result)

    def test_openai(self):
        self.run_test_case("openai", self.question)

    def test_llamacpp(self):
        self.run_test_case("llamacpp", self.question)

    def test_gpt4all_j(self):
        self.run_test_case("gpt4all-j", self.question)

    def test_huggingface(self):
        self.run_test_case("huggingface", self.question)

    def test_hftgi(self):
        self.run_test_case("hftgi", self.question)


def chat():
    start = timer()
    llm_loader = app_init()[0]
    end = timer()
    print(f"Model loaded in {end - start:.3f}s")

    chat_chain = ChatChain(llm_loader)
    chat_history = []

    chat_start = timer()

    while True:
        query = input("Please enter your question: ")
        query = query.strip()
        if query.lower() == "exit":
            break

        print("\nQuestion: " + query)

        start = timer()
        result = chat_chain.call_chain(
            {"question": query, "chat_history": chat_history}, None
        )
        end = timer()
        print(f"Completed in {end - start:.3f}s")

        chat_history.append((query, result["text"]))

    chat_end = timer()
    print(f"Total time used: {chat_end - chat_start:.3f}s")


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "chat":
        chat()
    else:
        unittest.main()