Skip to content

Commit

Permalink
Fix OpenAI for OnPrem Test Framework (#30)
Browse files Browse the repository at this point in the history
* bump versions, conditional API button
* bump llama_index
* add cohere
* Fix OPEN_API_KEY issue on testing framework
  • Loading branch information
gotsysdba authored Oct 16, 2024
1 parent 009813b commit 53f2ef1
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 54 deletions.
16 changes: 8 additions & 8 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
### pip3 install -r app/requirements.txt

## Top-Level brings in the required dependencies, if adding modules, try to find the minimum required
giskard==2.15.1
bokeh==3.6.0
evaluate==0.4.3
faiss-cpu==1.8.0.post1
IPython==8.27.0
langchain-cohere==0.3.0
langchain-community==0.3.1
faiss-cpu==1.9.0
giskard==2.15.2
IPython==8.28.0
langchain-cohere==0.3.1
langchain-community==0.3.2
langchain-huggingface==0.1.0
langchain-ollama==0.2.0
langchain-openai==0.2.1
llama_index==0.11.14
langchain-openai==0.2.2
llama_index==0.11.18
lxml==5.3.0
matplotlib==3.9.2
oci>=2.0.0
oracledb>=2.0.0
plotly==5.24.1
streamlit==1.38.0
streamlit==1.39.0
umap-learn==0.5.6

## For Licensing Purposes; ensures no GPU modules are installed
Expand Down
7 changes: 4 additions & 3 deletions app/src/content/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def main():
}
</style>
"""

st.markdown(css, unsafe_allow_html=True)
st.title("Test Framework")
# Used to clear the uploader files
Expand Down Expand Up @@ -232,7 +233,7 @@ def main():
if qa_gen_file:
qa_gen_button_disabled = False

left, right = st.columns(2, vertical_alignment="bottom")
left, right = st.columns([0.15, 0.85])
if left.button("Generate Q&A", type="primary", key="qa_generation", disabled=qa_gen_button_disabled):
placeholder = st.empty()
with placeholder:
Expand All @@ -255,7 +256,7 @@ def main():
# Generate Q&A
qa_file = os.path.join(state["temp_dir"], f"{file_name}_{str(qa_count)}_test_set.json")
state.qa_file = qa_file
state.test_set = utilities.generate_qa(qa_file, kb, qa_count)
state.test_set = utilities.generate_qa(qa_file, kb, qa_count, model=qa_llm, client=llm_client)
placeholder.empty()
st.success("Q&A Generation Succeeded.", icon="✅")
right.button("Reset", key="reset_test_framework", type="primary", on_click=reset_test_set)
Expand All @@ -274,7 +275,7 @@ def main():
if len(test_set_file) > 0:
test_set_button_disabled = False

left, right = st.columns(2, vertical_alignment="bottom")
left, right = st.columns([0.15, 0.85], vertical_alignment="bottom")
if left.button("Load Tests", key="load_tests", disabled=test_set_button_disabled):
placeholder = st.empty()
with placeholder:
Expand Down
13 changes: 7 additions & 6 deletions app/src/modules/chatbot_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ def gui_start():
# ChatBot Sidebar
###################################
def chatbot_sidebar():
st.session_state["port"] = st.sidebar.number_input(
"Enter the port number for the chatbot server:", value=8000, min_value=1, max_value=65535
)
st.session_state["api_key"] = st.sidebar.text_input("API_KEY", type="password", value="abc")
st.sidebar.button("Start server", type="primary", on_click=gui_start)
st.sidebar.divider()
if not state.disable_api:
st.session_state["port"] = st.sidebar.number_input(
"Enter the port number for the chatbot server:", value=8000, min_value=1, max_value=65535
)
st.session_state["api_key"] = st.sidebar.text_input("API_KEY", type="password", value="abc")
st.sidebar.button("Start server", type="primary", on_click=gui_start)
st.sidebar.divider()
3 changes: 2 additions & 1 deletion app/src/modules/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def setup_logging():
logging.getLogger("PIL").setLevel(logging.INFO)
logging.getLogger("openai").setLevel(logging_level)
logging.getLogger("httpcore").setLevel(logging_level)

# Sagemaker continuously complains about config, suppress
logging.getLogger("sagemaker.config").setLevel(logging.WARNING)

# Call setup_logging when this module is imported
setup_logging()
11 changes: 8 additions & 3 deletions app/src/modules/st_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ def empty_key(obj):
state_dict_filt = {key: state_dict[key] for key in include_keys if key in state_dict}
state_dict_filt = empty_key(state_dict_filt)
session_state_json = json.dumps(state_dict_filt, indent=4)
st.sidebar.download_button(
label="Download Settings", data=session_state_json, file_name="sandbox_settings.json", use_container_width=True
)
# Only allow exporting settings if tools/admin is enabled
if not state.disable_tools and not state.disable_admin:
st.sidebar.download_button(
label="Download Settings",
data=session_state_json,
file_name="sandbox_settings.json",
use_container_width=True,
)
10 changes: 5 additions & 5 deletions app/src/modules/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def populate_vs(
model_name,
distance_metric,
input_data: Union[List["LangchainDocument"], List] = None,
rate_limit = 0
rate_limit=0,
):
"""Populate the Vector Storage"""

Expand Down Expand Up @@ -350,7 +350,7 @@ def json_to_doc(file: str):
"Processing: %i Chunks of %i (Rate Limit: %i)",
len(unique_chunks) if len(unique_chunks) < i + batch_size else i + batch_size,
len(unique_chunks),
rate_limit
rate_limit,
)
OracleVS.add_documents(vectorstore, documents=batch)
if rate_limit > 0:
Expand Down Expand Up @@ -700,11 +700,11 @@ def build_knowledge_base(text_nodes, kb_file, llm_client, embed_client):
return knowledge_base


def generate_qa(qa_file, kb, qa_count, api="openai", model="gpt-4o-mini"):
def generate_qa(qa_file, kb, qa_count, api="openai", model="gpt-4o-mini", client=None):
"""Generate an example QA"""
logger.info("QA Generation starting..")
logger.info("QA Generation starting.. (model=%s, client=%s)", model, client)
set_llm_api(api)
set_default_client(OpenAIClient(model=model))
set_default_client(OpenAIClient(model=model, client=client))

test_set = generate_testset(
kb,
Expand Down
15 changes: 9 additions & 6 deletions app/src/oaim-sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
os.environ["GSK_DISABLE_SENTRY"] = "true"
os.environ["GSK_DISABLE_ANALYTICS"] = "true"


#############################################################################
# MAIN
#############################################################################
def main():
"""Streamlit GUI"""
st.set_page_config(layout="wide")
# initialize Components
db_initialize()
model_initialize()
Expand All @@ -48,7 +50,6 @@ def main():
# GUI Defaults
css = """
<style>
section.main > div {max-width:65rem; padding-top: 3.85rem;}
section[data-testid="stSidebar"] div.stButton button {
width: 100%;
}
Expand All @@ -61,12 +62,14 @@ def main():
st.markdown(css, unsafe_allow_html=True)
st.logo("images/logo_light.png")

# Page Definition
# Enable/Disable Functionality
state.disable_api = os.environ.get("DISABLE_API", "false").lower() == "true"
state.disable_tools = os.environ.get("DISABLE_TOOLS", "false").lower() == "true"
state.disable_tests = os.environ.get("DISABLE_TESTS", "false").lower() == "true" and not state.disable_tools
state.disable_admin = os.environ.get("DISABLE_ADMIN", "false").lower() == "true" and not state.disable_tools
state.disable_oci = os.environ.get("DISABLE_OCI", "false").lower() == "true" and not state.disable_admin

# Left Hand Side - Navigation
chatbot = st.Page("content/chatbot.py", title="ChatBot", icon="💬", default=True)
navigation = {
"": [chatbot],
Expand All @@ -81,22 +84,22 @@ def main():
navigation["Tools"] = [prompt_eng]

# Administration
import_settings = st.Page("content/import_settings.py", title="Import Settings", icon="💾")
navigation["Configuration"] = [import_settings]
if not state.disable_tools and not state.disable_admin:
# Define Additional Pages
import_settings = st.Page("content/import_settings.py", title="Import Settings", icon="💾")
split_embed = st.Page("content/split_embed.py", title="Split/Embed", icon="📚")
model_config = st.Page("content/model_config.py", title="Models", icon="🤖")
db_config = st.Page("content/db_config.py", title="Database", icon="🗄️")
# Update Navigation
navigation["Tools"].insert(0, split_embed)
navigation["Configuration"].insert(0, model_config)
navigation["Configuration"] = [model_config]
navigation["Configuration"].insert(1, db_config)
navigation["Configuration"].insert(2, import_settings)
if not state.disable_oci:
oci_config = st.Page("content/oci_config.py", title="OCI", icon="☁️")
navigation["Configuration"].insert(2, oci_config)

pg = st.navigation(navigation)
pg = st.navigation(navigation, position="sidebar", expanded=False)
pg.run()


Expand Down
2 changes: 2 additions & 0 deletions docs/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ keywords = 'oracle microservices development genai rag'
<!--
Copyright (c) 2023, 2024, Oracle and/or its affiliates.
Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl.
spell-checker:ignore streamlit, genai, venv, oaim
-->

{{% notice style="code" title="10-Sept-2024: Documentation In-Progress..." icon="pen" %}}
Expand Down
48 changes: 33 additions & 15 deletions docs/content/sandbox/configuration/model_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ At a minimum, a Large _Language Model_ (LLM) must be configured in **Oracle AI M
If there is a specific model that you would like to use with the **Oracle AI Microservices Sandbox**, please [open an issue in GitHub](https://github.com/oracle-samples/oaim-sandbox/issues/new).
{{% /notice %}}

| Model | Type | API | On-Premises |
| ------------------------------ | ----- | -------------------------------------------------------- | ----------- |
| llama3.1 | LLM | [ChatOllama](#additional-information) | X |
| gpt-3.5-turbo | LLM | [OpenAI](#additional-information) | |
| gpt-4o-mini | LLM | [OpenAI](#additional-information) | |
| gpt-4 | LLM | [OpenAI](#additional-information) | |
| gpt-4o | LLM | [OpenAI](#additional-information) | |
| llama-3-sonar-small-32k-chat | LLM | [ChatPerplexity](#additional-information) | |
| llama-3-sonar-small-32k-online | LLM | [ChatPerplexity](#additional-information) | |
| mxbai-embed-large | Embed | [OllamaEmbeddings](#additional-information) | X |
| nomic-embed-text | Embed | [OllamaEmbeddings](#additional-information) | X |
| all-minilm | Embed | [OllamaEmbeddings](#additional-information) | X |
| thenlper/gte-base | Embed | [HuggingFaceEndpointEmbeddings](#additional-information) | X |
| text-embedding-3-small | Embed | [OpenAIEmbeddings](#additional-information) | |
| text-embedding-3-large | Embed | [OpenAIEmbeddings](#additional-information) | |
| Model | Type | API | On-Premises |
| ------------------------------- | ----- | -------------------------------------------------------- | ----------- |
| llama3.1 | LLM | [ChatOllama](#additional-information) | X |
| gpt-3.5-turbo | LLM | [OpenAI](#additional-information) | |
| gpt-4o-mini | LLM | [OpenAI](#additional-information) | |
| gpt-4 | LLM | [OpenAI](#additional-information) | |
| gpt-4o | LLM | [OpenAI](#additional-information) | |
| llama-3-sonar-small-128k-chat | LLM | [ChatPerplexity](#additional-information) | |
| llama-3-sonar-small-128k-online | LLM | [ChatPerplexity](#additional-information) | |
| command-r | LLM | [Cohere](#additional-information) | |
| mxbai-embed-large | Embed | [OllamaEmbeddings](#additional-information) | |
| nomic-embed-text | Embed | [OllamaEmbeddings](#additional-information) | X |
| all-minilm | Embed | [OllamaEmbeddings](#additional-information) | X |
| thenlper/gte-base | Embed | [HuggingFaceEndpointEmbeddings](#additional-information) | X |
| text-embedding-3-small | Embed | [OpenAIEmbeddings](#additional-information) | |
| text-embedding-3-large | Embed | [OpenAIEmbeddings](#additional-information) | |
| embed-english-v3.0 | Embed | [CohereEmbeddings](#additional-information) | |
| embed-english-light-v3.0 | Embed | [CohereEmbeddings](#additional-information) | |


## Configuration

Expand Down Expand Up @@ -148,6 +152,20 @@ Example of running thenlper/gte-base in a container:
**NOTE:** if there is no IP, use 127.0.0.1
{{% /tab %}}
{{% tab title="Cohere" %}}
# Cohere
[Cohere](https://cohere.com/) is an AI-powered answer engine. To use Cohere, you will need to sign-up and provide the **Sandbox** an API Key. Cohere offers a free-trial, rate-limited API Key.
**WARNING:** Cohere is a cloud model and you should familiarize yourself with their Privacy Policies if using it to experiment with private, sensitive data in the **Sandbox**.
>[!code]Skip the GUI!
>You can set the following environment variable to automatically set the `API Key` and enable Perplexity models:
:
>```shell
>export COHERE_API_KEY=<super-secret API Key>
>```
{{% /tab %}}
{{% tab title="OpenAI" %}}
# OpenAI
Expand Down
22 changes: 22 additions & 0 deletions helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,26 @@ Set the path based on baseUrlPath
{{- else -}}
{{- $baseUrlPath -}}
{{- end -}}
{{- end -}}

{{/*
Set default value for mtlsWallet if not defined
*/}}
{{- define "getMtlsWallet" -}}
{{- if .Values.oaimSandbox.database.mtlsWallet -}}
{{- .Values.oaimSandbox.database.mtlsWallet -}}
{{- else -}}
{{- dict -}} # Return an empty dictionary if mtlsWallet is not defined
{{- end -}}
{{- end -}}

{{/*
Set default value for tnsAdmin if not defined
*/}}
{{- define "getTnsAdmin" -}}
{{- if .Values.oaimSandbox.database.tnsAdmin -}}
{{- .Values.oaimSandbox.database.tnsAdmin -}}
{{- else -}}
{{- dict -}} # Return an empty dictionary if tnsAdmin is not defined
{{- end -}}
{{- end -}}
1 change: 0 additions & 1 deletion helm/templates/adb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{{- if .Values.oaimSandbox.database.ocid }}
apiVersion: database.oracle.com/v1alpha1
kind: AutonomousDatabase
metadata:
metadata:
name: {{ .Values.oaimSandbox.database.name | lower }}-adb
labels:
Expand Down
3 changes: 3 additions & 0 deletions helm/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ data:
cookieSecret = "{{ .Chart.Name }}-cookie"
headless = true
fileWatcherType = "none"
{{- $path := include "getPath" . }}
{{- if ne $path "/" }}
baseUrlPath = {{ include "getPath" . | quote }}
{{- end }}
[client]
toolbarMode = "minimal"
Expand Down
23 changes: 18 additions & 5 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ spec:
secretKeyRef:
name: {{ .Values.oaimSandbox.database.authN.secretName | quote }}
key: {{ .Values.oaimSandbox.database.authN.serviceKey | quote }}
{{- if .Values.oaimSandbox.database.mtlsWallet.secretName }}
{{- $mtlsWallet := include "getMtlsWallet" . | fromJson }}
{{- if $mtlsWallet.secretName }}
- name: DB_WALLET_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.oaimSandbox.database.mtlsWallet.secretName | quote }}
key: {{ .Values.oaimSandbox.database.mtlsWallet.secretKey | quote }}
name: {{ $mtlsWallet.secretName | quote }}
key: {{ $mtlsWallet.secretKey | quote }}
{{ else }}
- name: DB_WALLET_PASSWORD
valueFrom:
Expand All @@ -113,6 +114,10 @@ spec:
- name: DISABLE_ADMIN
value: "true"
{{- end }}
{{- if eq .disableAAPI "true" }}
- name: DISABLE_API
value: "true"
{{- end }}
{{- if .models }}
{{- if .models.openAI.secretName }}
- name: OPENAI_API_KEY
Expand All @@ -128,6 +133,13 @@ spec:
name: {{ .models.perplexity.secretName }}
key: {{ .models.perplexity.secretKey }}
{{- end }}
{{- if .models.cohere.secretName }}
- name: COHERE_API_KEY
valueFrom:
secretKeyRef:
name: {{ .models.cohere.secretName }}
key: {{ .models.cohere.secretKey }}
{{- end }}
{{- end }}
{{- end }}

Expand Down Expand Up @@ -156,8 +168,9 @@ spec:
{{- if .Values.oaimSandbox.database.enabled }}
- name: tns-admin
secret:
{{- if .Values.oaimSandbox.database.tnsAdmin.secretName }}
secretName: {{ .Values.oaimSandbox.database.tnsAdmin.secretName | quote }}
{{- $tnsAdmin := include "getTnsAdmin" . | fromJson }}
{{- if $tnsAdmin.secretName }}
secretName: {{ $tnsAdmin.secretName | quote }}
{{ else }}
secretName: {{ .Values.oaimSandbox.database.name | lower }}-tns-admin-{{ .Release.Revision }}
{{- end }}
Expand Down
3 changes: 2 additions & 1 deletion helm/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Copyright (c) 2023, 2024, Oracle and/or its affiliates.
## Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl.

{{- if not .Values.oaimSandbox.database.mtlsWallet.secretName }}
{{- $mtlsWallet := include "getMtlsWallet" . | fromJson }}
{{- if not $mtlsWallet.secretName }}
apiVersion: v1
kind: Secret
metadata:
Expand Down

0 comments on commit 53f2ef1

Please sign in to comment.