Spaces:
Runtime error
Runtime error
KnowledgeGPT works with Azure OpenAI
Browse files- knowledge_gpt/components/sidebar.py +2 -0
- knowledge_gpt/main.py +37 -22
- knowledge_gpt/ui.py +7 -6
knowledge_gpt/components/sidebar.py
CHANGED
|
@@ -15,6 +15,7 @@ def sidebar():
|
|
| 15 |
"2. Upload a pdf, docx, or txt file📄\n"
|
| 16 |
"3. Ask a question about the document💬\n"
|
| 17 |
)
|
|
|
|
| 18 |
api_key_input = st.text_input(
|
| 19 |
"OpenAI API Key",
|
| 20 |
type="password",
|
|
@@ -25,6 +26,7 @@ def sidebar():
|
|
| 25 |
)
|
| 26 |
|
| 27 |
st.session_state["OPENAI_API_KEY"] = api_key_input
|
|
|
|
| 28 |
|
| 29 |
st.markdown("---")
|
| 30 |
st.markdown("# About")
|
|
|
|
| 15 |
"2. Upload a pdf, docx, or txt file📄\n"
|
| 16 |
"3. Ask a question about the document💬\n"
|
| 17 |
)
|
| 18 |
+
"""
|
| 19 |
api_key_input = st.text_input(
|
| 20 |
"OpenAI API Key",
|
| 21 |
type="password",
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
st.session_state["OPENAI_API_KEY"] = api_key_input
|
| 29 |
+
"""
|
| 30 |
|
| 31 |
st.markdown("---")
|
| 32 |
st.markdown("# About")
|
knowledge_gpt/main.py
CHANGED
|
@@ -1,14 +1,24 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from knowledge_gpt.components.sidebar import sidebar
|
| 6 |
|
| 7 |
from knowledge_gpt.ui import (
|
| 8 |
wrap_doc_in_html,
|
| 9 |
is_query_valid,
|
| 10 |
is_file_valid,
|
| 11 |
-
is_open_ai_key_valid,
|
| 12 |
display_file_read_error,
|
| 13 |
)
|
| 14 |
|
|
@@ -20,14 +30,7 @@ from knowledge_gpt.core.embedding import embed_files
|
|
| 20 |
from knowledge_gpt.core.qa import query_folder
|
| 21 |
from knowledge_gpt.core.utils import get_llm
|
| 22 |
|
| 23 |
-
|
| 24 |
-
# add all secrets into environmental variables
|
| 25 |
-
try:
|
| 26 |
-
for key, value in st.secrets.items():
|
| 27 |
-
os.environ[key] = value
|
| 28 |
-
except FileNotFoundError as e:
|
| 29 |
-
print(e)
|
| 30 |
-
print("./streamlit/secrets.toml not found. Assuming secrets are already available" "as environmental variables...")
|
| 31 |
|
| 32 |
|
| 33 |
def main():
|
|
@@ -38,8 +41,8 @@ def main():
|
|
| 38 |
# Uncomment to enable debug mode
|
| 39 |
# MODEL_LIST.insert(0, "debug")
|
| 40 |
|
| 41 |
-
st.set_page_config(page_title="
|
| 42 |
-
st.header("📖
|
| 43 |
|
| 44 |
# Enable caching for expensive functions
|
| 45 |
bootstrap_caching()
|
|
@@ -79,15 +82,17 @@ def main():
|
|
| 79 |
if not is_file_valid(file):
|
| 80 |
st.stop()
|
| 81 |
|
| 82 |
-
if not is_open_ai_key_valid(openai_api_key, model):
|
| 83 |
-
st.stop()
|
| 84 |
-
|
| 85 |
with st.spinner("Indexing document... This may take a while⏳"):
|
| 86 |
folder_index = embed_files(
|
| 87 |
files=[chunked_file],
|
| 88 |
embedding=EMBEDDING if model != "debug" else "debug",
|
| 89 |
vector_store=VECTOR_STORE if model != "debug" else "debug",
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
with st.form(key="qa_form"):
|
|
@@ -106,13 +111,23 @@ def main():
|
|
| 106 |
# Output Columns
|
| 107 |
answer_col, sources_col = st.columns(2)
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
with answer_col:
|
| 118 |
st.markdown("#### Answer")
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["OPENAI_API_TYPE"] = "azure" # configure API to Azure OpenAI
|
| 3 |
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
# add all secrets into environmental variables
|
| 7 |
+
try:
|
| 8 |
+
for key, value in st.secrets.items():
|
| 9 |
+
# os.environ[key] = value
|
| 10 |
+
st.session_state[key] = value
|
| 11 |
+
except FileNotFoundError as e:
|
| 12 |
+
print(e)
|
| 13 |
+
print("./streamlit/secrets.toml not found. Assuming secrets are already available" "as environmental variables...")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
from knowledge_gpt.components.sidebar import sidebar
|
| 17 |
|
| 18 |
from knowledge_gpt.ui import (
|
| 19 |
wrap_doc_in_html,
|
| 20 |
is_query_valid,
|
| 21 |
is_file_valid,
|
|
|
|
| 22 |
display_file_read_error,
|
| 23 |
)
|
| 24 |
|
|
|
|
| 30 |
from knowledge_gpt.core.qa import query_folder
|
| 31 |
from knowledge_gpt.core.utils import get_llm
|
| 32 |
|
| 33 |
+
from langchain.chat_models import AzureChatOpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def main():
|
|
|
|
| 41 |
# Uncomment to enable debug mode
|
| 42 |
# MODEL_LIST.insert(0, "debug")
|
| 43 |
|
| 44 |
+
st.set_page_config(page_title="ReferenceBot", page_icon="📖", layout="wide")
|
| 45 |
+
st.header("📖ReferenceBot")
|
| 46 |
|
| 47 |
# Enable caching for expensive functions
|
| 48 |
bootstrap_caching()
|
|
|
|
| 82 |
if not is_file_valid(file):
|
| 83 |
st.stop()
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
with st.spinner("Indexing document... This may take a while⏳"):
|
| 86 |
folder_index = embed_files(
|
| 87 |
files=[chunked_file],
|
| 88 |
embedding=EMBEDDING if model != "debug" else "debug",
|
| 89 |
vector_store=VECTOR_STORE if model != "debug" else "debug",
|
| 90 |
+
deployment=st.secrets["ENGINE_EMBEDDING"],
|
| 91 |
+
model=st.secrets["ENGINE"],
|
| 92 |
+
openai_api_key=st.secrets["OPENAI_API_KEY"],
|
| 93 |
+
openai_api_base=st.secrets["OPENAI_API_BASE"],
|
| 94 |
+
openai_api_type="azure",
|
| 95 |
+
chunk_size = 1,
|
| 96 |
)
|
| 97 |
|
| 98 |
with st.form(key="qa_form"):
|
|
|
|
| 111 |
# Output Columns
|
| 112 |
answer_col, sources_col = st.columns(2)
|
| 113 |
|
| 114 |
+
with st.spinner("Setting up AzureChatOpenAI bot..."):
|
| 115 |
+
llm = AzureChatOpenAI(
|
| 116 |
+
openai_api_base=st.secrets["OPENAI_API_BASE"],
|
| 117 |
+
openai_api_version=st.secrets["OPENAI_API_VERSION"],
|
| 118 |
+
deployment_name=st.secrets["ENGINE"],
|
| 119 |
+
openai_api_key=st.secrets["OPENAI_API_KEY"],
|
| 120 |
+
openai_api_type="azure",
|
| 121 |
+
temperature=0,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
with st.spinner("Querying folder to get result..."):
|
| 125 |
+
result = query_folder(
|
| 126 |
+
folder_index=folder_index,
|
| 127 |
+
query=query,
|
| 128 |
+
return_all=return_all_chunks,
|
| 129 |
+
llm=llm,
|
| 130 |
+
)
|
| 131 |
|
| 132 |
with answer_col:
|
| 133 |
st.markdown("#### Answer")
|
knowledge_gpt/ui.py
CHANGED
|
@@ -2,7 +2,7 @@ from typing import List
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.docstore.document import Document
|
| 4 |
from knowledge_gpt.core.parsing import File
|
| 5 |
-
import openai
|
| 6 |
from streamlit.logger import get_logger
|
| 7 |
from typing import NoReturn
|
| 8 |
|
|
@@ -51,11 +51,12 @@ def is_open_ai_key_valid(openai_api_key, model: str) -> bool:
|
|
| 51 |
st.error("Please enter your OpenAI API key in the sidebar!")
|
| 52 |
return False
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
st.error(f"{e.__class__.__name__}: {e}")
|
| 61 |
logger.error(f"{e.__class__.__name__}: {e}")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.docstore.document import Document
|
| 4 |
from knowledge_gpt.core.parsing import File
|
| 5 |
+
#import openai
|
| 6 |
from streamlit.logger import get_logger
|
| 7 |
from typing import NoReturn
|
| 8 |
|
|
|
|
| 51 |
st.error("Please enter your OpenAI API key in the sidebar!")
|
| 52 |
return False
|
| 53 |
try:
|
| 54 |
+
pass
|
| 55 |
+
#openai.ChatCompletion.create(
|
| 56 |
+
# engine="chatbot-streamlit",#model,
|
| 57 |
+
# messages=[{"role": "user", "content": "test"}],
|
| 58 |
+
# #api_key=openai_api_key,
|
| 59 |
+
#)
|
| 60 |
except Exception as e:
|
| 61 |
st.error(f"{e.__class__.__name__}: {e}")
|
| 62 |
logger.error(f"{e.__class__.__name__}: {e}")
|