niro commited on
Commit
7f996ec
·
1 Parent(s): 535859d
Files changed (4) hide show
  1. .gitignore +2 -0
  2. Quivr +0 -1
  3. colleen.py +140 -0
  4. question.py +14 -18
.gitignore CHANGED
@@ -155,3 +155,5 @@ cython_debug/
155
 
156
  *.pkl
157
  *.csv
 
 
 
155
 
156
  *.pkl
157
  *.csv
158
+ colleen.py
159
+ assets/Images/colleen-logo.png
Quivr DELETED
@@ -1 +0,0 @@
1
- Subproject commit 3ee69683e0ae09c62e7b13d83f22bfc8606a8aa3
 
 
colleen.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # main.py
2
+ import os
3
+ import tempfile
4
+
5
+ import streamlit as st
6
+ from files import file_uploader, url_uploader
7
+ from question import chat_with_doc
8
+ from brain import brain
9
+ from langchain.embeddings.openai import OpenAIEmbeddings
10
+ from langchain.vectorstores import SupabaseVectorStore
11
+ from supabase import Client, create_client
12
+ from explorer import view_document
13
+ from stats import get_usage_today
14
+
15
+ supabase_url = 'https://ktexmliefragugupzmqw.supabase.co'
16
+ st.secrets.supabase_url = supabase_url
17
+
18
+ supabase_key = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imt0ZXhtbGllZnJhZ3VndXB6bXF3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODUyNjg1MjcsImV4cCI6MjAwMDg0NDUyN30.7DBDCcqelS0GNojPqv0zuvCT5vs5x2Codxyr5cDPZvU'
19
+ st.secrets.supabase_key = supabase_key
20
+
21
+ openai_api_key_head = 'sk-9utMl6JfUfgm4lRIXmK'
22
+ openai_api_key_tail = 'bT3BlbkFJBvNXhwDz9WJrzmi5G6FP'
23
+ openai_api_key = openai_api_key_head+openai_api_key_tail
24
+ st.secrets.opeanai_api_key = openai_api_key
25
+
26
+ st.secrets.anthropic_api_key = ""
27
+ st.secrets.usage_limit = 1000
28
+ anthropic_api_key = ''
29
+ supabase: Client = create_client(supabase_url, supabase_key)
30
+ st.secrets.self_hosted = "true"
31
+ self_hosted = "true"
32
+
33
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
34
+ vector_store = SupabaseVectorStore(
35
+ supabase, embeddings, table_name="documents")
36
+ models = ["gpt-3.5-turbo", "gpt-4"]
37
+ if anthropic_api_key:
38
+ models += ["claude-v1", "claude-v1.3",
39
+ "claude-instant-v1-100k", "claude-instant-v1.1-100k"]
40
+
41
+ # Set the theme
42
+ st.set_page_config(
43
+ page_title="ColleenGPT",
44
+ layout="wide",
45
+ initial_sidebar_state="expanded",
46
+ )
47
+
48
+ st.title("🧠 ColleenGPT")
49
+ st.markdown("ask your ledger anything.")
50
+ if self_hosted == "false":
51
+ 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.**')
52
+
53
+ st.markdown("---\n\n")
54
+
55
+ st.session_state["overused"] = False
56
+ if self_hosted == "false":
57
+ usage = get_usage_today(supabase)
58
+ if usage > st.secrets.usage_limit:
59
+ # if usage > 1000:
60
+ st.markdown(
61
+ 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)
62
+ # f"<span style='color:red'>You have used {usage} tokens today, which is more than your daily limit of {1000} tokens. Please come back later or consider self-hosting.</span>", unsafe_allow_html = True)
63
+
64
+ st.session_state["overused"] = True
65
+ else:
66
+ st.markdown(f"<span style='color:blue'>Usage today: {usage} tokens out of {st.secrets.usage_limit}</span>", unsafe_allow_html=True)
67
+ # st.markdown(f"<span style='color:blue'>Usage today: {usage} tokens out of {1000}</span>", unsafe_allow_html=True)
68
+
69
+ st.write("---")
70
+
71
+
72
+
73
+
74
+ # Initialize session state variables
75
+ if 'model' not in st.session_state:
76
+ st.session_state['model'] = "gpt-3.5-turbo"
77
+ if 'temperature' not in st.session_state:
78
+ st.session_state['temperature'] = 0.0
79
+ if 'chunk_size' not in st.session_state:
80
+ st.session_state['chunk_size'] = 500
81
+ if 'chunk_overlap' not in st.session_state:
82
+ st.session_state['chunk_overlap'] = 0
83
+ if 'max_tokens' not in st.session_state:
84
+ st.session_state['max_tokens'] = 256
85
+
86
+ # Create a radio button for user to choose between adding knowledge or asking a question
87
+ user_choice = st.radio(
88
+ "Choose an action", ('Add Knowledge', 'Chat with your Brain', 'Forget', "Explore"))
89
+
90
+ st.markdown("---\n\n")
91
+ # st.sidebar.image('assets/Images/Vanti - Main Logo@4x copy.png')
92
+ st.sidebar.image('assets/Images/colleen-logo.png')
93
+ if user_choice == 'Add Knowledge':
94
+ # Display chunk size and overlap selection only when adding knowledge
95
+ st.sidebar.title("Configuration")
96
+ st.sidebar.markdown(
97
+ "Choose your chunk size and overlap for adding knowledge.")
98
+ st.session_state['chunk_size'] = st.sidebar.slider(
99
+ "Select Chunk Size", 100, 1000, st.session_state['chunk_size'], 50)
100
+ st.session_state['chunk_overlap'] = st.sidebar.slider(
101
+ "Select Chunk Overlap", 0, 100, st.session_state['chunk_overlap'], 10)
102
+
103
+ # Create two columns for the file uploader and URL uploader
104
+ col1, col2 = st.columns(2)
105
+
106
+ with col1:
107
+ file_uploader(supabase, vector_store)
108
+ with col2:
109
+ url_uploader(supabase, vector_store)
110
+ elif user_choice == 'Chat with your Brain':
111
+ # Display model and temperature selection only when asking questions
112
+ st.sidebar.title("Configuration")
113
+ st.sidebar.markdown(
114
+ "Choose your model and temperature for asking questions.")
115
+ if self_hosted != "false":
116
+ st.session_state['model'] = st.sidebar.selectbox(
117
+ "Select Model", models, index=(models).index(st.session_state['model']))
118
+ else:
119
+ st.sidebar.write("**Model**: gpt-3.5-turbo")
120
+ st.sidebar.write("**Self Host to unlock more models such as claude-v1 and GPT4**")
121
+ st.session_state['model'] = "gpt-3.5-turbo"
122
+ st.session_state['temperature'] = st.sidebar.slider(
123
+ "Select Temperature", 0.0, 1.0, st.session_state['temperature'], 0.1)
124
+ if st.secrets.self_hosted != "false":
125
+ # if "true" != "false":
126
+ st.session_state['max_tokens'] = st.sidebar.slider(
127
+ "Select Max Tokens", 256, 2048, st.session_state['max_tokens'], 2048)
128
+ else:
129
+ st.session_state['max_tokens'] = 256
130
+
131
+ chat_with_doc(st.session_state['model'], vector_store, stats_db=supabase)
132
+ elif user_choice == 'Forget':
133
+ st.sidebar.title("Configuration")
134
+
135
+ brain(supabase)
136
+ elif user_choice == 'Explore':
137
+ st.sidebar.title("Configuration")
138
+ view_document(supabase)
139
+
140
+ st.markdown("---\n\n")
question.py CHANGED
@@ -16,13 +16,9 @@ memory = ConversationBufferMemory(
16
 
17
  openai_api_key_head = 'sk-9utMl6JfUfgm4lRIXmK'
18
  openai_api_key_tail = 'bT3BlbkFJBvNXhwDz9WJrzmi5G6FP'
19
- openai_api_key = openai_api_key_head+openai_api_key_tail
20
  anthropic_api_key = ""
21
  logger = get_logger(__name__)
22
- print('niro','niro', openai_api_key)
23
-
24
-
25
-
26
 
27
 
28
  def count_tokens(question, model):
@@ -33,12 +29,9 @@ def count_tokens(question, model):
33
 
34
 
35
  def chat_with_doc(model, vector_store: SupabaseVectorStore, stats_db):
36
-
37
  if 'chat_history' not in st.session_state:
38
  st.session_state['chat_history'] = []
39
-
40
-
41
-
42
  question = st.text_area("## Ask a question")
43
  columns = st.columns(3)
44
  with columns[0]:
@@ -47,9 +40,7 @@ def chat_with_doc(model, vector_store: SupabaseVectorStore, stats_db):
47
  count_button = st.button("Count Tokens", type='secondary')
48
  with columns[2]:
49
  clear_history = st.button("Clear History", type='secondary')
50
-
51
-
52
-
53
  if clear_history:
54
  # Clear memory in Langchain
55
  memory.clear()
@@ -59,19 +50,24 @@ def chat_with_doc(model, vector_store: SupabaseVectorStore, stats_db):
59
  if button:
60
  qa = None
61
  if not st.session_state["overused"]:
62
- add_usage(stats_db, "chat", "prompt" + question, {"model": model, "temperature": st.session_state['temperature']})
 
63
  if model.startswith("gpt"):
64
  logger.info('Using OpenAI model %s', model)
65
  qa = ConversationalRetrievalChain.from_llm(
66
  OpenAI(
67
- model_name=st.session_state['model'], openai_api_key=openai_api_key, temperature=st.session_state['temperature'], max_tokens=st.session_state['max_tokens']), vector_store.as_retriever(), memory=memory, verbose=True)
 
 
68
  elif anthropic_api_key and model.startswith("claude"):
69
  logger.info('Using Anthropics model %s', model)
70
  qa = ConversationalRetrievalChain.from_llm(
71
  ChatAnthropic(
72
- model=st.session_state['model'], anthropic_api_key=anthropic_api_key, temperature=st.session_state['temperature'], max_tokens_to_sample=st.session_state['max_tokens']), vector_store.as_retriever(), memory=memory, verbose=True, max_tokens_limit=102400)
73
-
74
-
 
 
75
  st.session_state['chat_history'].append(("You", question))
76
 
77
  # Generate model's response and add it to chat history
@@ -86,6 +82,6 @@ def chat_with_doc(model, vector_store: SupabaseVectorStore, stats_db):
86
  st.markdown(f"**{speaker}:** {text}")
87
  else:
88
  st.error("You have used all your free credits. Please try again later or self host.")
89
-
90
  if count_button:
91
  st.write(count_tokens(question, model))
 
16
 
17
  openai_api_key_head = 'sk-9utMl6JfUfgm4lRIXmK'
18
  openai_api_key_tail = 'bT3BlbkFJBvNXhwDz9WJrzmi5G6FP'
19
+ openai_api_key = openai_api_key_head + openai_api_key_tail
20
  anthropic_api_key = ""
21
  logger = get_logger(__name__)
 
 
 
 
22
 
23
 
24
  def count_tokens(question, model):
 
29
 
30
 
31
  def chat_with_doc(model, vector_store: SupabaseVectorStore, stats_db):
 
32
  if 'chat_history' not in st.session_state:
33
  st.session_state['chat_history'] = []
34
+
 
 
35
  question = st.text_area("## Ask a question")
36
  columns = st.columns(3)
37
  with columns[0]:
 
40
  count_button = st.button("Count Tokens", type='secondary')
41
  with columns[2]:
42
  clear_history = st.button("Clear History", type='secondary')
43
+
 
 
44
  if clear_history:
45
  # Clear memory in Langchain
46
  memory.clear()
 
50
  if button:
51
  qa = None
52
  if not st.session_state["overused"]:
53
+ add_usage(stats_db, "chat", "prompt" + question,
54
+ {"model": model, "temperature": st.session_state['temperature']})
55
  if model.startswith("gpt"):
56
  logger.info('Using OpenAI model %s', model)
57
  qa = ConversationalRetrievalChain.from_llm(
58
  OpenAI(
59
+ model_name=st.session_state['model'], openai_api_key=openai_api_key,
60
+ temperature=st.session_state['temperature'], max_tokens=st.session_state['max_tokens']),
61
+ vector_store.as_retriever(), memory=memory, verbose=True)
62
  elif anthropic_api_key and model.startswith("claude"):
63
  logger.info('Using Anthropics model %s', model)
64
  qa = ConversationalRetrievalChain.from_llm(
65
  ChatAnthropic(
66
+ model=st.session_state['model'], anthropic_api_key=anthropic_api_key,
67
+ temperature=st.session_state['temperature'],
68
+ max_tokens_to_sample=st.session_state['max_tokens']), vector_store.as_retriever(),
69
+ memory=memory, verbose=True, max_tokens_limit=102400)
70
+
71
  st.session_state['chat_history'].append(("You", question))
72
 
73
  # Generate model's response and add it to chat history
 
82
  st.markdown(f"**{speaker}:** {text}")
83
  else:
84
  st.error("You have used all your free credits. Please try again later or self host.")
85
+
86
  if count_button:
87
  st.write(count_tokens(question, model))