Skip to content

Commit

Permalink
Server/Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
gotsysdba committed Jan 25, 2025
1 parent 46193dc commit 12e9027
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 74 deletions.
29 changes: 18 additions & 11 deletions app/oaim_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

logger = logging_config.logging.getLogger("oaim_sandbox")

# Start the server if it's local and not running
# Import oaim_server if it exists
REMOTE_SERVER = False
try:
import oaim_server
oaim_server.start_server()
from oaim_server import start_server
except ImportError:
state.remote_server = True
REMOTE_SERVER = True


#############################################################################
# MAIN
#############################################################################
Expand All @@ -50,23 +52,21 @@ def main() -> None:
""",
)
st.logo("sandbox/media/logo_light.png")

# Setup Settings State
api_endpoint = f"{state.server['url']}:{state.server['port']}/v1/settings"
api_down = True
api_down = False
if "user_settings" not in state:
try:
state.user_settings = api_call.post(url=api_endpoint, params={"client": client_gen_id()})
api_down = False
except api_call.ApiError:
st.error("Unable to contact the API Server... trying again...", icon="🚨")
if "server_settings" not in state:
api_down = True
if not api_down and "server_settings" not in state:
try:
state.server_settings = api_call.get(url=api_endpoint, params={"client": "server"})
api_down = False
except api_call.ApiError:
st.error("Unable to contact the API Server. Stopping.", icon="🚨")
api_down = True
if api_down:
st.error("Unable to contact the API Server. Please check that it is running.", icon="🛑")
st.stop()

# Enable/Disable Functionality
Expand Down Expand Up @@ -122,6 +122,13 @@ def main() -> None:
pg.run()



if __name__ == "__main__":
set_server_state()
# Start Server if not running
if not REMOTE_SERVER:
try:
logger.debug("Server PID: %i", state.server["pid"])
except KeyError:
state.server["pid"] = start_server()
main()
2 changes: 1 addition & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
### pip3 install --upgrade pip wheel setuptools
### pip3 install -r requirements.txt
langchain-core==0.3.31
httpx==0.27.2
httpx==0.27.2 # litellm restriction
oracledb>=2.0.0
-r server/requirements.txt
-r sandbox/requirements.txt
4 changes: 1 addition & 3 deletions app/sandbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ ENV PATH=/opt/.venv/bin:$PATH
ENV TEMP=/app/tmp

COPY --chown=$RUNUSER:$RUNUSER . /app
RUN rm -rf /app/server && \
rm -rf /app/oaim_server.py && \
rm -rf /app/.oci
RUN rm -rf /app/server /app/.oci /app/oaim_server.py

# Set user and working directory
USER $RUNUSER
Expand Down
4 changes: 2 additions & 2 deletions app/sandbox/content/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def main() -> None:
get_model(model_type="ll", only_enabled=True)
available_ll_models = list(state.ll_model_enabled.keys())
if not available_ll_models:
st.error("No language models are configured and/or enabled. Disabling Sandbox.", icon="")
st.error("No language models are configured and/or enabled. Disabling Sandbox.", icon="🛑")
st.stop()
# the sidebars will set this to False if not everything is configured.
state.enable_sandbox = True
Expand Down Expand Up @@ -122,7 +122,7 @@ async def main() -> None:
asyncio.run(main())
except ValueError as ex:
logger.exception("Bug detected: %s", ex)
st.error("It looks like you found a bug; please open an issue", icon="🚨")
st.error("It looks like you found a bug; please open an issue", icon="🛑")
st.stop()
except IndexError as ex:
logger.exception("Unable to contact the server: %s", ex)
Expand Down
2 changes: 1 addition & 1 deletion app/sandbox/content/config/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def main() -> None:

if st.form_submit_button("Save"):
if not user or not password or not dsn:
st.error("Username, Password and Connect String fields are required.", icon="")
st.error("Username, Password and Connect String fields are required.", icon="🛑")
st.stop()
patch_database(name, user, password, dsn, wallet_password)

Expand Down
2 changes: 1 addition & 1 deletion app/sandbox/content/config/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def main() -> None:
security_token_file = None if not token_auth else security_token_file
user = None if token_auth else user
if not (fingerprint and tenancy and region and key_file and (user or security_token_file)):
st.error("All fields are required.", icon="")
st.error("All fields are required.", icon="🛑")
st.stop()
patch_oci(profile, fingerprint, tenancy, region, key_file, user, security_token_file)

Expand Down
20 changes: 12 additions & 8 deletions app/sandbox/content/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import streamlit as st
from streamlit import session_state as state

import oaim_server
import sandbox.utils.st_common as st_common
import sandbox.utils.client as client
import sandbox.utils.api_call as api_call
Expand All @@ -23,7 +22,11 @@

logger = logging_config.logging.getLogger("sandbox.content.server")


try:
import oaim_server
REMOTE_SERVER = False
except ImportError:
REMOTE_SERVER = True
#####################################################
# Functions
#####################################################
Expand Down Expand Up @@ -53,6 +56,7 @@ def server_restart() -> None:

oaim_server.stop_server(state.server["pid"])
state.server["pid"] = oaim_server.start_server(state.server["port"])
time.sleep(10)
state.pop("sever_client", None)


Expand All @@ -64,25 +68,23 @@ async def main() -> None:
st_common.set_server_state()
st.header("API Server")
st.write("Access the Sandbox with your own client.")
if "remote_server" not in state:
state.remote_server = False
left, right = st.columns([0.2, 0.8])
left.number_input(
"API Server Port:",
value=state.server["port"],
key="user_server_port",
min_value=1,
max_value=65535,
disabled=not state.remote_server
disabled=REMOTE_SERVER
)
right.text_input(
"API Server Key:",
value=state.server["key"],
key="user_server_key",
type="password",
disabled=not state.remote_server
disabled=REMOTE_SERVER
)
if state.remote_server:
if not REMOTE_SERVER:
st.button("Restart Server", type="primary", on_click=server_restart)

st.header("Server Configuration", divider="red")
Expand All @@ -106,14 +108,16 @@ async def main() -> None:
settings=state["server_settings"],
timeout=10,
)

server_client: client.SandboxClient = state.server_client

auto_refresh = st.toggle("Auto Refresh (every 10sec)", value=False, key="selected_auto_refresh")
st.button("Manual Refresh", disabled=auto_refresh)
with st.container():
history = await server_client.get_history()
if len(history) == 1:
if history is None or len(history) == 1:
st.write("No Server Activity")
history = []
for message in history:
if message["role"] in ("ai", "assistant"):
st.chat_message("ai").json(message, expanded=False)
Expand Down
6 changes: 3 additions & 3 deletions app/sandbox/utils/api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def send_request(
raise ApiError("An unexpected error occurred.")


def get(url: str, params: Optional[dict] = None, retries: int = 2, backoff_factor: float = 2.0) -> json:
def get(url: str, params: Optional[dict] = None, retries: int = 3, backoff_factor: float = 2.0) -> json:
"""GET Requests"""
response = send_request("GET", url, params=params, retries=retries, backoff_factor=backoff_factor)
return response.json()
Expand All @@ -94,7 +94,7 @@ def post(
params: Optional[dict] = None,
payload: Optional[Dict] = None,
timeout: int = 60,
retries: int = 2,
retries: int = 3,
backoff_factor: float = 2.0,
) -> json:
"""POST Requests"""
Expand All @@ -109,7 +109,7 @@ def patch(
params: Optional[dict] = None,
payload: Optional[dict] = None,
timeout: int = 60,
retries: int = 2,
retries: int = 3,
backoff_factor: float = 2.0,
) -> None:
"""PATCH Requests"""
Expand Down
27 changes: 15 additions & 12 deletions app/sandbox/utils/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,19 @@ async def completions(self, message: str) -> ChatResponse:
return f"Error: {response.status_code} - {error_msg}"

async def get_history(self) -> list[ChatMessage]:
response = httpx.get(
url=self.server_url + "/v1/chat/history",
params={"client": self.settings["client"]},
headers=self.headers,
timeout=self.timeout,
)
response_data = response.json()
logger.debug("Response Received: %s", response_data)
if response.status_code == 200:
return response_data
try:
response = httpx.get(
url=self.server_url + "/v1/chat/history",
params={"client": self.settings["client"]},
headers=self.headers,
timeout=self.timeout,
)
response_data = response.json()
logger.debug("Response Received: %s", response_data)
if response.status_code == 200:
return response_data

error_msg = response_data["detail"][0].get("msg", response.text)
return f"Error: {response.status_code} - {error_msg}"
error_msg = response_data["detail"][0].get("msg", response.text)
return f"Error: {response.status_code} - {error_msg}"
except httpx.ConnectError:
logger.error("Unable to contact the API Server; will try again later.")
12 changes: 6 additions & 6 deletions app/server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
# spell-checker:disable
bokeh==3.6.2
evaluate==0.4.3
fastapi==0.115.6
fastapi==0.115.7
faiss-cpu==1.9.0.post1
giskard==2.16.0
IPython==8.31.0
langchain-cohere==0.3.4
langchain-cohere==0.4.2
langchain-community==0.3.15
langchain-huggingface==0.1.2
langchain-ollama==0.2.2
langchain-openai==0.3.1
langgraph==0.2.65
litellm==1.59.1
llama_index==0.12.12
langchain-openai==0.3.2
langgraph==0.2.67
litellm==1.59.7
llama_index==0.12.13
lxml==5.3.0
matplotlib==3.10.0
oci>=2.0.0
Expand Down
63 changes: 63 additions & 0 deletions app/server/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Copyright (c) 2023, 2024, Oracle and/or its affiliates.
Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
"""
# spell-checker:ignore oaim

import json

from oracledb import Connection
import server.databases as databases
from common.schema import ClientIdType
import common.logging_config as logging_config

logger = logging_config.logging.getLogger("server.testbed")


def create_settings_objects(db_conn: Connection) -> None:
"""Create table to store Settings"""
settings_tbl = """
CREATE TABLE IF NOT EXISTS oaim_settings (
name VARCHAR2(255) NOT NULL,
type VARCHAR2(255) NOT NULL,
created TIMESTAMP(9) WITH LOCAL TIME ZONE,
updated TIMESTAMP(9) WITH LOCAL TIME ZONE,
settings JSON,
CONSTRAINT oaim_settings_pk PRIMARY KEY (name, type),
CONSTRAINT oaim_settings_type_ck CHECK (supplier_name IN ('sandbox', 'client'))
)
"""
logger.info("Creating settings Table")
_ = databases.execute_sql(db_conn, settings_tbl)


def upsert_settings(
db_conn: Connection,
name: ClientIdType,
type: str,
json_data: json,
) -> None:
"""Upsert Q&A"""
logger.info("Upsert Settings: %s - %s", name, type)
parsed_data = json.loads(json_data)
if not isinstance(parsed_data, list):
parsed_data = [parsed_data]
json_data = json.dumps(parsed_data) if isinstance(parsed_data, list) else json_data
binds = {"name": name, "type": type, "json_data": json_data}
plsql = """
DECLARE
l_name oaim_settings.name%TYPE := :name;
l_type oaim_settings.type%TYPE := :type;
l_qa_obj JSON_OBJECT_T := :json_data;
BEGIN
UPDATE oaim_settings SET
updated = CURRENT_TIMESTAMP,
settings = l_qa_object
WHERE name = l_name AND type = l_type;
EXCEPTION WHEN NO_DATA_FOUND
INSERT INTO oaim_settings (name, type, created, settings)
VALUES (l_name, l_type, CURRENT_TIMESTAMP, l_qa_object);
END;
"""
logger.debug("Upsert PLSQL: %s", plsql)
return databases.execute_sql(db_conn, plsql, binds)
Loading

0 comments on commit 12e9027

Please sign in to comment.