Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
feat: Deploy financial assistant as a RESTful API
Browse files Browse the repository at this point in the history
  • Loading branch information
iusztinpaul committed Oct 11, 2023
1 parent b94acc8 commit 3a860f1
Show file tree
Hide file tree
Showing 14 changed files with 473 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"name": "Financial Bot [Dev]",
"type": "python",
"request": "launch",
"module": "tools.run_chain",
"module": "tools.bot",
"justMyCode": false,
"cwd": "${workspaceFolder}/modules/financial_bot",
},
Expand Down
5 changes: 5 additions & 0 deletions modules/financial_bot/.beamignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model_cache/*
results/*
output/*
logs/*
.ruff_cache/
34 changes: 31 additions & 3 deletions modules/financial_bot/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Install ###
# === Install ===

install:
@echo "Installing financial bot..."
Expand All @@ -25,10 +25,38 @@ add_dev:
run:
@echo "Running financial_bot..."

poetry run python -m tools.run_chain
poetry run python -m tools.bot


### PEP 8 ###
# === Beam ===

export_requirements:
@echo "Exporting requirements..."

if [ -f requirements.txt ]; then rm requirements.txt; fi
poetry export -f requirements.txt --output requirements.txt --without-hashes

run_beam: export_requirements
@echo "Running financial_bot on Beam..."

BEAM_IGNORE_IMPORTS_OFF=true beam run ./tools/bot.py:run -d '{"env_file_path": "env", "model_cache_dir": "./model_cache"}'

deploy_beam: export_requirements
@echo "Deploying financial_bot on Beam..."

BEAM_IGNORE_IMPORTS_OFF=true beam deploy ./tools/bot.py:run

call_restful_api:
curl -X POST \
--compressed 'https://apps.beam.cloud/${DEPLOYMENT_ID}' \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Authorization: Basic ${TOKEN}' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-d '{"about_me": "I am a student and I have some money that I want to invest.", "question": "Should I consider investing in stocks from the Tech Sector?"}'

# === Formatting & Linting ===
# Be sure to install the dev dependencies first #

lint_check:
Expand Down
49 changes: 49 additions & 0 deletions modules/financial_bot/financial_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import logging
import logging.config
import os
import yaml

from dotenv import load_dotenv, find_dotenv
from pathlib import Path


logger = logging.getLogger(__name__)


def initialize(logging_config_path: str = "logging.yaml", env_file_path: str = ".env"):
logger.info("Initializing logger...")
try:
initialize_logger(config_path=logging_config_path)
except FileNotFoundError:
logger.warning(
f"No logging configuration file found at: {logging_config_path}. Setting logging level to INFO."
)
logging.basicConfig(level=logging.INFO)

logger.info("Initializing env vars...")
if env_file_path is None:
env_file_path = find_dotenv(raise_error_if_not_found=True, usecwd=False)

logger.info(f"Loading environment variables from: {env_file_path}")
found_env_file = load_dotenv(env_file_path, verbose=True, override=True)
if found_env_file is False:
raise RuntimeError(f"Could not find environment file at: {env_file_path}")


def initialize_logger(
config_path: str = "logging.yaml", logs_dir_name: str = "logs"
) -> logging.Logger:
"""Initialize logger from a YAML config file."""

# Create logs directory.
config_path_parent = Path(config_path).parent
logs_dir = config_path_parent / logs_dir_name
logs_dir.mkdir(parents=True, exist_ok=True)

with open(config_path, "rt") as f:
config = yaml.safe_load(f.read())

# Make sure that existing logger will still work.
config["disable_existing_loggers"] = False

logging.config.dictConfig(config)
2 changes: 1 addition & 1 deletion modules/financial_bot/financial_bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
SYSTEM_MESSAGE = "You are a financial expert. Based on the context I provide, respond in a helpful manner"

# === Misc ===
DEBUG = True
DEBUG = False
7 changes: 6 additions & 1 deletion modules/financial_bot/financial_bot/langchain_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from pathlib import Path

from langchain import chains
from langchain.memory import ConversationBufferMemory
Expand All @@ -18,12 +19,16 @@ def __init__(
self,
llm_model_id: str = constants.LLM_MODEL_ID,
llm_lora_model_id: str = constants.LLM_QLORA_CHECKPOINT,
model_cache_dir: Path = constants.CACHE_DIR,
debug: bool = constants.DEBUG,
):
self._qdrant_client = build_qdrant_client()
self._embd_model = EmbeddingModelSingleton()
self._llm_agent = build_huggingface_pipeline(
llm_model_id=llm_model_id, llm_lora_model_id=llm_lora_model_id, debug=debug
llm_model_id=llm_model_id,
llm_lora_model_id=llm_lora_model_id,
cache_dir=model_cache_dir,
debug=debug,
)
self.finbot_chain = self.build_chain()

Expand Down
27 changes: 27 additions & 0 deletions modules/financial_bot/logging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 1

formatters:
simple:
format: "%(asctime)s - %(levelname)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
file_info:
class: logging.FileHandler
formatter: simple
level: INFO
filename: logs/info.log
mode: 'a'
file_error:
class: logging.FileHandler
formatter: simple
level: ERROR
filename: logs/error.log
mode: 'a'
root:
level: INFO
handlers: [console, file_info, file_error]

Loading

0 comments on commit 3a860f1

Please sign in to comment.