Skip to content

Commit

Permalink
add configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
Maclenn77 committed Dec 9, 2023
1 parent 0c0062a commit 1b2adc8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
51 changes: 38 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def set_api_key():
st.session_state.api_message = gm.api_message(openai.api_key)


def click_wk_button():
"""Set the OpenAI API key."""
st.session_state.wk_button = not st.session_state.wk_button


openai.api_key = os.getenv("OPENAI_API_KEY")

if "api_message" not in st.session_state:
Expand All @@ -44,16 +49,28 @@ def set_api_key():
st.write(
"You can find your API key at https://platform.openai.com/account/api-keys"
)
if "wk_button" not in st.session_state:
st.session_state.wk_button = False

st.checkbox(
"Use Wikipedia", on_change=click_wk_button, value=st.session_state.wk_button
)
st.subheader("Creativity")
st.write("The higher the value, the crazier the text.")
st.slider(
"Temperature",
min_value=0.0,
max_value=2.0,
value=0.9,
step=0.01,
key="temperature",
)


# Build settings
chroma_db = ChromaDB(openai.api_key)
openai_client, collection = settings.build(chroma_db)

# Create Agent
openai_api_key = openai.api_key
llm = ChatOpenAI(temperature=0.9, model="gpt-3.5-turbo-16k", api_key=openai_api_key)
agent = PDFExplainer(llm, chroma_db).agent

# Main
st.title("PDF Explainer")
st.subheader("Create your knowledge base")
Expand Down Expand Up @@ -81,18 +98,26 @@ def set_api_key():
st.write("Please upload a file of type: pdf")

st.subheader("Search on your knowledge base")
# if st.button("Chroma data collection"):
# st.write(collection)

# if st.button("Delete Chroma Collection"):
# try:
# chroma_db.client.delete_collection(collection.name)
# except AttributeError:
# st.error("Collection erased.")

prompt = st.chat_input()

if prompt:
# Create Agent
try:
openai_api_key = openai.api_key
llm = ChatOpenAI(
temperature=st.session_state.temperature,
model="gpt-3.5-turbo-16k",
api_key=openai.api_key,
)
agent = PDFExplainer(
llm,
chroma_db,
extra_tools=st.session_state.wk_button,
).agent
except Exception: # pylint: disable=broad-exception-caught
st.warning("Missing OpenAI API Key.")

st.chat_message("user").write(prompt)
with st.chat_message("assistant"):
st_callback = StreamlitCallbackHandler(st.container())
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ tiktoken
langchain
pymupdf
pypdf
duckduckgo-search
chromadb>='0.4.18'
sentence_transformers
streamlit
9 changes: 6 additions & 3 deletions src/agent.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
"""An Langchain Agent that uses ChromaDB as a query tool"""
from langchain.agents import AgentType, initialize_agent
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.tools import Tool
from src.search import Search


class PDFExplainer:
"""An Agent that uses ChromaDB as a query tool"""

def __init__(self, llm, chroma_db):
def __init__(self, llm, chroma_db, extra_tools=False):
"""Initialize the Agent"""
search = Search(chroma_db)

self.tools = [
Tool.from_function(
func=search.run,
name="Search DB",
description="Useful when you need more context about a specific topic.",
description="Useful when you need more context for answering a question.",
handle_parsing_errors=True,
)
]

if extra_tools:
self.tools.extend(load_tools(["wikipedia"]))

self.agent = initialize_agent(
self.tools,
llm,
Expand Down

0 comments on commit 1b2adc8

Please sign in to comment.