Skip to content

Commit

Permalink
Upgrade docs with docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkmyster committed Aug 29, 2023
1 parent 6c0e90d commit 088d471
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 43 deletions.
9 changes: 9 additions & 0 deletions langcontroller/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def create_skeleton(project: Renamer):
with open(f"{project.python_name()}/templates/__init__.py", "w") as my_file:
my_file.write(f'"""{project.human_name()} Templates."""\n')

os.mkdir(f"{project.python_name()}/docs")
os.mkdir(f"{project.python_name()}/docs/diagrams")

@staticmethod
def do_project(project_name: str):
"""Create a new LangController Project.
Expand Down Expand Up @@ -135,6 +138,12 @@ def do_project(project_name: str):
context=dict(project_name=project.human_name()),
)

template_writer.render_and_write(
template_file="docs/dataflow.mermaid.j2",
output_file=f"{project.python_name()}/docs/diagrams/dataflow.mermaid",
context=dict(),
)

@staticmethod
def do_sensor(
target_action: str, attribute_1: str, attribute_2: str, attribute_3: str
Expand Down
2 changes: 1 addition & 1 deletion langcontroller/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""LangController Controllers."""
from langcontroller.controllers.generic import *
from langcontroller.controllers.base import *
from langcontroller.controllers.marvin import *
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
"""Structured Output Strategy Abstract Base Class.
"""LangController Base Classes.
This module contains the Structured Output Strategy Abstract Base Class.
This module contains the base classes of the LangController project.
"""
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Callable
from typing import Callable, TypeVar, Any

from jinja2 import Environment, FileSystemLoader, select_autoescape

from langcontroller.controllers import OutputModel
OutputModel = TypeVar("OutputModel")
OutputModelStrategy = Callable[[Any], OutputModel]


@dataclass
class StructuredOutputStrategy(ABC):
class StructuredLLMOutputBase(ABC):
"""Structured Output Strategy Abstract Base Class."""

output_model: OutputModelStrategy
prompt_template: str = field(default="")
output_model: Callable[[str], OutputModel] = field(default_factory=OutputModel)

@abstractmethod
def apply(self, **kwargs) -> OutputModel:
Expand Down
21 changes: 12 additions & 9 deletions langcontroller/controllers/marvin.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
"""Marvin Structured Output Strategy.
"""Marvin Controller Module.
MarvinAI specific Strategy for submitting prompts to Marvin
This module contains the MarvinController class, which is a subclass of
the LangController class.
"""
from dataclasses import dataclass

from langcontroller.controllers.structs import OutputModel
from langcontroller.controllers.generic import (
StructuredOutputStrategy,
import pydantic

from langcontroller.controllers.base import (
StructuredLLMOutputBase,
OutputModel,
)


@dataclass
class MarvinStructuredOutputStrategy(StructuredOutputStrategy):
"""A MarvinAI specific Mixin for structured output."""
class MarvinStructuredLLMOutput(StructuredLLMOutputBase):
"""Marvin Structured LLM Output."""

def apply(self, **kwargs) -> OutputModel:
"""Apply prompt_template then output model.
"""Apply prompt_template then output pydantic model.
Args:
**kwargs: The context to apply to the jinja2 template for the llm prompt_template
Returns:
OutputModel: The populated structured output model based on Pydantic
pydantic.main.ModelMetaclass: The output model
"""
prompt = self.get_rendered_prompt(
prompt_template=self.prompt_template, **kwargs
Expand Down
4 changes: 0 additions & 4 deletions langcontroller/controllers/structs.py

This file was deleted.

27 changes: 27 additions & 0 deletions langcontroller/templates/docs/dataflow.mermaid.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: "Data Flow Architecture"
---
flowchart LR
Request -- Raw Text --> FeatureService -- Safe Response --> Response
PromptProcessor("Prompt Processor") -- Safe Text --> StructuredLLMOutput

subgraph FeatureService["Feature Service"]
direction LR
SecurityMiddlewarePre --> PromptTemplate --> SecurityMiddlewarePost
subgraph PromptProcessor["Prompt Processor"]
direction TB
PromptTemplate["Render Template"]
SecurityMiddlewarePre["Security Middleware Pre-Template"]
SecurityMiddlewarePost["Security Middleware Post-Template"]
end

InputModel --> LLMProcessor --> OutputModel
subgraph StructuredLLMOutput["Safe A.I."]
direction TB
InputModel["Input Model"]
LLMProcessor["LLM Processor"]
OutputModel["Output Model"]
end

end

22 changes: 19 additions & 3 deletions langcontroller/templates/repository/append_asset_class.py.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
@api_app.post("{{ target_action_underscore_name }}")
@api_app.post("/{{ target_action_underscore_name }}")
@cli_app.command()
def create_{{ target_action_underscore_name }}(
{{ source_action_underscore_name }}: str,
) -> {{ target_action_python_name }}:
"""Creates a {{ target_action_human_name }}."""
r"""Creates a {{ target_action_human_name }}.

```mermaid
flowchart LR
Input --> Processor --> Output

Input["Input Model\n___\napp.models.{{ source_action_python_name }}"]
Processor("LLM Processor\n___\nlangcontroller.controllers.marvin.MarvinStructuredLLMOutput")
Output["Output Model\n___\napp.models.{{ target_action_python_name }}"]
```

Args:
{{ source_action_underscore_name }} (str): The str of `app.models.{{ source_action_python_name }}

Returns:
`app.models.{{ target_action_python_name }}`: The {{ target_action_human_name }}
"""
print("Creating {{ target_action_human_name }}...")

model: {{ target_action_python_name }} = {{ controller_type }}StructuredOutputController(
model: {{ target_action_python_name }} = {{ controller_type }}StructuredLLMOutput(
prompt_template="{{ prompt_name }}",
output_model={{ target_action_python_name }},
).apply(content={{ source_action_underscore_name }})
Expand Down
16 changes: 14 additions & 2 deletions langcontroller/templates/repository/append_sensor_class.py.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
@api_app.post("/{{ target_action_underscore_name }}")
@cli_app.command()
def create_{{ target_action_underscore_name }}() -> {{ target_action_python_name }}:
"""Creates a {{ target_action_human_name }}."""
r"""Creates a {{ target_action_human_name }}.

```mermaid
flowchart LR
Processor --> Output

Processor("LLM Processor\n___\nlangcontroller.controllers.marvin.MarvinStructuredLLMOutput")
Output["Output Model\n___\napp.models.{{ target_action_python_name }}"]
```

Returns:
`app.models.{{ target_action_python_name }}`: The {{ target_action_human_name }}.
"""
print("Creating {{ target_action_human_name }}...")

model: {{ target_action_python_name }} = {{ controller_type }}StructuredOutputController(
model: {{ target_action_python_name }} = {{ controller_type }}StructuredLLMOutput(
prompt_template="{{ prompt_name }}",
output_model={{ target_action_python_name }},
).apply()
Expand Down
23 changes: 14 additions & 9 deletions langcontroller/templates/repository/create_repository_file.py.j2
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""{{ project_name }} Repository Services.
# -*- coding: utf-8 -*-
"""{{ project_nmae }}Feature Service Repository.

This module contains the important repository services of the {{ project_name }} project,
such as the FastAPI application and the CLI application.
This module contains the repository of feature services for the Scaled Agile Framework project.

Components wrap the controllers and models, and are used to provide a clean
interface to the developer.
Feature Services:
- wrap processors and models
- are used to provide a clean interface to the developer

Patterns:
Decorator-Pattern
Service-Pattern
Command-Pattern
Strategy-Pattern
- Decorator-Pattern
- Service-Pattern
- Command-Pattern
- Strategy-Pattern

```mermaid
.. include:: ../docs/diagrams/dataflow.mermaid
```
"""
import typer
from fastapi import FastAPI
Expand Down
16 changes: 7 additions & 9 deletions langcontroller/templates/tests/append_pipeline_class.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ from unittest.mock import patch, Mock


class StrategyCreationTestCase(unittest.TestCase):
def test_strategy_creation_returns_expected_content(self):
with patch(
"dagster.AssetExecutionContext"
) as mock_asset_execution_context, patch(
"app.pipeline.create_strategy"
) as mock_create_strategy:
@patch("dagster.AssetExecutionContext")
def test_strategy_creation_returns_expected_content(self, mock_asset_execution_context):
with patch("app.pipeline.create_strategy") as mock_create_strategy:
expected_content = dict(content="Test Content")

mock_response = Mock()
mock_response.dict.return_value = expected_content
mock_strategy_response = Mock()
mock_strategy_response.dict.return_value = expected_content

mock_create_strategy.side_effect = [mock_response]
mock_create_strategy.side_effect = [mock_strategy_response]

actual_strategy_result = strategy(context=mock_asset_execution_context)
self.assertEqual(actual_strategy_result, str(expected_content))
mock_create_strategy.assert_called_once()

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ black = "^23.7.0"
docformatter = "^1.7.5"
hypothesis = "^6.82.6"
pydocstyle = { extras = ["toml"], version = "^6.3.0" }
pdoc = "^14.0.0"

[tool.poetry.group.dagster.dependencies]
dagster = "^1.4.7"
Expand Down

0 comments on commit 088d471

Please sign in to comment.