maclenn77 commited on
Commit
4b20003
·
unverified ·
1 Parent(s): 9dddd1e

Feat: User can add own api key (#7)

Browse files
Files changed (1) hide show
  1. app.py +38 -19
app.py CHANGED
@@ -13,24 +13,45 @@ from openai import OpenAI
13
 
14
  load_dotenv()
15
 
16
- if os.getenv("OPENAI_API_KEY") is None:
17
- st.error("Please set OPENAI_API_KEY environment variable")
18
- st.stop()
19
- else:
20
- openai.api_key = os.getenv("OPENAI_API_KEY")
21
-
22
- client = OpenAI()
23
- embedding_function = OpenAIEmbeddingFunction(
24
- api_key=openai.api_key, model_name="text-embedding-ada-002"
25
- )
26
- # from openai import OpenAI
27
-
28
  chroma_client = chromadb.PersistentClient(path="tmp/chroma")
29
  chroma_client.heartbeat()
30
 
31
- collection = chroma_client.get_or_create_collection(
32
- name="pdf-explainer", embedding_function=embedding_function
33
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # Query ChromaDb
36
  query = st.text_input("Query ChromaDb", value="", placeholder="Enter query")
@@ -42,7 +63,7 @@ if st.button("Search"):
42
 
43
  for idx, result in enumerate(results["documents"][0]):
44
  st.markdown(
45
- result[0:150]
46
  + "..."
47
  + "**Source:** "
48
  + results["metadatas"][0][idx]["source"]
@@ -65,7 +86,7 @@ if pdf is not None:
65
  )
66
  if st.button("Save chunks"):
67
  with st.spinner("Saving chunks..."):
68
- chunks = textwrap.wrap(text, 24000)
69
  for idx, chunk in enumerate(chunks):
70
  encoding = tiktoken.get_encoding("cl100k_base")
71
  num_tokens = len(encoding.encode(chunk))
@@ -85,7 +106,6 @@ if pdf is not None:
85
  else:
86
  st.write("Please upload a file of type: pdf")
87
 
88
-
89
  if st.button("Chroma data collection"):
90
  st.write(collection)
91
 
@@ -94,4 +114,3 @@ if st.button("Delete Chroma Collection"):
94
  chroma_client.delete_collection(collection.name)
95
  except AttributeError:
96
  st.error("Collection erased.")
97
-
 
13
 
14
  load_dotenv()
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  chroma_client = chromadb.PersistentClient(path="tmp/chroma")
17
  chroma_client.heartbeat()
18
 
19
+
20
+ def set_api_key():
21
+ """Set the OpenAI API key."""
22
+ openai.api_key = st.session_state.api_key
23
+ st.write("Your API key is setup ")
24
+
25
+
26
+ openai.api_key = os.getenv("OPENAI_API_KEY")
27
+
28
+ if os.getenv("OPENAI_API_KEY") is None:
29
+ st.warning("Add your OpenAI API key")
30
+ openai.api_key = st.text_input(
31
+ "Enter your OpenAI API key",
32
+ value="",
33
+ type="password",
34
+ key="api_key",
35
+ on_change=set_api_key,
36
+ label_visibility="collapsed",
37
+ )
38
+ st.write("You can find your API key at https://beta.openai.com/account/api-keys")
39
+ client = OpenAI(api_key=openai.api_key)
40
+ embedding_function = OpenAIEmbeddingFunction(
41
+ api_key=openai.api_key, model_name="text-embedding-ada-002"
42
+ )
43
+ collection = chroma_client.get_or_create_collection(
44
+ name="pdf-explainer", embedding_function=embedding_function
45
+ )
46
+ else:
47
+ client = OpenAI()
48
+ embedding_function = OpenAIEmbeddingFunction(
49
+ api_key=openai.api_key, model_name="text-embedding-ada-002"
50
+ )
51
+ collection = chroma_client.get_or_create_collection(
52
+ name="pdf-explainer", embedding_function=embedding_function
53
+ )
54
+
55
 
56
  # Query ChromaDb
57
  query = st.text_input("Query ChromaDb", value="", placeholder="Enter query")
 
63
 
64
  for idx, result in enumerate(results["documents"][0]):
65
  st.markdown(
66
+ result
67
  + "..."
68
  + "**Source:** "
69
  + results["metadatas"][0][idx]["source"]
 
86
  )
87
  if st.button("Save chunks"):
88
  with st.spinner("Saving chunks..."):
89
+ chunks = textwrap.wrap(text, 3000)
90
  for idx, chunk in enumerate(chunks):
91
  encoding = tiktoken.get_encoding("cl100k_base")
92
  num_tokens = len(encoding.encode(chunk))
 
106
  else:
107
  st.write("Please upload a file of type: pdf")
108
 
 
109
  if st.button("Chroma data collection"):
110
  st.write(collection)
111
 
 
114
  chroma_client.delete_collection(collection.name)
115
  except AttributeError:
116
  st.error("Collection erased.")