Donghao Huang
commited on
Commit
·
bf1e59b
1
Parent(s):
6b469d2
clean up code
Browse files- app_modules/llm_inference.py +7 -11
- test.py +0 -1
app_modules/llm_inference.py
CHANGED
|
@@ -5,7 +5,6 @@ import urllib
|
|
| 5 |
from queue import Queue
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
-
from langchain.callbacks.tracers import LangChainTracer
|
| 9 |
from langchain.chains.base import Chain
|
| 10 |
|
| 11 |
from app_modules.llm_loader import LLMLoader, TextIteratorStreamer
|
|
@@ -24,12 +23,8 @@ class LLMInference(metaclass=abc.ABCMeta):
|
|
| 24 |
def create_chain(self) -> Chain:
|
| 25 |
pass
|
| 26 |
|
| 27 |
-
def get_chain(self
|
| 28 |
if self.chain is None:
|
| 29 |
-
if tracing:
|
| 30 |
-
tracer = LangChainTracer()
|
| 31 |
-
tracer.load_default_session()
|
| 32 |
-
|
| 33 |
self.chain = self.create_chain()
|
| 34 |
|
| 35 |
return self.chain
|
|
@@ -39,7 +34,6 @@ class LLMInference(metaclass=abc.ABCMeta):
|
|
| 39 |
inputs,
|
| 40 |
streaming_handler,
|
| 41 |
q: Queue = None,
|
| 42 |
-
tracing: bool = False,
|
| 43 |
testing: bool = False,
|
| 44 |
):
|
| 45 |
print(inputs)
|
|
@@ -49,7 +43,7 @@ class LLMInference(metaclass=abc.ABCMeta):
|
|
| 49 |
try:
|
| 50 |
self.llm_loader.streamer.reset(q)
|
| 51 |
|
| 52 |
-
chain = self.get_chain(
|
| 53 |
result = (
|
| 54 |
self._run_chain(chain, inputs, streaming_handler, testing)
|
| 55 |
if streaming_handler is not None
|
|
@@ -84,7 +78,7 @@ class LLMInference(metaclass=abc.ABCMeta):
|
|
| 84 |
)
|
| 85 |
t.start()
|
| 86 |
|
| 87 |
-
if self.llm_loader.streamer.for_huggingface
|
| 88 |
count = (
|
| 89 |
2
|
| 90 |
if "chat_history" in inputs and len(inputs.get("chat_history")) > 0
|
|
@@ -94,12 +88,14 @@ class LLMInference(metaclass=abc.ABCMeta):
|
|
| 94 |
while count > 0:
|
| 95 |
try:
|
| 96 |
for token in self.llm_loader.streamer:
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
self.llm_loader.streamer.reset()
|
| 100 |
count -= 1
|
| 101 |
except Exception:
|
| 102 |
-
|
|
|
|
| 103 |
time.sleep(0.5)
|
| 104 |
|
| 105 |
t.join()
|
|
|
|
| 5 |
from queue import Queue
|
| 6 |
from threading import Thread
|
| 7 |
|
|
|
|
| 8 |
from langchain.chains.base import Chain
|
| 9 |
|
| 10 |
from app_modules.llm_loader import LLMLoader, TextIteratorStreamer
|
|
|
|
| 23 |
def create_chain(self) -> Chain:
|
| 24 |
pass
|
| 25 |
|
| 26 |
+
def get_chain(self) -> Chain:
|
| 27 |
if self.chain is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
self.chain = self.create_chain()
|
| 29 |
|
| 30 |
return self.chain
|
|
|
|
| 34 |
inputs,
|
| 35 |
streaming_handler,
|
| 36 |
q: Queue = None,
|
|
|
|
| 37 |
testing: bool = False,
|
| 38 |
):
|
| 39 |
print(inputs)
|
|
|
|
| 43 |
try:
|
| 44 |
self.llm_loader.streamer.reset(q)
|
| 45 |
|
| 46 |
+
chain = self.get_chain()
|
| 47 |
result = (
|
| 48 |
self._run_chain(chain, inputs, streaming_handler, testing)
|
| 49 |
if streaming_handler is not None
|
|
|
|
| 78 |
)
|
| 79 |
t.start()
|
| 80 |
|
| 81 |
+
if self.llm_loader.streamer.for_huggingface:
|
| 82 |
count = (
|
| 83 |
2
|
| 84 |
if "chat_history" in inputs and len(inputs.get("chat_history")) > 0
|
|
|
|
| 88 |
while count > 0:
|
| 89 |
try:
|
| 90 |
for token in self.llm_loader.streamer:
|
| 91 |
+
if not testing:
|
| 92 |
+
streaming_handler.on_llm_new_token(token)
|
| 93 |
|
| 94 |
self.llm_loader.streamer.reset()
|
| 95 |
count -= 1
|
| 96 |
except Exception:
|
| 97 |
+
if not testing:
|
| 98 |
+
print("nothing generated yet - retry in 0.5s")
|
| 99 |
time.sleep(0.5)
|
| 100 |
|
| 101 |
t.join()
|
test.py
CHANGED
|
@@ -72,7 +72,6 @@ while True:
|
|
| 72 |
{"question": query, "chat_history": chat_history},
|
| 73 |
custom_handler,
|
| 74 |
None,
|
| 75 |
-
False,
|
| 76 |
True,
|
| 77 |
)
|
| 78 |
end = timer()
|
|
|
|
| 72 |
{"question": query, "chat_history": chat_history},
|
| 73 |
custom_handler,
|
| 74 |
None,
|
|
|
|
| 75 |
True,
|
| 76 |
)
|
| 77 |
end = timer()
|