-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03ccb0f
commit 5588cb0
Showing
10 changed files
with
56 additions
and
16 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
|
||
import streamlit as st | ||
|
||
from langchain.callbacks.base import BaseCallbackHandler | ||
|
||
|
||
class StreamHandler(BaseCallbackHandler): | ||
def __init__( | ||
self, container: st.delta_generator.DeltaGenerator, initial_text: str = "" | ||
): | ||
self.container = container | ||
self.text = initial_text | ||
self.run_id_ignore_token = None | ||
|
||
def on_llm_start(self, serialized: dict, prompts: list, **kwargs): | ||
# Workaround to prevent showing the rephrased question as output | ||
if prompts[0].startswith("Human"): | ||
self.run_id_ignore_token = kwargs.get("run_id") | ||
|
||
def on_llm_new_token(self, token: str, **kwargs) -> None: | ||
if self.run_id_ignore_token == kwargs.get("run_id", False): | ||
return | ||
self.text += token | ||
self.container.markdown(self.text) | ||
|
||
|
||
class PrintRetrievalHandler(BaseCallbackHandler): | ||
def __init__(self, container): | ||
self.status = container.status("**Context Retrieval**") | ||
|
||
def on_retriever_start(self, serialized: dict, query: str, **kwargs): | ||
self.status.write(f"**Question:** {query}") | ||
self.status.update(label=f"**Context Retrieval:** {query}") | ||
|
||
def on_retriever_end(self, documents, **kwargs): | ||
for idx, doc in enumerate(documents): | ||
source = os.path.basename(doc.metadata["source"]) | ||
self.status.write(f"**Document {idx} from {source}**") | ||
self.status.markdown(doc.page_content) | ||
self.status.update(state="complete") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters