[Chat PDF] update chat pdf logic (#1053)
Co-authored-by: Deven Patel <deven298@yahoo.com>
This commit is contained in:
@@ -12,8 +12,7 @@ from embedchain.helpers.callbacks import (StreamingStdOutCallbackHandlerYield,
|
|||||||
generate)
|
generate)
|
||||||
|
|
||||||
|
|
||||||
@st.cache_resource
|
def embedchain_bot(db_path, api_key):
|
||||||
def embedchain_bot():
|
|
||||||
return App.from_config(
|
return App.from_config(
|
||||||
config={
|
config={
|
||||||
"llm": {
|
"llm": {
|
||||||
@@ -24,31 +23,43 @@ def embedchain_bot():
|
|||||||
"max_tokens": 1000,
|
"max_tokens": 1000,
|
||||||
"top_p": 1,
|
"top_p": 1,
|
||||||
"stream": True,
|
"stream": True,
|
||||||
|
"api_key": api_key,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"vectordb": {
|
"vectordb": {
|
||||||
"provider": "chroma",
|
"provider": "chroma",
|
||||||
"config": {"collection_name": "chat-pdf", "dir": "db", "allow_reset": True},
|
"config": {"collection_name": "chat-pdf", "dir": db_path, "allow_reset": True},
|
||||||
},
|
},
|
||||||
|
"embedder": {"provider": "openai", "config": {"api_key": api_key}},
|
||||||
"chunker": {"chunk_size": 2000, "chunk_overlap": 0, "length_function": "len"},
|
"chunker": {"chunk_size": 2000, "chunk_overlap": 0, "length_function": "len"},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@st.cache_data
|
def get_db_path():
|
||||||
def update_openai_key():
|
tmpdirname = tempfile.mkdtemp()
|
||||||
os.environ["OPENAI_API_KEY"] = st.session_state.chatbot_api_key
|
return tmpdirname
|
||||||
|
|
||||||
|
|
||||||
|
def get_ec_app(api_key):
|
||||||
|
if "app" in st.session_state:
|
||||||
|
print("Found app in session state")
|
||||||
|
app = st.session_state.app
|
||||||
|
else:
|
||||||
|
print("Creating app")
|
||||||
|
db_path = get_db_path()
|
||||||
|
app = embedchain_bot(db_path, api_key)
|
||||||
|
st.session_state.app = app
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
with st.sidebar:
|
with st.sidebar:
|
||||||
openai_access_token = st.text_input(
|
openai_access_token = st.text_input("OpenAI API Key", key="api_key", type="password")
|
||||||
"OpenAI API Key", value=os.environ.get("OPENAI_API_KEY"), key="chatbot_api_key", type="password"
|
|
||||||
) # noqa: E501
|
|
||||||
"WE DO NOT STORE YOUR OPENAI KEY."
|
"WE DO NOT STORE YOUR OPENAI KEY."
|
||||||
"Just paste your OpenAI API key here and we'll use it to power the chatbot. [Get your OpenAI API key](https://platform.openai.com/api-keys)" # noqa: E501
|
"Just paste your OpenAI API key here and we'll use it to power the chatbot. [Get your OpenAI API key](https://platform.openai.com/api-keys)" # noqa: E501
|
||||||
|
|
||||||
if openai_access_token:
|
if st.session_state.api_key:
|
||||||
update_openai_key()
|
app = get_ec_app(st.session_state.api_key)
|
||||||
|
|
||||||
pdf_files = st.file_uploader("Upload your PDF files", accept_multiple_files=True, type="pdf")
|
pdf_files = st.file_uploader("Upload your PDF files", accept_multiple_files=True, type="pdf")
|
||||||
add_pdf_files = st.session_state.get("add_pdf_files", [])
|
add_pdf_files = st.session_state.get("add_pdf_files", [])
|
||||||
@@ -57,10 +68,9 @@ with st.sidebar:
|
|||||||
if file_name in add_pdf_files:
|
if file_name in add_pdf_files:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
if not os.environ.get("OPENAI_API_KEY"):
|
if not st.session_state.api_key:
|
||||||
st.error("Please enter your OpenAI API Key")
|
st.error("Please enter your OpenAI API Key")
|
||||||
st.stop()
|
st.stop()
|
||||||
app = embedchain_bot()
|
|
||||||
temp_file_name = None
|
temp_file_name = None
|
||||||
with tempfile.NamedTemporaryFile(mode="wb", delete=False, prefix=file_name, suffix=".pdf") as f:
|
with tempfile.NamedTemporaryFile(mode="wb", delete=False, prefix=file_name, suffix=".pdf") as f:
|
||||||
f.write(pdf_file.getvalue())
|
f.write(pdf_file.getvalue())
|
||||||
@@ -97,11 +107,12 @@ for message in st.session_state.messages:
|
|||||||
st.markdown(message["content"])
|
st.markdown(message["content"])
|
||||||
|
|
||||||
if prompt := st.chat_input("Ask me anything!"):
|
if prompt := st.chat_input("Ask me anything!"):
|
||||||
if not os.environ.get("OPENAI_API_KEY"):
|
if not st.session_state.api_key:
|
||||||
st.error("Please enter your OpenAI API Key", icon="🤖")
|
st.error("Please enter your OpenAI API Key", icon="🤖")
|
||||||
st.stop()
|
st.stop()
|
||||||
|
|
||||||
app = embedchain_bot()
|
app = get_ec_app(st.session_state.api_key)
|
||||||
|
|
||||||
with st.chat_message("user"):
|
with st.chat_message("user"):
|
||||||
st.session_state.messages.append({"role": "user", "content": prompt})
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
||||||
st.markdown(prompt)
|
st.markdown(prompt)
|
||||||
@@ -146,5 +157,5 @@ if prompt := st.chat_input("Ask me anything!"):
|
|||||||
full_response += f"- {source}\n"
|
full_response += f"- {source}\n"
|
||||||
|
|
||||||
msg_placeholder.markdown(full_response)
|
msg_placeholder.markdown(full_response)
|
||||||
print("Answer: ", answer)
|
print("Answer: ", full_response)
|
||||||
st.session_state.messages.append({"role": "assistant", "content": answer})
|
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
||||||
|
|||||||
Reference in New Issue
Block a user