From c76b45342551be253f465fe8aabcaf606a24f1b4 Mon Sep 17 00:00:00 2001 From: Collin Dutter Date: Thu, 15 Aug 2024 09:11:25 -0700 Subject: [PATCH 1/2] Lazily initialize config drivers field (#1062) --- griptape/config/__init__.py | 2 ++ griptape/config/base_config.py | 18 +++++++++++----- griptape/config/config.py | 37 +++++++++++++++++++++++++++----- tests/unit/config/test_config.py | 25 +++++++++++++++++++++ tests/unit/conftest.py | 20 +++++++++++++---- 5 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 tests/unit/config/test_config.py diff --git a/griptape/config/__init__.py b/griptape/config/__init__.py index b242d80a70..4a87c2d015 100644 --- a/griptape/config/__init__.py +++ b/griptape/config/__init__.py @@ -9,6 +9,7 @@ from .anthropic_driver_config import AnthropicDriverConfig from .google_driver_config import GoogleDriverConfig from .cohere_driver_config import CohereDriverConfig +from .logging_config import LoggingConfig from .config import config @@ -22,5 +23,6 @@ "AnthropicDriverConfig", "GoogleDriverConfig", "CohereDriverConfig", + "LoggingConfig", "config", ] diff --git a/griptape/config/base_config.py b/griptape/config/base_config.py index 9209aa4a4f..ef62a4e9b1 100644 --- a/griptape/config/base_config.py +++ b/griptape/config/base_config.py @@ -1,14 +1,22 @@ +from __future__ import annotations + from abc import ABC +from typing import TYPE_CHECKING, Optional -from attrs import define +from attrs import define, field from griptape.mixins.serializable_mixin import SerializableMixin -from .base_driver_config import BaseDriverConfig -from .logging_config import LoggingConfig +if TYPE_CHECKING: + from .base_driver_config import BaseDriverConfig + from .logging_config import LoggingConfig @define(kw_only=True) class BaseConfig(SerializableMixin, ABC): - drivers: BaseDriverConfig - logging: LoggingConfig + _logging: Optional[LoggingConfig] = field(alias="logging") + _drivers: Optional[BaseDriverConfig] = field(alias="drivers") + + def reset(self) -> None: + self._logging = None + self._drivers = None diff --git a/griptape/config/config.py b/griptape/config/config.py index 97d501abb1..7b70df409a 100644 --- a/griptape/config/config.py +++ b/griptape/config/config.py @@ -1,15 +1,42 @@ -from attrs import Factory, define, field +from __future__ import annotations + +from typing import TYPE_CHECKING, Optional + +from attrs import define, field from .base_config import BaseConfig -from .base_driver_config import BaseDriverConfig from .logging_config import LoggingConfig from .openai_driver_config import OpenAiDriverConfig +if TYPE_CHECKING: + from .base_driver_config import BaseDriverConfig -@define + +@define(kw_only=True) class _Config(BaseConfig): - drivers: BaseDriverConfig = field(default=Factory(lambda: OpenAiDriverConfig()), kw_only=True) - logging: LoggingConfig = field(default=Factory(lambda: LoggingConfig()), kw_only=True) + _logging: Optional[LoggingConfig] = field(default=None, alias="logging") + _drivers: Optional[BaseDriverConfig] = field(default=None, alias="drivers") + + @property + def drivers(self) -> BaseDriverConfig: + """Lazily instantiates the drivers configuration to avoid client errors like missing API key.""" + if self._drivers is None: + self._drivers = OpenAiDriverConfig() + return self._drivers + + @drivers.setter + def drivers(self, drivers: BaseDriverConfig) -> None: + self._drivers = drivers + + @property + def logging(self) -> LoggingConfig: + if self._logging is None: + self._logging = LoggingConfig() + return self._logging + + @logging.setter + def logging(self, logging: LoggingConfig) -> None: + self._logging = logging config = _Config() diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py new file mode 100644 index 0000000000..ecd02e1a18 --- /dev/null +++ b/tests/unit/config/test_config.py @@ -0,0 +1,25 @@ +import pytest + +from griptape.config.openai_driver_config import OpenAiDriverConfig + + +class TestConfig: + @pytest.mark.skip_mock_config() + def test_init(self): + from griptape.config import LoggingConfig, config + + assert isinstance(config.drivers, OpenAiDriverConfig) + assert isinstance(config.logging, LoggingConfig) + + @pytest.mark.skip_mock_config() + def test_lazy_init(self): + from griptape.config import config + + assert config._drivers is None + assert config._logging is None + + assert config.drivers is not None + assert config.logging is not None + + assert config._drivers is not None + assert config._logging is not None diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 8a37f6d283..db881bc201 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -1,12 +1,12 @@ import pytest -from griptape.config import config -from griptape.events import event_bus from tests.mocks.mock_driver_config import MockDriverConfig @pytest.fixture(autouse=True) def mock_event_bus(): + from griptape.events import event_bus + event_bus.clear_event_listeners() yield event_bus @@ -15,7 +15,19 @@ def mock_event_bus(): @pytest.fixture(autouse=True) -def mock_config(): +def mock_config(request): + from griptape.config import config + + config.reset() + + # Some tests we don't want to use the autouse fixture's MockDriverConfig + if "skip_mock_config" in request.keywords: + yield + + return + config.drivers = MockDriverConfig() - return config + yield config + + config.reset() From babc56a5b4415b8358c7cbcdda7abc137aebed37 Mon Sep 17 00:00:00 2001 From: Matt Vallillo Date: Thu, 15 Aug 2024 11:31:05 -0500 Subject: [PATCH 2/2] Add logging filters, update config namespaces (#1059) --- CHANGELOG.md | 2 +- .../src/multiple_agent_shared_memory_1.py | 3 +- docs/examples/src/talk_to_a_video_1.py | 3 +- .../drivers/src/embedding_drivers_10.py | 3 +- .../drivers/src/event_listener_drivers_4.py | 3 +- docs/griptape-framework/structures/config.md | 34 ++++++++++++------- .../structures/src/config_1.py | 3 +- .../structures/src/config_2.py | 3 +- .../structures/src/config_3.py | 3 +- .../structures/src/config_4.py | 3 +- .../structures/src/config_5.py | 3 +- .../structures/src/config_6.py | 3 +- .../structures/src/config_7.py | 3 +- .../structures/src/config_8.py | 3 +- .../structures/src/config_logging.py | 14 ++++++++ .../structures/src/task_memory_6.py | 6 ++-- .../official-tools/src/rest_api_tool_1.py | 3 +- griptape/config/__init__.py | 20 ----------- griptape/config/base_config.py | 4 +-- griptape/config/config.py | 6 ++-- griptape/config/drivers/__init__.py | 20 +++++++++++ .../amazon_bedrock_driver_config.py | 2 +- .../{ => drivers}/anthropic_driver_config.py | 2 +- .../azure_openai_driver_config.py | 2 +- .../{ => drivers}/base_driver_config.py | 0 .../{ => drivers}/cohere_driver_config.py | 2 +- .../config/{ => drivers}/driver_config.py | 2 +- .../{ => drivers}/google_driver_config.py | 2 +- .../{ => drivers}/openai_driver_config.py | 2 +- griptape/config/logging/__init__.py | 5 +++ .../config/{ => logging}/logging_config.py | 11 ++---- .../config/logging/newline_logging_filter.py | 13 +++++++ .../config/logging/truncate_logging_filter.py | 17 ++++++++++ tests/mocks/mock_driver_config.py | 2 +- tests/unit/config/drivers/__init__.py | 0 .../test_amazon_bedrock_driver_config.py | 2 +- .../test_anthropic_driver_config.py | 2 +- .../test_azure_openai_driver_config.py | 2 +- .../test_cohere_driver_config.py | 2 +- .../{ => drivers}/test_driver_config.py | 2 +- .../test_google_driver_config.py | 2 +- .../test_openai_driver_config.py | 2 +- tests/unit/config/logging/__init__.py | 0 .../logging/test_newline_logging_filter.py | 20 +++++++++++ .../logging/test_truncate_logging_filter.py | 20 +++++++++++ tests/unit/config/test_config.py | 5 +-- 46 files changed, 185 insertions(+), 81 deletions(-) create mode 100644 docs/griptape-framework/structures/src/config_logging.py create mode 100644 griptape/config/drivers/__init__.py rename griptape/config/{ => drivers}/amazon_bedrock_driver_config.py (98%) rename griptape/config/{ => drivers}/anthropic_driver_config.py (96%) rename griptape/config/{ => drivers}/azure_openai_driver_config.py (98%) rename griptape/config/{ => drivers}/base_driver_config.py (100%) rename griptape/config/{ => drivers}/cohere_driver_config.py (95%) rename griptape/config/{ => drivers}/driver_config.py (97%) rename griptape/config/{ => drivers}/google_driver_config.py (94%) rename griptape/config/{ => drivers}/openai_driver_config.py (97%) create mode 100644 griptape/config/logging/__init__.py rename griptape/config/{ => logging}/logging_config.py (53%) create mode 100644 griptape/config/logging/newline_logging_filter.py create mode 100644 griptape/config/logging/truncate_logging_filter.py create mode 100644 tests/unit/config/drivers/__init__.py rename tests/unit/config/{ => drivers}/test_amazon_bedrock_driver_config.py (98%) rename tests/unit/config/{ => drivers}/test_anthropic_driver_config.py (96%) rename tests/unit/config/{ => drivers}/test_azure_openai_driver_config.py (98%) rename tests/unit/config/{ => drivers}/test_cohere_driver_config.py (96%) rename tests/unit/config/{ => drivers}/test_driver_config.py (96%) rename tests/unit/config/{ => drivers}/test_google_driver_config.py (97%) rename tests/unit/config/{ => drivers}/test_openai_driver_config.py (98%) create mode 100644 tests/unit/config/logging/__init__.py create mode 100644 tests/unit/config/logging/test_newline_logging_filter.py create mode 100644 tests/unit/config/logging/test_truncate_logging_filter.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b69e7f323..ca7c663ecc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **BREAKING**: Removed `Structure.embedding_driver`, set this via `griptape.config.config.drivers.embedding` instead. - **BREAKING**: Removed `Structure.custom_logger` and `Structure.logger_level`, set these via `griptape.config.config.logger` instead. - **BREAKING**: Removed `BaseStructureConfig.merge_config`. -- **BREAKING**: Renamed `StructureConfig` to `DriverConfig`, and renamed fields accordingly. +- **BREAKING**: Renamed `StructureConfig` to `DriverConfig`, moved to `griptape.config.drivers` and renamed fields accordingly. - **BREAKING**: `RagContext.output` was changed to `RagContext.outputs` to support multiple outputs. All relevant RAG modules were adjusted accordingly. - **BREAKING**: Removed before and after response modules from `ResponseRagStage`. - **BREAKING**: Moved ruleset and metadata ingestion from standalone modules to `PromptResponseRagModule`. diff --git a/docs/examples/src/multiple_agent_shared_memory_1.py b/docs/examples/src/multiple_agent_shared_memory_1.py index e156e531ad..11590df390 100644 --- a/docs/examples/src/multiple_agent_shared_memory_1.py +++ b/docs/examples/src/multiple_agent_shared_memory_1.py @@ -1,6 +1,7 @@ import os -from griptape.config import AzureOpenAiDriverConfig, config +from griptape.config import config +from griptape.config.drivers import AzureOpenAiDriverConfig from griptape.drivers import AzureMongoDbVectorStoreDriver, AzureOpenAiEmbeddingDriver from griptape.structures import Agent from griptape.tools import TaskMemoryTool, WebScraperTool diff --git a/docs/examples/src/talk_to_a_video_1.py b/docs/examples/src/talk_to_a_video_1.py index 3538c90712..377e177a68 100644 --- a/docs/examples/src/talk_to_a_video_1.py +++ b/docs/examples/src/talk_to_a_video_1.py @@ -3,7 +3,8 @@ import google.generativeai as genai from griptape.artifacts import GenericArtifact, TextArtifact -from griptape.config import GoogleDriverConfig, config +from griptape.config import config +from griptape.config.drivers import GoogleDriverConfig from griptape.structures import Agent config.drivers = GoogleDriverConfig() diff --git a/docs/griptape-framework/drivers/src/embedding_drivers_10.py b/docs/griptape-framework/drivers/src/embedding_drivers_10.py index 4f7560c991..a27e60298c 100644 --- a/docs/griptape-framework/drivers/src/embedding_drivers_10.py +++ b/docs/griptape-framework/drivers/src/embedding_drivers_10.py @@ -1,4 +1,5 @@ -from griptape.config import DriverConfig, config +from griptape.config import config +from griptape.config.drivers import DriverConfig from griptape.drivers import ( OpenAiChatPromptDriver, VoyageAiEmbeddingDriver, diff --git a/docs/griptape-framework/drivers/src/event_listener_drivers_4.py b/docs/griptape-framework/drivers/src/event_listener_drivers_4.py index a70e05d79f..7a0957e63f 100644 --- a/docs/griptape-framework/drivers/src/event_listener_drivers_4.py +++ b/docs/griptape-framework/drivers/src/event_listener_drivers_4.py @@ -1,6 +1,7 @@ import os -from griptape.config import DriverConfig, config +from griptape.config import config +from griptape.config.drivers import DriverConfig from griptape.drivers import AwsIotCoreEventListenerDriver, OpenAiChatPromptDriver from griptape.events import EventListener, FinishStructureRunEvent, event_bus from griptape.rules import Rule diff --git a/docs/griptape-framework/structures/config.md b/docs/griptape-framework/structures/config.md index 89399f60c9..67721ebb15 100644 --- a/docs/griptape-framework/structures/config.md +++ b/docs/griptape-framework/structures/config.md @@ -5,15 +5,17 @@ search: ## Overview -The [DriverConfig](../../reference/griptape/config/driver_config.md) class allows for the customization of Structures within Griptape, enabling specific settings such as Drivers to be defined for Tasks. +Griptape exposes global configuration options to easily customize different parts of the framework. -### Premade Configs +### Driver Configs -Griptape provides predefined [DriverConfig](../../reference/griptape/config/driver_config.md)'s for widely used services that provide APIs for most Driver types Griptape offers. +The [DriverConfig](../../reference/griptape/config/drivers/driver_config.md) class allows for the customization of Structures within Griptape, enabling specific settings such as Drivers to be defined for Tasks. + +Griptape provides predefined [DriverConfig](../../reference/griptape/config/drivers/driver_config.md)'s for widely used services that provide APIs for most Driver types Griptape offers. #### OpenAI -The [OpenAI Driver config](../../reference/griptape/config/openai_driver_config.md) provides default Drivers for OpenAI's APIs. This is the default config for all Structures. +The [OpenAI Driver config](../../reference/griptape/config/drivers/openai_driver_config.md) provides default Drivers for OpenAI's APIs. This is the default config for all Structures. ```python --8<-- "docs/griptape-framework/structures/src/config_1.py" @@ -21,21 +23,21 @@ The [OpenAI Driver config](../../reference/griptape/config/openai_driver_config. #### Azure OpenAI -The [Azure OpenAI Driver config](../../reference/griptape/config/azure_openai_driver_config.md) provides default Drivers for Azure's OpenAI APIs. +The [Azure OpenAI Driver config](../../reference/griptape/config/drivers/azure_openai_driver_config.md) provides default Drivers for Azure's OpenAI APIs. ```python --8<-- "docs/griptape-framework/structures/src/config_2.py" ``` #### Amazon Bedrock -The [Amazon Bedrock Driver config](../../reference/griptape/config/amazon_bedrock_driver_config.md) provides default Drivers for Amazon Bedrock's APIs. +The [Amazon Bedrock Driver config](../../reference/griptape/config/drivers/amazon_bedrock_driver_config.md) provides default Drivers for Amazon Bedrock's APIs. ```python --8<-- "docs/griptape-framework/structures/src/config_3.py" ``` #### Google -The [Google Driver config](../../reference/griptape/config/google_driver_config.md) provides default Drivers for Google's Gemini APIs. +The [Google Driver config](../../reference/griptape/config/drivers/google_driver_config.md) provides default Drivers for Google's Gemini APIs. ```python --8<-- "docs/griptape-framework/structures/src/config_4.py" @@ -43,7 +45,7 @@ The [Google Driver config](../../reference/griptape/config/google_driver_config. #### Anthropic -The [Anthropic Driver config](../../reference/griptape/config/anthropic_driver_config.md) provides default Drivers for Anthropic's APIs. +The [Anthropic Driver config](../../reference/griptape/config/drivers/anthropic_driver_config.md) provides default Drivers for Anthropic's APIs. !!! info Anthropic does not provide an embeddings API which means you will need to use another service for embeddings. @@ -56,22 +58,30 @@ The [Anthropic Driver config](../../reference/griptape/config/anthropic_driver_c #### Cohere -The [Cohere Driver config](../../reference/griptape/config/cohere_driver_config.md) provides default Drivers for Cohere's APIs. +The [Cohere Driver config](../../reference/griptape/config/drivers/cohere_driver_config.md) provides default Drivers for Cohere's APIs. ```python --8<-- "docs/griptape-framework/structures/src/config_6.py" ``` -### Custom Configs +#### Custom -You can create your own [DriverConfig](../../reference/griptape/config/driver_config.md) by overriding relevant Drivers. -The [DriverConfig](../../reference/griptape/config/driver_config.md) class includes "Dummy" Drivers for all types, which throw a [DummyError](../../reference/griptape/exceptions/dummy_exception.md) if invoked without being overridden. +You can create your own [DriverConfig](../../reference/griptape/config/drivers/driver_config.md) by overriding relevant Drivers. +The [DriverConfig](../../reference/griptape/config/drivers/driver_config.md) class includes "Dummy" Drivers for all types, which throw a [DummyError](../../reference/griptape/exceptions/dummy_exception.md) if invoked without being overridden. This approach ensures that you are informed through clear error messages if you attempt to use Structures without proper Driver configurations. ```python --8<-- "docs/griptape-framework/structures/src/config_7.py" ``` +### Logging Config + +Griptape provides a predefined [LoggingConfig](../../reference/griptape/config/logging/logging_config.md)'s for easily customizing the logging events that the framework emits. In order to customize the logger, the logger can be fetched by using the `config.logging.logger_name`. + +```python +--8<-- "docs/griptape-framework/structures/src/config_logging.py" +``` + ### Loading/Saving Configs ```python diff --git a/docs/griptape-framework/structures/src/config_1.py b/docs/griptape-framework/structures/src/config_1.py index 0c7a5ed9ea..df75488dcd 100644 --- a/docs/griptape-framework/structures/src/config_1.py +++ b/docs/griptape-framework/structures/src/config_1.py @@ -1,4 +1,5 @@ -from griptape.config import OpenAiDriverConfig, config +from griptape.config import config +from griptape.config.drivers import OpenAiDriverConfig from griptape.structures import Agent config.drivers = OpenAiDriverConfig() diff --git a/docs/griptape-framework/structures/src/config_2.py b/docs/griptape-framework/structures/src/config_2.py index a5f8efbbee..6fcdedbc8b 100644 --- a/docs/griptape-framework/structures/src/config_2.py +++ b/docs/griptape-framework/structures/src/config_2.py @@ -1,6 +1,7 @@ import os -from griptape.config import AzureOpenAiDriverConfig, config +from griptape.config import config +from griptape.config.drivers import AzureOpenAiDriverConfig from griptape.structures import Agent config.drivers = AzureOpenAiDriverConfig( diff --git a/docs/griptape-framework/structures/src/config_3.py b/docs/griptape-framework/structures/src/config_3.py index 6b3f51a766..e4e33e3797 100644 --- a/docs/griptape-framework/structures/src/config_3.py +++ b/docs/griptape-framework/structures/src/config_3.py @@ -2,7 +2,8 @@ import boto3 -from griptape.config import AmazonBedrockDriverConfig, config +from griptape.config import config +from griptape.config.drivers import AmazonBedrockDriverConfig from griptape.structures import Agent config.drivers = AmazonBedrockDriverConfig( diff --git a/docs/griptape-framework/structures/src/config_4.py b/docs/griptape-framework/structures/src/config_4.py index 5362b8c6b3..7ab5eee701 100644 --- a/docs/griptape-framework/structures/src/config_4.py +++ b/docs/griptape-framework/structures/src/config_4.py @@ -1,4 +1,5 @@ -from griptape.config import GoogleDriverConfig, config +from griptape.config import config +from griptape.config.drivers import GoogleDriverConfig from griptape.structures import Agent config.drivers = GoogleDriverConfig() diff --git a/docs/griptape-framework/structures/src/config_5.py b/docs/griptape-framework/structures/src/config_5.py index 4f787a922f..bee5050c26 100644 --- a/docs/griptape-framework/structures/src/config_5.py +++ b/docs/griptape-framework/structures/src/config_5.py @@ -1,4 +1,5 @@ -from griptape.config import AnthropicDriverConfig, config +from griptape.config import config +from griptape.config.drivers import AnthropicDriverConfig from griptape.structures import Agent config.drivers = AnthropicDriverConfig() diff --git a/docs/griptape-framework/structures/src/config_6.py b/docs/griptape-framework/structures/src/config_6.py index c265024015..5690001802 100644 --- a/docs/griptape-framework/structures/src/config_6.py +++ b/docs/griptape-framework/structures/src/config_6.py @@ -1,6 +1,7 @@ import os -from griptape.config import CohereDriverConfig, config +from griptape.config import config +from griptape.config.drivers import CohereDriverConfig from griptape.structures import Agent config.drivers = CohereDriverConfig(api_key=os.environ["COHERE_API_KEY"]) diff --git a/docs/griptape-framework/structures/src/config_7.py b/docs/griptape-framework/structures/src/config_7.py index 6b285f0e58..9f464b167d 100644 --- a/docs/griptape-framework/structures/src/config_7.py +++ b/docs/griptape-framework/structures/src/config_7.py @@ -1,6 +1,7 @@ import os -from griptape.config import DriverConfig, config +from griptape.config import config +from griptape.config.drivers import DriverConfig from griptape.drivers import AnthropicPromptDriver from griptape.structures import Agent diff --git a/docs/griptape-framework/structures/src/config_8.py b/docs/griptape-framework/structures/src/config_8.py index 4f23e3eaa0..6bc87998c9 100644 --- a/docs/griptape-framework/structures/src/config_8.py +++ b/docs/griptape-framework/structures/src/config_8.py @@ -1,4 +1,5 @@ -from griptape.config import AmazonBedrockDriverConfig, config +from griptape.config import config +from griptape.config.drivers import AmazonBedrockDriverConfig from griptape.structures import Agent custom_config = AmazonBedrockDriverConfig() diff --git a/docs/griptape-framework/structures/src/config_logging.py b/docs/griptape-framework/structures/src/config_logging.py new file mode 100644 index 0000000000..81645d5e24 --- /dev/null +++ b/docs/griptape-framework/structures/src/config_logging.py @@ -0,0 +1,14 @@ +import logging + +from griptape.config import config +from griptape.config.drivers import OpenAiDriverConfig +from griptape.config.logging import TruncateLoggingFilter +from griptape.structures import Agent + +config.drivers = OpenAiDriverConfig() + +logger = logging.getLogger(config.logging.logger_name) +logger.setLevel(logging.ERROR) +logger.addFilter(TruncateLoggingFilter(max_log_length=100)) + +agent = Agent() diff --git a/docs/griptape-framework/structures/src/task_memory_6.py b/docs/griptape-framework/structures/src/task_memory_6.py index 3f4d14b0a1..8d39f0286a 100644 --- a/docs/griptape-framework/structures/src/task_memory_6.py +++ b/docs/griptape-framework/structures/src/task_memory_6.py @@ -1,8 +1,6 @@ from griptape.artifacts import TextArtifact -from griptape.config import ( - OpenAiDriverConfig, - config, -) +from griptape.config import config +from griptape.config.drivers import OpenAiDriverConfig from griptape.drivers import ( LocalVectorStoreDriver, OpenAiChatPromptDriver, diff --git a/docs/griptape-tools/official-tools/src/rest_api_tool_1.py b/docs/griptape-tools/official-tools/src/rest_api_tool_1.py index 2093163b72..4ef73dd9de 100644 --- a/docs/griptape-tools/official-tools/src/rest_api_tool_1.py +++ b/docs/griptape-tools/official-tools/src/rest_api_tool_1.py @@ -1,6 +1,7 @@ from json import dumps -from griptape.config import DriverConfig, config +from griptape.config import config +from griptape.config.drivers import DriverConfig from griptape.drivers import OpenAiChatPromptDriver from griptape.memory.structure import ConversationMemory from griptape.structures import Pipeline diff --git a/griptape/config/__init__.py b/griptape/config/__init__.py index 4a87c2d015..043d152bad 100644 --- a/griptape/config/__init__.py +++ b/griptape/config/__init__.py @@ -1,28 +1,8 @@ from .base_config import BaseConfig - -from .base_driver_config import BaseDriverConfig - -from .driver_config import DriverConfig -from .openai_driver_config import OpenAiDriverConfig -from .azure_openai_driver_config import AzureOpenAiDriverConfig -from .amazon_bedrock_driver_config import AmazonBedrockDriverConfig -from .anthropic_driver_config import AnthropicDriverConfig -from .google_driver_config import GoogleDriverConfig -from .cohere_driver_config import CohereDriverConfig -from .logging_config import LoggingConfig from .config import config __all__ = [ "BaseConfig", - "BaseDriverConfig", - "DriverConfig", - "OpenAiDriverConfig", - "AzureOpenAiDriverConfig", - "AmazonBedrockDriverConfig", - "AnthropicDriverConfig", - "GoogleDriverConfig", - "CohereDriverConfig", - "LoggingConfig", "config", ] diff --git a/griptape/config/base_config.py b/griptape/config/base_config.py index ef62a4e9b1..7ed00e4459 100644 --- a/griptape/config/base_config.py +++ b/griptape/config/base_config.py @@ -8,8 +8,8 @@ from griptape.mixins.serializable_mixin import SerializableMixin if TYPE_CHECKING: - from .base_driver_config import BaseDriverConfig - from .logging_config import LoggingConfig + from .drivers.base_driver_config import BaseDriverConfig + from .logging.logging_config import LoggingConfig @define(kw_only=True) diff --git a/griptape/config/config.py b/griptape/config/config.py index 7b70df409a..11c2f95855 100644 --- a/griptape/config/config.py +++ b/griptape/config/config.py @@ -5,11 +5,11 @@ from attrs import define, field from .base_config import BaseConfig -from .logging_config import LoggingConfig -from .openai_driver_config import OpenAiDriverConfig +from .drivers.openai_driver_config import OpenAiDriverConfig +from .logging.logging_config import LoggingConfig if TYPE_CHECKING: - from .base_driver_config import BaseDriverConfig + from .drivers.base_driver_config import BaseDriverConfig @define(kw_only=True) diff --git a/griptape/config/drivers/__init__.py b/griptape/config/drivers/__init__.py new file mode 100644 index 0000000000..9d5f2f5104 --- /dev/null +++ b/griptape/config/drivers/__init__.py @@ -0,0 +1,20 @@ +from .base_driver_config import BaseDriverConfig +from .driver_config import DriverConfig + +from .openai_driver_config import OpenAiDriverConfig +from .azure_openai_driver_config import AzureOpenAiDriverConfig +from .amazon_bedrock_driver_config import AmazonBedrockDriverConfig +from .anthropic_driver_config import AnthropicDriverConfig +from .google_driver_config import GoogleDriverConfig +from .cohere_driver_config import CohereDriverConfig + +__all__ = [ + "BaseDriverConfig", + "DriverConfig", + "OpenAiDriverConfig", + "AzureOpenAiDriverConfig", + "AmazonBedrockDriverConfig", + "AnthropicDriverConfig", + "GoogleDriverConfig", + "CohereDriverConfig", +] diff --git a/griptape/config/amazon_bedrock_driver_config.py b/griptape/config/drivers/amazon_bedrock_driver_config.py similarity index 98% rename from griptape/config/amazon_bedrock_driver_config.py rename to griptape/config/drivers/amazon_bedrock_driver_config.py index a073006388..ea540b391f 100644 --- a/griptape/config/amazon_bedrock_driver_config.py +++ b/griptape/config/drivers/amazon_bedrock_driver_config.py @@ -4,7 +4,7 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers import ( AmazonBedrockImageGenerationDriver, AmazonBedrockImageQueryDriver, diff --git a/griptape/config/anthropic_driver_config.py b/griptape/config/drivers/anthropic_driver_config.py similarity index 96% rename from griptape/config/anthropic_driver_config.py rename to griptape/config/drivers/anthropic_driver_config.py index 642a3fced2..0c4524159e 100644 --- a/griptape/config/anthropic_driver_config.py +++ b/griptape/config/drivers/anthropic_driver_config.py @@ -1,6 +1,6 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers import ( AnthropicImageQueryDriver, AnthropicPromptDriver, diff --git a/griptape/config/azure_openai_driver_config.py b/griptape/config/drivers/azure_openai_driver_config.py similarity index 98% rename from griptape/config/azure_openai_driver_config.py rename to griptape/config/drivers/azure_openai_driver_config.py index c987a31b56..bcb173fbd9 100644 --- a/griptape/config/azure_openai_driver_config.py +++ b/griptape/config/drivers/azure_openai_driver_config.py @@ -4,7 +4,7 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers import ( AzureOpenAiChatPromptDriver, AzureOpenAiEmbeddingDriver, diff --git a/griptape/config/base_driver_config.py b/griptape/config/drivers/base_driver_config.py similarity index 100% rename from griptape/config/base_driver_config.py rename to griptape/config/drivers/base_driver_config.py diff --git a/griptape/config/cohere_driver_config.py b/griptape/config/drivers/cohere_driver_config.py similarity index 95% rename from griptape/config/cohere_driver_config.py rename to griptape/config/drivers/cohere_driver_config.py index 7195f550fe..eb8a55ce49 100644 --- a/griptape/config/cohere_driver_config.py +++ b/griptape/config/drivers/cohere_driver_config.py @@ -1,6 +1,6 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers import ( BaseEmbeddingDriver, BasePromptDriver, diff --git a/griptape/config/driver_config.py b/griptape/config/drivers/driver_config.py similarity index 97% rename from griptape/config/driver_config.py rename to griptape/config/drivers/driver_config.py index 3255912588..d342ecb0ae 100644 --- a/griptape/config/driver_config.py +++ b/griptape/config/drivers/driver_config.py @@ -4,7 +4,7 @@ from attrs import Factory, define, field -from griptape.config import BaseDriverConfig +from griptape.config.drivers import BaseDriverConfig from griptape.drivers import ( DummyAudioTranscriptionDriver, DummyEmbeddingDriver, diff --git a/griptape/config/google_driver_config.py b/griptape/config/drivers/google_driver_config.py similarity index 94% rename from griptape/config/google_driver_config.py rename to griptape/config/drivers/google_driver_config.py index a1089f0ee3..6f4243f016 100644 --- a/griptape/config/google_driver_config.py +++ b/griptape/config/drivers/google_driver_config.py @@ -1,6 +1,6 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers import ( BaseEmbeddingDriver, BasePromptDriver, diff --git a/griptape/config/openai_driver_config.py b/griptape/config/drivers/openai_driver_config.py similarity index 97% rename from griptape/config/openai_driver_config.py rename to griptape/config/drivers/openai_driver_config.py index 35ccde43d4..0b05d16364 100644 --- a/griptape/config/openai_driver_config.py +++ b/griptape/config/drivers/openai_driver_config.py @@ -1,6 +1,6 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers import ( BaseAudioTranscriptionDriver, BaseEmbeddingDriver, diff --git a/griptape/config/logging/__init__.py b/griptape/config/logging/__init__.py new file mode 100644 index 0000000000..de77260607 --- /dev/null +++ b/griptape/config/logging/__init__.py @@ -0,0 +1,5 @@ +from .logging_config import LoggingConfig +from .truncate_logging_filter import TruncateLoggingFilter +from .newline_logging_filter import NewlineLoggingFilter + +__all__ = ["LoggingConfig", "TruncateLoggingFilter", "NewlineLoggingFilter"] diff --git a/griptape/config/logging_config.py b/griptape/config/logging/logging_config.py similarity index 53% rename from griptape/config/logging_config.py rename to griptape/config/logging/logging_config.py index 0c0fcc020c..80497d7c83 100644 --- a/griptape/config/logging_config.py +++ b/griptape/config/logging/logging_config.py @@ -9,16 +9,9 @@ @define class LoggingConfig: logger_name: str = field(default="griptape", kw_only=True) - logger_level: int = field( - default=logging.INFO, - kw_only=True, - on_setattr=lambda self, _, value: logging.getLogger(self.logger_name).setLevel(value), - ) def __attrs_post_init__(self) -> None: logger = logging.getLogger(self.logger_name) - + logger.setLevel(logging.INFO) logger.propagate = False - logger.setLevel(self.logger_level) - - logger.handlers = [RichHandler(show_time=True, show_path=False)] + logger.addHandler(RichHandler(show_time=True, show_path=False)) diff --git a/griptape/config/logging/newline_logging_filter.py b/griptape/config/logging/newline_logging_filter.py new file mode 100644 index 0000000000..bae08265fe --- /dev/null +++ b/griptape/config/logging/newline_logging_filter.py @@ -0,0 +1,13 @@ +import logging +from typing import Any + +from attrs import define, field + + +@define +class NewlineLoggingFilter(logging.Filter): + replace_str: str = field(default=" ", kw_only=True) + + def filter(self, record: Any) -> bool: + record.msg = record.msg.replace("\n", self.replace_str) + return True diff --git a/griptape/config/logging/truncate_logging_filter.py b/griptape/config/logging/truncate_logging_filter.py new file mode 100644 index 0000000000..9888fc169d --- /dev/null +++ b/griptape/config/logging/truncate_logging_filter.py @@ -0,0 +1,17 @@ +import logging +from typing import Any + +from attrs import define, field + + +@define +class TruncateLoggingFilter(logging.Filter): + max_log_length: int = field(default=1000, kw_only=True) + + def filter(self, record: Any) -> bool: + message = record.getMessage() + + if len(message) > self.max_log_length: + record.msg = f"{message[:self.max_log_length]}... [{len(message) - self.max_log_length} more characters]" + record.args = () + return True diff --git a/tests/mocks/mock_driver_config.py b/tests/mocks/mock_driver_config.py index 6b152721d6..20f2ac6848 100644 --- a/tests/mocks/mock_driver_config.py +++ b/tests/mocks/mock_driver_config.py @@ -1,6 +1,6 @@ from attrs import Factory, define, field -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig from griptape.drivers.vector.local_vector_store_driver import LocalVectorStoreDriver from tests.mocks.mock_embedding_driver import MockEmbeddingDriver from tests.mocks.mock_image_generation_driver import MockImageGenerationDriver diff --git a/tests/unit/config/drivers/__init__.py b/tests/unit/config/drivers/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/config/test_amazon_bedrock_driver_config.py b/tests/unit/config/drivers/test_amazon_bedrock_driver_config.py similarity index 98% rename from tests/unit/config/test_amazon_bedrock_driver_config.py rename to tests/unit/config/drivers/test_amazon_bedrock_driver_config.py index 57a80809e3..a76eeb2787 100644 --- a/tests/unit/config/test_amazon_bedrock_driver_config.py +++ b/tests/unit/config/drivers/test_amazon_bedrock_driver_config.py @@ -1,7 +1,7 @@ import boto3 import pytest -from griptape.config import AmazonBedrockDriverConfig +from griptape.config.drivers import AmazonBedrockDriverConfig from tests.utils.aws import mock_aws_credentials diff --git a/tests/unit/config/test_anthropic_driver_config.py b/tests/unit/config/drivers/test_anthropic_driver_config.py similarity index 96% rename from tests/unit/config/test_anthropic_driver_config.py rename to tests/unit/config/drivers/test_anthropic_driver_config.py index a2ccbd25b9..a496c47b79 100644 --- a/tests/unit/config/test_anthropic_driver_config.py +++ b/tests/unit/config/drivers/test_anthropic_driver_config.py @@ -1,6 +1,6 @@ import pytest -from griptape.config import AnthropicDriverConfig +from griptape.config.drivers import AnthropicDriverConfig class TestAnthropicDriverConfig: diff --git a/tests/unit/config/test_azure_openai_driver_config.py b/tests/unit/config/drivers/test_azure_openai_driver_config.py similarity index 98% rename from tests/unit/config/test_azure_openai_driver_config.py rename to tests/unit/config/drivers/test_azure_openai_driver_config.py index 3c88b859d7..ef418e0975 100644 --- a/tests/unit/config/test_azure_openai_driver_config.py +++ b/tests/unit/config/drivers/test_azure_openai_driver_config.py @@ -1,6 +1,6 @@ import pytest -from griptape.config import AzureOpenAiDriverConfig +from griptape.config.drivers import AzureOpenAiDriverConfig class TestAzureOpenAiDriverConfig: diff --git a/tests/unit/config/test_cohere_driver_config.py b/tests/unit/config/drivers/test_cohere_driver_config.py similarity index 96% rename from tests/unit/config/test_cohere_driver_config.py rename to tests/unit/config/drivers/test_cohere_driver_config.py index 9e8407d842..5a75c98cd7 100644 --- a/tests/unit/config/test_cohere_driver_config.py +++ b/tests/unit/config/drivers/test_cohere_driver_config.py @@ -1,6 +1,6 @@ import pytest -from griptape.config import CohereDriverConfig +from griptape.config.drivers import CohereDriverConfig class TestCohereDriverConfig: diff --git a/tests/unit/config/test_driver_config.py b/tests/unit/config/drivers/test_driver_config.py similarity index 96% rename from tests/unit/config/test_driver_config.py rename to tests/unit/config/drivers/test_driver_config.py index dd3fd1a476..71220646f5 100644 --- a/tests/unit/config/test_driver_config.py +++ b/tests/unit/config/drivers/test_driver_config.py @@ -1,6 +1,6 @@ import pytest -from griptape.config import DriverConfig +from griptape.config.drivers import DriverConfig class TestDriverConfig: diff --git a/tests/unit/config/test_google_driver_config.py b/tests/unit/config/drivers/test_google_driver_config.py similarity index 97% rename from tests/unit/config/test_google_driver_config.py rename to tests/unit/config/drivers/test_google_driver_config.py index fb6cd23b54..3a16173b54 100644 --- a/tests/unit/config/test_google_driver_config.py +++ b/tests/unit/config/drivers/test_google_driver_config.py @@ -1,6 +1,6 @@ import pytest -from griptape.config import GoogleDriverConfig +from griptape.config.drivers import GoogleDriverConfig class TestGoogleDriverConfig: diff --git a/tests/unit/config/test_openai_driver_config.py b/tests/unit/config/drivers/test_openai_driver_config.py similarity index 98% rename from tests/unit/config/test_openai_driver_config.py rename to tests/unit/config/drivers/test_openai_driver_config.py index 55156730c5..860f705185 100644 --- a/tests/unit/config/test_openai_driver_config.py +++ b/tests/unit/config/drivers/test_openai_driver_config.py @@ -1,6 +1,6 @@ import pytest -from griptape.config import OpenAiDriverConfig +from griptape.config.drivers import OpenAiDriverConfig class TestOpenAiDriverConfig: diff --git a/tests/unit/config/logging/__init__.py b/tests/unit/config/logging/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/config/logging/test_newline_logging_filter.py b/tests/unit/config/logging/test_newline_logging_filter.py new file mode 100644 index 0000000000..d5b05e3238 --- /dev/null +++ b/tests/unit/config/logging/test_newline_logging_filter.py @@ -0,0 +1,20 @@ +import io +import logging +from contextlib import redirect_stdout + +from griptape.config import config +from griptape.config.logging import NewlineLoggingFilter +from griptape.structures import Agent + + +class TestNewlineLoggingFilter: + def test_filter(self): + # use the filter in an Agent + logger = logging.getLogger(config.logging.logger_name) + logger.addFilter(NewlineLoggingFilter(replace_str="$$$")) + agent = Agent() + # use a context manager to capture the stdout + with io.StringIO() as buf, redirect_stdout(buf): + agent.run() + output = buf.getvalue() + assert "$$$" in output diff --git a/tests/unit/config/logging/test_truncate_logging_filter.py b/tests/unit/config/logging/test_truncate_logging_filter.py new file mode 100644 index 0000000000..fc0aa1c47f --- /dev/null +++ b/tests/unit/config/logging/test_truncate_logging_filter.py @@ -0,0 +1,20 @@ +import io +import logging +from contextlib import redirect_stdout + +from griptape.config import config +from griptape.config.logging import TruncateLoggingFilter +from griptape.structures import Agent + + +class TestTruncateLoggingFilter: + def test_filter(self): + # use the filter in an Agent + logger = logging.getLogger(config.logging.logger_name) + logger.addFilter(TruncateLoggingFilter(max_log_length=0)) + agent = Agent() + # use a context manager to capture the stdout + with io.StringIO() as buf, redirect_stdout(buf): + agent.run("test") + output = buf.getvalue() + assert "more characters]" in output diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index ecd02e1a18..04d5586d25 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -1,12 +1,13 @@ import pytest -from griptape.config.openai_driver_config import OpenAiDriverConfig +from griptape.config.drivers import OpenAiDriverConfig class TestConfig: @pytest.mark.skip_mock_config() def test_init(self): - from griptape.config import LoggingConfig, config + from griptape.config import config + from griptape.config.logging import LoggingConfig assert isinstance(config.drivers, OpenAiDriverConfig) assert isinstance(config.logging, LoggingConfig)