Spaces:
Sleeping
Sleeping
Update src/chatbot.py
Browse files- src/chatbot.py +4 -10
src/chatbot.py
CHANGED
|
@@ -17,17 +17,13 @@ logger = logging.getLogger(__name__)
|
|
| 17 |
from langchain.callbacks.base import BaseCallbackHandler
|
| 18 |
|
| 19 |
class StreamHandler(BaseCallbackHandler):
|
| 20 |
-
def __init__(self
|
| 21 |
self.current_text = ""
|
| 22 |
-
|
| 23 |
|
| 24 |
def on_llm_new_token(self, token: str, **kwargs):
|
|
|
|
| 25 |
self.current_text += token
|
| 26 |
-
if self.placeholder:
|
| 27 |
-
self.placeholder.markdown(self.current_text + "▌")
|
| 28 |
-
|
| 29 |
-
def get_text(self):
|
| 30 |
-
return self.current_text
|
| 31 |
|
| 32 |
|
| 33 |
class BioethicsChatbot:
|
|
@@ -168,11 +164,9 @@ class BioethicsChatbot:
|
|
| 168 |
|
| 169 |
self.stream_handler.current_text = ""
|
| 170 |
|
| 171 |
-
if stream_placeholder:
|
| 172 |
-
self.stream_handler.placeholder = stream_placeholder
|
| 173 |
-
|
| 174 |
# streaming happens here
|
| 175 |
_ = self.llm.invoke(prompt)
|
|
|
|
| 176 |
|
| 177 |
answer = self.stream_handler.get_text()
|
| 178 |
self.history.append((question, answer))
|
|
|
|
| 17 |
from langchain.callbacks.base import BaseCallbackHandler
|
| 18 |
|
| 19 |
class StreamHandler(BaseCallbackHandler):
|
| 20 |
+
def __init__(self):
|
| 21 |
self.current_text = ""
|
| 22 |
+
|
| 23 |
|
| 24 |
def on_llm_new_token(self, token: str, **kwargs):
|
| 25 |
+
print(token, end="", flush=True) # stream to console
|
| 26 |
self.current_text += token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
class BioethicsChatbot:
|
|
|
|
| 164 |
|
| 165 |
self.stream_handler.current_text = ""
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
# streaming happens here
|
| 168 |
_ = self.llm.invoke(prompt)
|
| 169 |
+
print()
|
| 170 |
|
| 171 |
answer = self.stream_handler.get_text()
|
| 172 |
self.history.append((question, answer))
|