Skip to content

Commit

Permalink
Docs/pydocs (iusztinpaul#40)
Browse files Browse the repository at this point in the history
* docs: Rename GA files

* docs: Add PyDocs
  • Loading branch information
iusztinpaul authored Nov 16, 2023
1 parent 520a038 commit d48a3d6
Show file tree
Hide file tree
Showing 20 changed files with 535 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd_streaming_pipeline.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Continuous Deployment (CD) for the Streaming Pipeline
name: Continuous Deployment (CD) | Streaming Pipeline

on: [workflow_dispatch]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_financial_bot.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Continuous Integration (CI) for the Financial Bot
name: Continuous Integration (CI) | Financial Bot

on: [push]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_streaming_pipeline.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Continuous Integration (CI) for the Streaming Pipeline
name: Continuous Integration (CI) | Streaming Pipeline

on: [push]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_training_pipeline.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Continuous Integration (CI) for the Training Pipeline
name: Continuous Integration (CI) | Training Pipeline

on: [push]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Destroy the AWS infrastructure for the Streaming Pipeline
name: Destroy AWS Infrastructure

on: [workflow_dispatch]

Expand Down
82 changes: 75 additions & 7 deletions modules/financial_bot/tools/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ def load_bot(
"""
Load the financial assistant bot in production or development mode based on the `debug` flag
production: the embedding model runs on GPU and the fine-tuned LLM is used.
dev: the embedding model runs on CPU and the fine-tuned LLM is mocked.
In DEV mode the embedding model runs on CPU and the fine-tuned LLM is mocked.
Otherwise, the embedding model runs on GPU and the fine-tuned LLM is used.
Args:
env_file_path (str): Path to the environment file.
logging_config_path (str): Path to the logging configuration file.
model_cache_dir (str): Path to the directory where the model cache is stored.
embedding_model_device (str): Device to use for the embedding model.
debug (bool): Flag to indicate whether to run the bot in debug mode or not.
Returns:
FinancialBot: An instance of the FinancialBot class.
"""

from financial_bot import initialize
Expand Down Expand Up @@ -83,7 +93,17 @@ def load_bot_dev(
logging_config_path: str = "logging.yaml",
model_cache_dir: str = "./model_cache",
):
"""Load the Financial Assistant Bot in dev mode: the embedding model runs on CPU and the LLM is mocked."""
"""
Load the Financial Assistant Bot in dev mode: the embedding model runs on CPU and the LLM is mocked.
Args:
env_file_path (str): Path to the environment file.
logging_config_path (str): Path to the logging configuration file.
model_cache_dir (str): Path to the directory where the model cache is stored.
Returns:
The loaded Financial Assistant Bot in dev mode.
"""

return load_bot(
env_file_path=env_file_path,
Expand All @@ -99,7 +119,19 @@ def load_bot_dev(

@financial_bot.rest_api(keep_warm_seconds=300, loader=load_bot)
def run(**inputs):
"""Run the bot under the Beam RESTful API endpoint."""
"""
Run the bot under the Beam RESTful API endpoint.
Args:
inputs (dict): A dictionary containing the following keys:
- context: The bot instance.
- about_me (str): Information about the user.
- question (str): The user's question.
- history (list): A list of previous conversations (optional).
Returns:
str: The bot's response to the user's question.
"""

response = _run(**inputs)

Expand All @@ -108,7 +140,19 @@ def run(**inputs):

@financial_bot_dev.rest_api(keep_warm_seconds=300, loader=load_bot_dev)
def run_dev(**inputs):
"""Run the bot under the Beam RESTful API endpoint [Dev Mode]."""
"""
Run the bot under the Beam RESTful API endpoint [Dev Mode].
Args:
inputs (dict): A dictionary containing the following keys:
- context: The bot instance.
- about_me (str): Information about the user.
- question (str): The user's question.
- history (list): A list of previous conversations (optional).
Returns:
str: The bot's response to the user's question.
"""

response = _run(**inputs)

Expand All @@ -121,7 +165,19 @@ def run_local(
history: List[Tuple[str, str]] = None,
debug: bool = False,
):
"""Run the bot locally in production or dev mode."""
"""
Run the bot locally in production or dev mode.
Args:
about_me (str): A string containing information about the user.
question (str): A string containing the user's question.
history (List[Tuple[str, str]], optional): A list of tuples containing the user's previous questions
and the bot's responses. Defaults to None.
debug (bool, optional): A boolean indicating whether to run the bot in debug mode. Defaults to False.
Returns:
str: A string containing the bot's response to the user's question.
"""

if debug is True:
bot = load_bot_dev(model_cache_dir=None)
Expand All @@ -141,7 +197,19 @@ def run_local(


def _run(**inputs):
"""Central function that calls the bot and returns the response"""
"""
Central function that calls the bot and returns the response.
Args:
inputs (dict): A dictionary containing the following keys:
- context: The bot instance.
- about_me (str): Information about the user.
- question (str): The user's question.
- history (list): A list of previous conversations (optional).
Returns:
str: The bot's response to the user's question.
"""

from financial_bot import utils

Expand Down
33 changes: 31 additions & 2 deletions modules/financial_bot/tools/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@


def parseargs() -> argparse.Namespace:
"""
Parses command line arguments for the Financial Assistant Bot.
Returns:
argparse.Namespace: An object containing the parsed arguments.
"""

parser = argparse.ArgumentParser(description="Financial Assistant Bot")

parser.add_argument(
Expand Down Expand Up @@ -66,8 +73,18 @@ def load_bot(
"""
Load the financial assistant bot in production or development mode based on the `debug` flag
production: the embedding model runs on GPU and the fine-tuned LLM is used.
dev: the embedding model runs on CPU and the fine-tuned LLM is mocked.
In DEV mode the embedding model runs on CPU and the fine-tuned LLM is mocked.
Otherwise, the embedding model runs on GPU and the fine-tuned LLM is used.
Args:
env_file_path (str): Path to the environment file.
logging_config_path (str): Path to the logging configuration file.
model_cache_dir (str): Path to the directory where the model cache is stored.
embedding_model_device (str): Device to use for the embedding model.
debug (bool): Flag to indicate whether to run the bot in debug mode or not.
Returns:
FinancialBot: An instance of the FinancialBot class.
"""

from financial_bot import initialize
Expand Down Expand Up @@ -106,6 +123,18 @@ def load_bot(


def predict(message: str, history: List[List[str]], about_me: str) -> str:
"""
Predicts a response to a given message using the financial_bot Gradio UI.
Args:
message (str): The message to generate a response for.
history (List[List[str]]): A list of previous conversations.
about_me (str): A string describing the user.
Returns:
str: The generated response.
"""

generate_kwargs = {
"about_me": about_me,
"question": message,
Expand Down
8 changes: 8 additions & 0 deletions modules/streaming_pipeline/streaming_pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@


def initialize(logging_config_path: str = "logging.yaml", env_file_path: str = ".env"):
"""
Initializes the logger and environment variables.
Args:
logging_config_path (str): The path to the logging configuration file. Defaults to "logging.yaml".
env_file_path (str): The path to the environment variables file. Defaults to ".env".
"""

logger.info("Initializing logger...")
try:
initialize_logger(config_path=logging_config_path)
Expand Down
74 changes: 73 additions & 1 deletion modules/streaming_pipeline/streaming_pipeline/alpaca_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class AlpacaNewsBatchInput(DynamicInput):
Args:
tickers: list - should be a list of tickers, use "*" for all
from_datetime: datetime.datetime - the start datetime for the news data
to_datetime: datetime.datetime - the end datetime for the news data
"""

def __init__(
Expand Down Expand Up @@ -52,6 +54,15 @@ def build(self, worker_index, worker_count):


class AlpacaNewsBatchSource(StatelessSource):
"""
A batch source for retrieving news articles from Alpaca.
Args:
tickers (List[str]): A list of ticker symbols to retrieve news for.
from_datetime (datetime.datetime): The start datetime to retrieve news from.
to_datetime (datetime.datetime): The end datetime to retrieve news from.
"""

def __init__(
self,
tickers: List[str],
Expand All @@ -63,6 +74,13 @@ def __init__(
)

def next(self):
"""
Retrieves the next batch of news articles.
Returns:
List[dict]: A list of news articles.
"""

news = self._alpaca_client.list()

if news is None or len(news) == 0:
Expand All @@ -71,6 +89,10 @@ def next(self):
return news

def close(self):
"""
Closes the batch source.
"""

pass


Expand All @@ -81,6 +103,23 @@ def build_alpaca_client(
api_secret: Optional[str] = None,
tickers: Optional[List[str]] = None,
) -> "AlpacaNewsBatchClient":
"""
Builds an AlpacaNewsBatchClient object with the specified parameters.
Args:
from_datetime (datetime.datetime): The start datetime for the news batch.
to_datetime (datetime.datetime): The end datetime for the news batch.
api_key (Optional[str], optional): The Alpaca API key. Defaults to None.
api_secret (Optional[str], optional): The Alpaca API secret. Defaults to None.
tickers (Optional[List[str]], optional): The list of tickers to retrieve news for. Defaults to None.
Raises:
KeyError: If api_key or api_secret is not provided and is not found in the environment variables.
Returns:
AlpacaNewsBatchClient: The AlpacaNewsBatchClient object.
"""

if api_key is None:
try:
api_key = os.environ["ALPACA_API_KEY"]
Expand Down Expand Up @@ -110,7 +149,19 @@ def build_alpaca_client(


class AlpacaNewsBatchClient:
"""Alpaca News API Client that uses a RESTful API to fetch news data."""
"""
Alpaca News API Client that uses a RESTful API to fetch news data.
Attributes:
NEWS_URL (str): The URL for the Alpaca News API.
_from_datetime (datetime.datetime): The start datetime for the news data.
_to_datetime (datetime.datetime): The end datetime for the news data.
_api_key (str): The API key for the Alpaca News API.
_api_secret (str): The API secret for the Alpaca News API.
_tickers (List[str]): A list of tickers to filter the news data.
_page_token (str): The page token for the next page of news data.
_first_request (bool): A flag indicating whether this is the first request for news data.
"""

NEWS_URL = "https://data.alpaca.markets/v1beta1/news"

Expand All @@ -122,6 +173,17 @@ def __init__(
api_secret: str,
tickers: List[str],
):
"""
Initializes a new instance of the AlpacaNewsBatchClient class.
Args:
from_datetime (datetime.datetime): The start datetime for the news data.
to_datetime (datetime.datetime): The end datetime for the news data.
api_key (str): The API key for the Alpaca News API.
api_secret (str): The API secret for the Alpaca News API.
tickers (List[str]): A list of tickers to filter the news data.
"""

self._from_datetime = from_datetime
self._to_datetime = to_datetime
self._api_key = api_key
Expand All @@ -133,11 +195,21 @@ def __init__(

@property
def try_request(self) -> bool:
"""
A property indicating whether a request should be attempted.
Returns:
bool: True if a request should be attempted, False otherwise.
"""

return self._first_request or self._page_token is not None

def list(self):
"""
Convenience function to fetch a batch of news from Alpaca API
Returns:
List[Dict]: A list of news items.
"""

if not self.try_request:
Expand Down
Loading

0 comments on commit d48a3d6

Please sign in to comment.