Spaces:
Sleeping
Sleeping
Commit
·
09113b6
1
Parent(s):
3d3f248
UI with chat history.
Browse files- .gitignore +2 -1
- main.py +49 -0
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
.env
|
| 2 |
-
/langchain-docs
|
|
|
|
|
|
| 1 |
.env
|
| 2 |
+
/langchain-docs
|
| 3 |
+
__pycache__/
|
main.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from backend.core import run_llm
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from streamlit_chat import message
|
| 4 |
+
from typing import Set
|
| 5 |
+
|
| 6 |
+
st.header("Peter's LangChain chatbot")
|
| 7 |
+
prompt = st.text_input("Ask me anything about LangChain", placeholder="...")
|
| 8 |
+
|
| 9 |
+
# Initialize session state variables if they don't exist yet.
|
| 10 |
+
if "user_prompt_history" not in st.session_state:
|
| 11 |
+
st.session_state.user_prompt_history = []
|
| 12 |
+
|
| 13 |
+
if "chat_answers_history" not in st.session_state:
|
| 14 |
+
st.session_state.chat_answers_history = []
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Format sources links (referencing LangChain urls) as a clean enumerated list.
|
| 18 |
+
def create_sources_string(source_urls: Set[str]) -> str:
|
| 19 |
+
if not source_urls:
|
| 20 |
+
return ""
|
| 21 |
+
sources_list = list(source_urls)
|
| 22 |
+
sources_list.sort()
|
| 23 |
+
sources_string = "sources:\n"
|
| 24 |
+
for i, source in enumerate(sources_list):
|
| 25 |
+
sources_string += f"{i+1}. {source}\n"
|
| 26 |
+
return sources_string
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if prompt:
|
| 30 |
+
with st.spinner("Thinking..."):
|
| 31 |
+
generated_response = run_llm(query=prompt)
|
| 32 |
+
sources = set(
|
| 33 |
+
[doc.metadata["source"]
|
| 34 |
+
for doc in generated_response["source_documents"]
|
| 35 |
+
]
|
| 36 |
+
)
|
| 37 |
+
formatted_response = (
|
| 38 |
+
f"{generated_response['result']} \n\n Sources: {create_sources_string(sources)}"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Add the user's prompt and the chatbot's response to the session state variables.
|
| 42 |
+
st.session_state.user_prompt_history.append(prompt)
|
| 43 |
+
st.session_state.chat_answers_history.append(formatted_response)
|
| 44 |
+
|
| 45 |
+
if st.session_state.chat_answers_history:
|
| 46 |
+
for generated_response, user_query in zip(
|
| 47 |
+
st.session_state.chat_answers_history, st.session_state.user_prompt_history):
|
| 48 |
+
message(user_query, is_user=True)
|
| 49 |
+
message(generated_response, is_user=False)
|