Skip to content

Commit

Permalink
Add initenv API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
yoomlam committed Jun 7, 2024
1 parent 43b13eb commit c24ba4d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
1 change: 1 addition & 0 deletions 05-assistive-chatbot/.env-DEV
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CHATBOT_LOG_LEVEL='WARN'

ENABLE_CHATBOT_API=False

Expand Down
4 changes: 2 additions & 2 deletions 05-assistive-chatbot/.env-PROD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ENABLE_CHATBOT_API=True

CHATBOT_LOG_LEVEL='DEBUG'

ENABLE_CHATBOT_API=True

CHAT_ENGINE='Summaries'
LLM_MODEL_NAME='openai :: gpt-3.5-turbo-instruct'
RETRIEVE_K=4
Expand Down
1 change: 1 addition & 0 deletions 05-assistive-chatbot/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ log/

# .env contains secret API keys
.env
.secrets-*

guru_cards*.json
5 changes: 3 additions & 2 deletions 05-assistive-chatbot/chatbot-chainlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def init_chat():
build_date = os.environ.get("BUILD_DATE", "unknown")
await cl.Message(f"Welcome to the Assistive Chat prototype (built {build_date})").send()

available_llms = llms.available_llms()
# https://docs.chainlit.io/api-reference/chat-settings
chat_settings = cl.ChatSettings(
[
Expand All @@ -44,7 +45,7 @@ async def init_chat():
Select(
id="model",
label="Primary LLM Model",
values=llms.available_llms(),
values=available_llms,
initial_value=chatbot.initial_settings["model"],
),
Slider(
Expand All @@ -66,7 +67,7 @@ async def init_chat():
Select(
id="model2",
label="LLM Model for summarizer",
values=llms.available_llms(),
values=available_llms,
initial_value=chatbot.initial_settings["model2"],
),
Slider(
Expand Down
37 changes: 25 additions & 12 deletions 05-assistive-chatbot/chatbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
# - add unit tests


## Set default environment variables


# Opt out of telemetry -- https://docs.trychroma.com/telemetry
os.environ.setdefault("ANONYMIZED_TELEMETRY", "False")

# Used by SentenceTransformerEmbeddings and HuggingFaceEmbeddings
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", "./.sentence-transformers-cache")

# Disable DSPy cache to get different responses for retry attempts
# Set to true to enable caching for faster responses and optimizing prompts using DSPy
os.environ.setdefault("DSP_CACHEBOOL", "false")

os.environ.setdefault("BUILD_DATE", str(date.today()))


## Initialize logging


Expand All @@ -29,22 +45,11 @@ def configure_logging():
dotenv.load_dotenv()
configure_logging()
logger = logging.getLogger(__name__)
logger.info("Build date: %s", os.environ.get("BUILD_DATE"))


## Initialize settings

# Opt out of telemetry -- https://docs.trychroma.com/telemetry
os.environ.setdefault("ANONYMIZED_TELEMETRY", "False")

# Used by SentenceTransformerEmbeddings and HuggingFaceEmbeddings
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", "./.sentence-transformers-cache")

# Disable DSPy cache to get different responses for retry attempts
# Set to true to enable caching for faster responses and optimizing prompts using DSPy
os.environ.setdefault("DSP_CACHEBOOL", "false")

os.environ.setdefault("BUILD_DATE", str(date.today()))


@utils.verbose_timer(logger)
def _init_settings():
Expand All @@ -70,6 +75,14 @@ def is_true(string):
initial_settings = _init_settings()


def reset():
configure_logging()
engines._engines.clear()
llms._llms.clear()
global initial_settings
initial_settings = _init_settings()


@utils.verbose_timer(logger)
def validate_settings(settings):
chat_engine = settings["chat_engine"]
Expand Down
23 changes: 22 additions & 1 deletion 05-assistive-chatbot/chatbot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import logging
import os
from functools import cached_property
from io import StringIO
from typing import Dict

from fastapi import FastAPI, Request
import dotenv
from fastapi import Body, FastAPI, Request
from fastapi.responses import HTMLResponse

import chatbot
Expand Down Expand Up @@ -60,6 +62,25 @@ def healthcheck(request: Request):
return HTMLResponse(f"Healthy {build_date} {git_sha}")


ALLOWED_ENV_VARS = ["CHATBOT_LOG_LEVEL"]


@app.post("/initenvs")
def initenvs(env_file_contents: str = Body()):
print(f"{type(env_file_contents)}: {env_file_contents}")
env_values = dotenv.dotenv_values(stream=StringIO(env_file_contents))
values_changed = []
for name, value in env_values.items():
if name.endswith("_API_KEY") or name.endswith("_API_TOKEN") or name in ALLOWED_ENV_VARS:
logger.info("Setting environment variable %s", name)
os.environ[name] = value or ""
values_changed.append(name)
else:
logger.warning("Setting environment variable %s is not allowed!", name)
chatbot.reset()
return str(values_changed)


if __name__ == "__main__":
import uvicorn

Expand Down

0 comments on commit c24ba4d

Please sign in to comment.