Spaces:
Running
Running
File size: 3,698 Bytes
a796108 9061790 a796108 d5a4cb4 a796108 9061790 a796108 9061790 a796108 9061790 d5a4cb4 9061790 45180a0 |
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 |
import streamlit as st
from typing import Dict, Any
from sql_formatter.core import format_sql
from langchain.callbacks.streamlit.streamlit_callback_handler import StreamlitCallbackHandler
from langchain.schema.output import LLMResult
class ChatDataSelfSearchCallBackHandler(StreamlitCallbackHandler):
def __init__(self) -> None:
self.progress_bar = st.progress(value=0.0, text="Working...")
self.tokens_stream = ""
def on_llm_start(self, serialized, prompts, **kwargs) -> None:
pass
def on_text(self, text: str, **kwargs) -> None:
self.progress_bar.progress(value=0.2, text="Asking LLM...")
def on_chain_end(self, outputs, **kwargs) -> None:
self.progress_bar.progress(value=0.6, text='Searching in DB...')
st.markdown('### Generated Filter')
st.write(outputs['text'], unsafe_allow_html=True)
def on_chain_start(self, serialized, inputs, **kwargs) -> None:
pass
class ChatDataSelfAskCallBackHandler(StreamlitCallbackHandler):
def __init__(self) -> None:
self.progress_bar = st.progress(value=0.0, text='Searching DB...')
self.status_bar = st.empty()
self.prog_value = 0.0
self.prog_map = {
'langchain.chains.qa_with_sources.retrieval.RetrievalQAWithSourcesChain': 0.2,
'langchain.chains.combine_documents.map_reduce.MapReduceDocumentsChain': 0.4,
'langchain.chains.combine_documents.stuff.StuffDocumentsChain': 0.8
}
def on_llm_start(self, serialized, prompts, **kwargs) -> None:
pass
def on_text(self, text: str, **kwargs) -> None:
pass
def on_chain_start(self, serialized, inputs, **kwargs) -> None:
cid = '.'.join(serialized['id'])
if cid != 'langchain.chains.llm.LLMChain':
self.progress_bar.progress(value=self.prog_map[cid], text=f'Running Chain `{cid}`...')
self.prog_value = self.prog_map[cid]
else:
self.prog_value += 0.1
self.progress_bar.progress(value=self.prog_value, text=f'Running Chain `{cid}`...')
def on_chain_end(self, outputs, **kwargs) -> None:
pass
class ChatDataSQLSearchCallBackHandler(StreamlitCallbackHandler):
def __init__(self) -> None:
self.progress_bar = st.progress(value=0.0, text='Writing SQL...')
self.status_bar = st.empty()
self.prog_value = 0
self.prog_interval = 0.2
def on_llm_start(self, serialized, prompts, **kwargs) -> None:
pass
def on_llm_end(
self,
response: LLMResult,
*args,
**kwargs,
):
text = response.generations[0][0].text
if text.replace(' ', '').upper().startswith('SELECT'):
st.write('We generated Vector SQL for you:')
st.markdown(f'''```sql\n{format_sql(text, max_len=80)}\n```''')
print(f"Vector SQL: {text}")
self.prog_value += self.prog_interval
self.progress_bar.progress(value=self.prog_value, text="Searching in DB...")
def on_chain_start(self, serialized, inputs, **kwargs) -> None:
cid = '.'.join(serialized['id'])
self.prog_value += self.prog_interval
self.progress_bar.progress(value=self.prog_value, text=f'Running Chain `{cid}`...')
def on_chain_end(self, outputs, **kwargs) -> None:
pass
class ChatDataSQLAskCallBackHandler(ChatDataSQLSearchCallBackHandler):
def __init__(self) -> None:
self.progress_bar = st.progress(value=0.0, text='Writing SQL...')
self.status_bar = st.empty()
self.prog_value = 0
self.prog_interval = 0.1 |