2023-05-13 00:58:19 +03:00
# main.py
2023-05-13 00:05:31 +03:00
import os
import tempfile
import streamlit as st
2023-05-16 02:02:52 +03:00
from files import file_uploader , url_uploader
2023-05-13 00:05:31 +03:00
from question import chat_with_doc
2023-05-13 02:12:51 +03:00
from brain import brain
2023-05-13 00:05:31 +03:00
from langchain . embeddings . openai import OpenAIEmbeddings
from langchain . vectorstores import SupabaseVectorStore
from supabase import Client , create_client
2023-05-16 18:04:45 +03:00
from explorer import view_document
2023-05-17 13:12:52 +03:00
from stats import get_usage_today
2023-05-13 00:05:31 +03:00
supabase_url = st . secrets . supabase_url
2023-05-13 00:22:21 +03:00
supabase_key = st . secrets . supabase_service_key
2023-05-13 00:05:31 +03:00
openai_api_key = st . secrets . openai_api_key
2023-05-14 11:30:03 +03:00
anthropic_api_key = st . secrets . anthropic_api_key
2023-05-13 00:05:31 +03:00
supabase : Client = create_client ( supabase_url , supabase_key )
2023-05-17 13:12:52 +03:00
self_hosted = st . secrets . self_hosted
2023-05-13 00:05:31 +03:00
embeddings = OpenAIEmbeddings ( openai_api_key = openai_api_key )
2023-05-14 11:30:03 +03:00
vector_store = SupabaseVectorStore (
supabase , embeddings , table_name = " documents " )
models = [ " gpt-3.5-turbo " , " gpt-4 " ]
if anthropic_api_key :
models + = [ " claude-v1 " , " claude-v1.3 " ,
" claude-instant-v1-100k " , " claude-instant-v1.1-100k " ]
2023-05-13 00:05:31 +03:00
2023-05-13 00:58:19 +03:00
# Set the theme
st . set_page_config (
2023-05-14 22:46:31 +03:00
page_title = " Quivr " ,
2023-05-13 00:58:19 +03:00
layout = " wide " ,
initial_sidebar_state = " expanded " ,
)
2023-05-13 00:05:31 +03:00
2023-05-14 13:40:37 +03:00
2023-05-14 22:46:31 +03:00
st . title ( " 🧠 Quivr - Your second brain 🧠 " )
2023-05-13 00:05:31 +03:00
st . markdown ( " Store your knowledge in a vector store and query it with OpenAI ' s GPT-3/4. " )
2023-05-17 13:53:36 +03:00
if self_hosted == " false " :
st . markdown ( ' **📢 Note: In the public demo, access to functionality is restricted. You can only use the GPT-3.5-turbo model and upload files up to 1Mb. To use more models and upload larger files, consider self-hosting Quivr.** ' )
2023-05-13 00:05:31 +03:00
st . markdown ( " --- \n \n " )
2023-05-17 13:12:52 +03:00
st . session_state [ " overused " ] = False
if self_hosted == " false " :
usage = get_usage_today ( supabase )
if usage > st . secrets . usage_limit :
2023-05-17 13:53:36 +03:00
st . markdown (
f " <span style= ' color:red ' >You have used { usage } tokens today, which is more than your daily limit of { st . secrets . usage_limit } tokens. Please come back later or consider self-hosting.</span> " , unsafe_allow_html = True )
2023-05-17 13:12:52 +03:00
st . session_state [ " overused " ] = True
2023-05-17 13:53:36 +03:00
else :
st . markdown ( f " <span style= ' color:blue ' >Usage today: { usage } tokens out of { st . secrets . usage_limit } </span> " , unsafe_allow_html = True )
st . write ( " --- " )
2023-05-17 13:12:52 +03:00
2023-05-13 00:58:19 +03:00
# 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
2023-05-14 11:30:03 +03:00
if ' max_tokens ' not in st . session_state :
st . session_state [ ' max_tokens ' ] = 256
2023-05-13 00:58:19 +03:00
# Create a radio button for user to choose between adding knowledge or asking a question
2023-05-14 11:30:03 +03:00
user_choice = st . radio (
2023-05-16 18:04:45 +03:00
" Choose an action " , ( ' Add Knowledge ' , ' Chat with your Brain ' , ' Forget ' , " Explore " ) )
2023-05-13 00:58:19 +03:00
2023-05-13 01:25:12 +03:00
st . markdown ( " --- \n \n " )
2023-05-13 02:30:00 +03:00
if user_choice == ' Add Knowledge ' :
2023-05-13 00:58:19 +03:00
# Display chunk size and overlap selection only when adding knowledge
2023-05-14 11:30:03 +03:00
st . sidebar . title ( " Configuration " )
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 )
2023-05-16 11:17:57 +03:00
# Create two columns for the file uploader and URL uploader
col1 , col2 = st . columns ( 2 )
with col1 :
2023-05-20 00:13:46 +03:00
file_uploader ( supabase , vector_store )
2023-05-16 11:17:57 +03:00
with col2 :
2023-05-20 00:13:46 +03:00
url_uploader ( supabase , vector_store )
2023-05-13 02:30:00 +03:00
elif user_choice == ' Chat with your Brain ' :
2023-05-13 00:58:19 +03:00
# Display model and temperature selection only when asking questions
2023-05-14 11:30:03 +03:00
st . sidebar . title ( " Configuration " )
st . sidebar . markdown (
" Choose your model and temperature for asking questions. " )
2023-05-20 00:13:46 +03:00
if self_hosted != " false " :
2023-05-17 13:12:52 +03:00
st . session_state [ ' model ' ] = st . sidebar . selectbox (
2023-05-14 11:30:03 +03:00
" Select Model " , models , index = ( models ) . index ( st . session_state [ ' model ' ] ) )
2023-05-17 13:12:52 +03:00
else :
st . sidebar . write ( " **Model**: gpt-3.5-turbo " )
st . sidebar . write ( " **Self Host to unlock more models such as claude-v1 and GPT4** " )
st . session_state [ ' model ' ] = " gpt-3.5-turbo "
2023-05-14 11:30:03 +03:00
st . session_state [ ' temperature ' ] = st . sidebar . slider (
" Select Temperature " , 0.0 , 1.0 , st . session_state [ ' temperature ' ] , 0.1 )
2023-05-17 13:12:52 +03:00
if st . secrets . self_hosted != " false " :
st . session_state [ ' max_tokens ' ] = st . sidebar . slider (
" Select Max Tokens " , 256 , 2048 , st . session_state [ ' max_tokens ' ] , 2048 )
else :
2023-05-17 13:53:36 +03:00
st . session_state [ ' max_tokens ' ] = 256
2023-05-17 13:12:52 +03:00
chat_with_doc ( st . session_state [ ' model ' ] , vector_store , stats_db = supabase )
2023-05-13 02:30:00 +03:00
elif user_choice == ' Forget ' :
st . sidebar . title ( " Configuration " )
2023-05-14 11:30:03 +03:00
2023-05-13 02:30:00 +03:00
brain ( supabase )
2023-05-16 18:04:45 +03:00
elif user_choice == ' Explore ' :
st . sidebar . title ( " Configuration " )
view_document ( supabase )
2023-05-13 00:58:19 +03:00
2023-05-20 00:13:46 +03:00
st . markdown ( " --- \n \n " )