feat(visual): moved things around

This commit is contained in:
Stan Girard 2023-05-12 23:58:19 +02:00
parent 4174f0693a
commit 5d66cd5223
5 changed files with 39 additions and 36 deletions

View File

@ -1,4 +1,5 @@
import streamlit as st import streamlit as st
import os
from loaders.audio import process_audio from loaders.audio import process_audio
from loaders.txt import process_txt from loaders.txt import process_txt
from loaders.csv import process_csv from loaders.csv import process_csv

View File

@ -1,6 +1,7 @@
import tempfile import tempfile
from utils import compute_sha1_from_file from utils import compute_sha1_from_file
from langchain.schema import Document from langchain.schema import Document
import streamlit as st
from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.text_splitter import RecursiveCharacterTextSplitter
def process_file(vector_store, file, loader_class, file_suffix): def process_file(vector_store, file, loader_class, file_suffix):

42
main.py
View File

@ -1,16 +1,14 @@
# main.py
import os import os
import tempfile import tempfile
import streamlit as st import streamlit as st
from sidebar import sidebar
from files import file_uploader from files import file_uploader
from question import chat_with_doc from question import chat_with_doc
from langchain.embeddings.openai import OpenAIEmbeddings from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import SupabaseVectorStore from langchain.vectorstores import SupabaseVectorStore
from supabase import Client, create_client from supabase import Client, create_client
# supabase_url = "https://fqgpcifsfmamprzldyiv.supabase.co" # supabase_url = "https://fqgpcifsfmamprzldyiv.supabase.co"
supabase_url = st.secrets.supabase_url supabase_url = st.secrets.supabase_url
supabase_key = st.secrets.supabase_service_key supabase_key = st.secrets.supabase_service_key
@ -20,16 +18,44 @@ supabase: Client = create_client(supabase_url, supabase_key)
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
vector_store = SupabaseVectorStore(supabase, embeddings, table_name="documents") vector_store = SupabaseVectorStore(supabase, embeddings, table_name="documents")
# Set the theme
st.set_page_config(
page_title="Second Brain",
layout="wide",
initial_sidebar_state="expanded",
)
st.title("🧠 Second Brain 🧠") st.title("🧠 Second Brain 🧠")
st.markdown("Store your knowledge in a vector store and query it with OpenAI's GPT-3/4.") st.markdown("Store your knowledge in a vector store and query it with OpenAI's GPT-3/4.")
st.markdown("---\n\n") st.markdown("---\n\n")
# Initialize session state variables
if 'model' not in st.session_state:
st.session_state['model'] = "gpt-3.5-turbo"
if 'temperature' not in st.session_state:
st.session_state['temperature'] = 0.0
if 'chunk_size' not in st.session_state:
st.session_state['chunk_size'] = 500
if 'chunk_overlap' not in st.session_state:
st.session_state['chunk_overlap'] = 0
# Create a radio button for user to choose between adding knowledge or asking a question
user_choice = st.radio("Choose an action", ('Add Knowledge to the Brain', 'Ask a Question to the Brain'))
sidebar(supabase) if user_choice == 'Add Knowledge to the Brain':
file_uploader(supabase,openai_api_key, vector_store) # Display chunk size and overlap selection only when adding knowledge
st.markdown("---\n\n") st.sidebar.title("Configuration")
chat_with_doc(openai_api_key, vector_store) st.sidebar.markdown("Choose your chunk size and overlap for adding knowledge.")
st.session_state['chunk_size'] = st.sidebar.slider("Select Chunk Size", 100, 1000, st.session_state['chunk_size'], 50)
st.session_state['chunk_overlap'] = st.sidebar.slider("Select Chunk Overlap", 0, 100, st.session_state['chunk_overlap'], 10)
file_uploader(supabase, openai_api_key, vector_store)
elif user_choice == 'Ask a Question to the Brain':
# Display model and temperature selection only when asking questions
st.sidebar.title("Configuration")
st.sidebar.markdown("Choose your model and temperature for asking questions.")
st.session_state['model'] = st.sidebar.selectbox("Select Model", ["gpt-3.5-turbo", "gpt-4"], index=("gpt-3.5-turbo", "gpt-4").index(st.session_state['model']))
st.session_state['temperature'] = st.sidebar.slider("Select Temperature", 0.0, 1.0, st.session_state['temperature'], 0.1)
chat_with_doc(openai_api_key, vector_store)
st.markdown("---\n\n")

View File

@ -3,16 +3,12 @@ from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI from langchain.llms import OpenAI
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
def chat_with_doc(openai_api_key, vector_store):
def chat_with_doc(openai_api_key,vector_store):
question = st.text_input("## Ask a question") question = st.text_input("## Ask a question")
temperature = st.session_state.get("temperature", 0.0)
model = st.session_state.get("model", "gpt-3.5-turbo")
button = st.button("Ask") button = st.button("Ask")
if button: if button:
qa = ConversationalRetrievalChain.from_llm(OpenAI(model_name=model, openai_api_key=openai_api_key, temperature=temperature), vector_store.as_retriever(), memory=memory) qa = ConversationalRetrievalChain.from_llm(OpenAI(model_name=st.session_state['model'], openai_api_key=openai_api_key, temperature=st.session_state['temperature']), vector_store.as_retriever(), memory=memory)
result = qa({"question": question}) result = qa({"question": question})
st.write(result["answer"]) st.write(result["answer"])

View File

@ -2,31 +2,10 @@ import streamlit as st
def sidebar(supabase): def sidebar(supabase):
st.sidebar.title("Configuration") st.sidebar.title("Database Information")
## Get the number of documents in the database
number_of_docs = number_of_documents(supabase) number_of_docs = number_of_documents(supabase)
## Display the number of documents in the database
st.sidebar.markdown(f"**Docs in DB:** {number_of_docs}") st.sidebar.markdown(f"**Docs in DB:** {number_of_docs}")
## Allow user to choose model between gpt-3.5-turbo and gpt-4
model = st.sidebar.selectbox("Select Model", ["gpt-3.5-turbo", "gpt-4"])
## Allow user to choose temperature between 0.0 and 1.0
temperature = st.sidebar.slider("Select Temperature", 0.0, 1.0, 0.0, 0.1)
## Allow user to choose chunk_size between 100 and 1000
chunk_size = st.sidebar.slider("Select Chunk Size", 100, 1000, 500, 50)
## Allow user to choose chunk_overlap between 0 and 100
chunk_overlap = st.sidebar.slider("Select Chunk Overlap", 0, 100, 0, 10)
## Save the user's choices
st.session_state.model = model
st.session_state.temperature = temperature
st.session_state.chunk_size = chunk_size
st.session_state.chunk_overlap = chunk_overlap
def number_of_documents(supabase): def number_of_documents(supabase):
## Get the number of documents in the database
documents = supabase.table("documents").select("id", count="exact").execute() documents = supabase.table("documents").select("id", count="exact").execute()
return documents.count return documents.count