Skip to content

Commit

Permalink
Remove many instances of catching exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Aug 23, 2024
1 parent 489453e commit 4e31e3d
Show file tree
Hide file tree
Showing 41 changed files with 225 additions and 371 deletions.
4 changes: 0 additions & 4 deletions docs/examples/src/load_query_and_chat_marqo_1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

from griptape import utils
from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import MarqoVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader
from griptape.structures import Agent
Expand All @@ -27,9 +26,6 @@
# Load artifacts from the web
artifacts = WebLoader().load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert the artifacts into the vector store
vector_store.upsert_text_artifacts(
{
Expand Down
3 changes: 0 additions & 3 deletions docs/examples/src/query_webpage_1.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import LocalVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

vector_store = LocalVectorStoreDriver(embedding_driver=OpenAiEmbeddingDriver(api_key=os.environ["OPENAI_API_KEY"]))

artifacts = WebLoader(max_tokens=100).load("https://www.griptape.ai")
if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

for a in artifacts:
vector_store.upsert_text_artifact(a, namespace="griptape")
Expand Down
4 changes: 1 addition & 3 deletions docs/examples/src/query_webpage_astra_db_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts import ErrorArtifact
from griptape.drivers import (
AstraDbVectorStoreDriver,
OpenAiChatPromptDriver,
Expand Down Expand Up @@ -45,8 +44,7 @@
)

artifacts = WebLoader(max_tokens=256).load(input_blogpost)
if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

vector_store_driver.upsert_text_artifacts({namespace: artifacts})

rag_tool = RagTool(
Expand Down
3 changes: 0 additions & 3 deletions docs/examples/src/talk_to_a_pdf_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import LocalVectorStoreDriver, OpenAiChatPromptDriver, OpenAiEmbeddingDriver
from griptape.engines.rag import RagEngine
from griptape.engines.rag.modules import PromptResponseRagModule, VectorStoreRetrievalRagModule
Expand Down Expand Up @@ -32,8 +31,6 @@
)

artifacts = PdfLoader().load(response.content)
if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

vector_store.upsert_text_artifacts({namespace: artifacts})

Expand Down
4 changes: 0 additions & 4 deletions docs/examples/src/talk_to_a_webpage_1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import LocalVectorStoreDriver, OpenAiChatPromptDriver, OpenAiEmbeddingDriver
from griptape.engines.rag import RagEngine
from griptape.engines.rag.modules import PromptResponseRagModule, VectorStoreRetrievalRagModule
Expand Down Expand Up @@ -28,9 +27,6 @@

artifacts = WebLoader().load("https://en.wikipedia.org/wiki/Physics")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

vector_store_driver.upsert_text_artifacts({namespace: artifacts})

rag_tool = RagTool(
Expand Down
3 changes: 0 additions & 3 deletions docs/griptape-framework/drivers/src/vector_store_drivers_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts import ErrorArtifact
from griptape.drivers import LocalVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

Expand All @@ -12,8 +11,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=100).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)
# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(a, namespace="griptape") for a in artifacts]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import OpenAiEmbeddingDriver, QdrantVectorStoreDriver
from griptape.loaders import WebLoader

Expand All @@ -22,9 +21,6 @@
# Load Artifacts from the web
artifacts = WebLoader().load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Recreate Qdrant collection
vector_store_driver.client.recreate_collection(
collection_name=vector_store_driver.collection_name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import AstraDbVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

Expand All @@ -23,9 +22,6 @@
# Load Artifacts from the web
artifacts = WebLoader().load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(a, namespace="griptape") for a in artifacts]

Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts import ErrorArtifact
from griptape.drivers import OpenAiEmbeddingDriver, PineconeVectorStoreDriver
from griptape.loaders import WebLoader

Expand All @@ -17,9 +16,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=100).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
[vector_store_driver.upsert_text_artifact(a, namespace="griptape") for a in artifacts]

Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_4.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts import ErrorArtifact
from griptape.drivers import MarqoVectorStoreDriver, OpenAiChatPromptDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

Expand All @@ -22,9 +21,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=200).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
vector_store_driver.upsert_text_artifacts(
{
Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_5.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import MongoDbAtlasVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

Expand Down Expand Up @@ -28,9 +27,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=200).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
vector_store_driver.upsert_text_artifacts(
{
Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_6.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import AzureMongoDbVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

Expand Down Expand Up @@ -28,9 +27,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=200).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
vector_store_driver.upsert_text_artifacts(
{
Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_7.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import OpenAiEmbeddingDriver, RedisVectorStoreDriver
from griptape.loaders import WebLoader

Expand All @@ -18,9 +17,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=200).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
vector_store_driver.upsert_text_artifacts(
{
Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_8.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import boto3

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import AmazonOpenSearchVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader

Expand All @@ -19,9 +18,6 @@
# Load Artifacts from the web
artifacts = WebLoader(max_tokens=200).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
vector_store_driver.upsert_text_artifacts(
{
Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/drivers/src/vector_store_drivers_9.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import OpenAiEmbeddingDriver, PgVectorVectorStoreDriver
from griptape.loaders import WebLoader

Expand All @@ -25,9 +24,6 @@
# Load Artifacts from the web
artifacts = WebLoader().load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

# Upsert Artifacts into the Vector Store Driver
vector_store_driver.upsert_text_artifacts(
{
Expand Down
3 changes: 0 additions & 3 deletions docs/griptape-framework/engines/src/rag_engines_1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from griptape.artifacts import ErrorArtifact
from griptape.drivers import LocalVectorStoreDriver, OpenAiChatPromptDriver, OpenAiEmbeddingDriver
from griptape.engines.rag import RagContext, RagEngine
from griptape.engines.rag.modules import PromptResponseRagModule, TranslateQueryRagModule, VectorStoreRetrievalRagModule
Expand All @@ -11,8 +10,6 @@
vector_store = LocalVectorStoreDriver(embedding_driver=OpenAiEmbeddingDriver())
artifacts = WebLoader(max_tokens=500).load("https://www.griptape.ai")

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

vector_store.upsert_text_artifacts(
{
Expand Down
4 changes: 0 additions & 4 deletions docs/griptape-framework/engines/src/summary_engines_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests

from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import OpenAiChatPromptDriver
from griptape.engines import PromptSummaryEngine
from griptape.loaders import PdfLoader
Expand All @@ -12,9 +11,6 @@

artifacts = PdfLoader().load(response.content)

if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

text = "\n\n".join([a.value for a in artifacts])

engine.summarize_text(text)
3 changes: 0 additions & 3 deletions docs/griptape-tools/official-tools/src/vector_store_tool_1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.drivers import LocalVectorStoreDriver, OpenAiEmbeddingDriver
from griptape.loaders import WebLoader
from griptape.structures import Agent
Expand All @@ -9,8 +8,6 @@
)

artifacts = WebLoader().load("https://www.griptape.ai")
if isinstance(artifacts, ErrorArtifact):
raise Exception(artifacts.value)

vector_store_driver.upsert_text_artifacts({"griptape": artifacts})
vector_db = VectorStoreTool(
Expand Down
63 changes: 21 additions & 42 deletions griptape/drivers/file_manager/base_file_manager_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,60 +41,39 @@ class BaseFileManagerDriver(ABC):
)

def list_files(self, path: str) -> TextArtifact | ErrorArtifact:
try:
entries = self.try_list_files(path)
return TextArtifact("\n".join(list(entries)))
except FileNotFoundError:
return ErrorArtifact("Path not found")
except NotADirectoryError:
return ErrorArtifact("Path is not a directory")
except Exception as e:
return ErrorArtifact(f"Failed to list files: {str(e)}")
entries = self.try_list_files(path)
return TextArtifact("\n".join(list(entries)))

@abstractmethod
def try_list_files(self, path: str) -> list[str]: ...

def load_file(self, path: str) -> BaseArtifact:
try:
extension = path.split(".")[-1]
loader = self.loaders.get(extension) or self.default_loader
source = self.try_load_file(path)
result = loader.load(source)

if isinstance(result, BaseArtifact):
return result
else:
return ListArtifact(result)
except FileNotFoundError:
return ErrorArtifact("Path not found")
except IsADirectoryError:
return ErrorArtifact("Path is a directory")
except NotADirectoryError:
return ErrorArtifact("Not a directory")
except Exception as e:
return ErrorArtifact(f"Failed to load file: {str(e)}")
extension = path.split(".")[-1]
loader = self.loaders.get(extension) or self.default_loader
source = self.try_load_file(path)
result = loader.load(source)

if isinstance(result, BaseArtifact):
return result
else:
return ListArtifact(result)

@abstractmethod
def try_load_file(self, path: str) -> bytes: ...

def save_file(self, path: str, value: bytes | str) -> InfoArtifact | ErrorArtifact:
try:
extension = path.split(".")[-1]
loader = self.loaders.get(extension) or self.default_loader
encoding = None if loader is None else loader.encoding
def save_file(self, path: str, value: bytes | str) -> InfoArtifact:
extension = path.split(".")[-1]
loader = self.loaders.get(extension) or self.default_loader
encoding = None if loader is None else loader.encoding

if isinstance(value, str):
value = value.encode() if encoding is None else value.encode(encoding=encoding)
elif isinstance(value, (bytearray, memoryview)):
raise ValueError(f"Unsupported type: {type(value)}")
if isinstance(value, str):
value = value.encode() if encoding is None else value.encode(encoding=encoding)
elif isinstance(value, (bytearray, memoryview)):
raise ValueError(f"Unsupported type: {type(value)}")

self.try_save_file(path, value)
self.try_save_file(path, value)

return InfoArtifact("Successfully saved file")
except IsADirectoryError:
return ErrorArtifact("Path is a directory")
except Exception as e:
return ErrorArtifact(f"Failed to save file: {str(e)}")
return InfoArtifact("Successfully saved file")

@abstractmethod
def try_save_file(self, path: str, value: bytes) -> None: ...
Loading

0 comments on commit 4e31e3d

Please sign in to comment.