Spaces:
Runtime error
Runtime error
add configuration options (#13)
Browse files- app.py +38 -13
- requirements.txt +1 -0
- src/agent.py +6 -3
app.py
CHANGED
|
@@ -24,6 +24,11 @@ def set_api_key():
|
|
| 24 |
st.session_state.api_message = gm.api_message(openai.api_key)
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 28 |
|
| 29 |
if "api_message" not in st.session_state:
|
|
@@ -44,16 +49,28 @@ with st.sidebar:
|
|
| 44 |
st.write(
|
| 45 |
"You can find your API key at https://platform.openai.com/account/api-keys"
|
| 46 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Build settings
|
| 49 |
chroma_db = ChromaDB(openai.api_key)
|
| 50 |
openai_client, collection = settings.build(chroma_db)
|
| 51 |
|
| 52 |
-
# Create Agent
|
| 53 |
-
openai_api_key = openai.api_key
|
| 54 |
-
llm = ChatOpenAI(temperature=0.9, model="gpt-3.5-turbo-16k", api_key=openai_api_key)
|
| 55 |
-
agent = PDFExplainer(llm, chroma_db).agent
|
| 56 |
-
|
| 57 |
# Main
|
| 58 |
st.title("PDF Explainer")
|
| 59 |
st.subheader("Create your knowledge base")
|
|
@@ -81,18 +98,26 @@ else:
|
|
| 81 |
st.write("Please upload a file of type: pdf")
|
| 82 |
|
| 83 |
st.subheader("Search on your knowledge base")
|
| 84 |
-
# if st.button("Chroma data collection"):
|
| 85 |
-
# st.write(collection)
|
| 86 |
-
|
| 87 |
-
# if st.button("Delete Chroma Collection"):
|
| 88 |
-
# try:
|
| 89 |
-
# chroma_db.client.delete_collection(collection.name)
|
| 90 |
-
# except AttributeError:
|
| 91 |
-
# st.error("Collection erased.")
|
| 92 |
|
| 93 |
prompt = st.chat_input()
|
| 94 |
|
| 95 |
if prompt:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
st.chat_message("user").write(prompt)
|
| 97 |
with st.chat_message("assistant"):
|
| 98 |
st_callback = StreamlitCallbackHandler(st.container())
|
|
|
|
| 24 |
st.session_state.api_message = gm.api_message(openai.api_key)
|
| 25 |
|
| 26 |
|
| 27 |
+
def click_wk_button():
|
| 28 |
+
"""Set the OpenAI API key."""
|
| 29 |
+
st.session_state.wk_button = not st.session_state.wk_button
|
| 30 |
+
|
| 31 |
+
|
| 32 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 33 |
|
| 34 |
if "api_message" not in st.session_state:
|
|
|
|
| 49 |
st.write(
|
| 50 |
"You can find your API key at https://platform.openai.com/account/api-keys"
|
| 51 |
)
|
| 52 |
+
if "wk_button" not in st.session_state:
|
| 53 |
+
st.session_state.wk_button = False
|
| 54 |
+
|
| 55 |
+
st.checkbox(
|
| 56 |
+
"Use Wikipedia", on_change=click_wk_button, value=st.session_state.wk_button
|
| 57 |
+
)
|
| 58 |
+
st.subheader("Creativity")
|
| 59 |
+
st.write("The higher the value, the crazier the text.")
|
| 60 |
+
st.slider(
|
| 61 |
+
"Temperature",
|
| 62 |
+
min_value=0.0,
|
| 63 |
+
max_value=2.0,
|
| 64 |
+
value=0.9,
|
| 65 |
+
step=0.01,
|
| 66 |
+
key="temperature",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
|
| 70 |
# Build settings
|
| 71 |
chroma_db = ChromaDB(openai.api_key)
|
| 72 |
openai_client, collection = settings.build(chroma_db)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# Main
|
| 75 |
st.title("PDF Explainer")
|
| 76 |
st.subheader("Create your knowledge base")
|
|
|
|
| 98 |
st.write("Please upload a file of type: pdf")
|
| 99 |
|
| 100 |
st.subheader("Search on your knowledge base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
prompt = st.chat_input()
|
| 103 |
|
| 104 |
if prompt:
|
| 105 |
+
# Create Agent
|
| 106 |
+
try:
|
| 107 |
+
openai_api_key = openai.api_key
|
| 108 |
+
llm = ChatOpenAI(
|
| 109 |
+
temperature=st.session_state.temperature,
|
| 110 |
+
model="gpt-3.5-turbo-16k",
|
| 111 |
+
api_key=openai.api_key,
|
| 112 |
+
)
|
| 113 |
+
agent = PDFExplainer(
|
| 114 |
+
llm,
|
| 115 |
+
chroma_db,
|
| 116 |
+
extra_tools=st.session_state.wk_button,
|
| 117 |
+
).agent
|
| 118 |
+
except Exception: # pylint: disable=broad-exception-caught
|
| 119 |
+
st.warning("Missing OpenAI API Key.")
|
| 120 |
+
|
| 121 |
st.chat_message("user").write(prompt)
|
| 122 |
with st.chat_message("assistant"):
|
| 123 |
st_callback = StreamlitCallbackHandler(st.container())
|
requirements.txt
CHANGED
|
@@ -3,6 +3,7 @@ tiktoken
|
|
| 3 |
langchain
|
| 4 |
pymupdf
|
| 5 |
pypdf
|
|
|
|
| 6 |
chromadb>='0.4.18'
|
| 7 |
sentence_transformers
|
| 8 |
streamlit
|
|
|
|
| 3 |
langchain
|
| 4 |
pymupdf
|
| 5 |
pypdf
|
| 6 |
+
duckduckgo-search
|
| 7 |
chromadb>='0.4.18'
|
| 8 |
sentence_transformers
|
| 9 |
streamlit
|
src/agent.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""An Langchain Agent that uses ChromaDB as a query tool"""
|
| 2 |
-
from langchain.agents import AgentType, initialize_agent
|
| 3 |
from langchain.tools import Tool
|
| 4 |
from src.search import Search
|
| 5 |
|
|
@@ -7,7 +7,7 @@ from src.search import Search
|
|
| 7 |
class PDFExplainer:
|
| 8 |
"""An Agent that uses ChromaDB as a query tool"""
|
| 9 |
|
| 10 |
-
def __init__(self, llm, chroma_db):
|
| 11 |
"""Initialize the Agent"""
|
| 12 |
search = Search(chroma_db)
|
| 13 |
|
|
@@ -15,11 +15,14 @@ class PDFExplainer:
|
|
| 15 |
Tool.from_function(
|
| 16 |
func=search.run,
|
| 17 |
name="Search DB",
|
| 18 |
-
description="Useful when you need more context
|
| 19 |
handle_parsing_errors=True,
|
| 20 |
)
|
| 21 |
]
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
self.agent = initialize_agent(
|
| 24 |
self.tools,
|
| 25 |
llm,
|
|
|
|
| 1 |
"""An Langchain Agent that uses ChromaDB as a query tool"""
|
| 2 |
+
from langchain.agents import AgentType, initialize_agent, load_tools
|
| 3 |
from langchain.tools import Tool
|
| 4 |
from src.search import Search
|
| 5 |
|
|
|
|
| 7 |
class PDFExplainer:
|
| 8 |
"""An Agent that uses ChromaDB as a query tool"""
|
| 9 |
|
| 10 |
+
def __init__(self, llm, chroma_db, extra_tools=False):
|
| 11 |
"""Initialize the Agent"""
|
| 12 |
search = Search(chroma_db)
|
| 13 |
|
|
|
|
| 15 |
Tool.from_function(
|
| 16 |
func=search.run,
|
| 17 |
name="Search DB",
|
| 18 |
+
description="Useful when you need more context for answering a question.",
|
| 19 |
handle_parsing_errors=True,
|
| 20 |
)
|
| 21 |
]
|
| 22 |
|
| 23 |
+
if extra_tools:
|
| 24 |
+
self.tools.extend(load_tools(["wikipedia"]))
|
| 25 |
+
|
| 26 |
self.agent = initialize_agent(
|
| 27 |
self.tools,
|
| 28 |
llm,
|