-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #644 from writer/feat-add-kg-block
feat: Add kg block
- Loading branch information
Showing
6 changed files
with
213 additions
and
32 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from typing import Any | ||
|
||
from writer.abstract import register_abstract_template | ||
from writer.blocks.base_block import WorkflowBlock | ||
from writer.ss_types import AbstractTemplate, WriterConfigurationError | ||
|
||
|
||
class WriterAddToKG(WorkflowBlock): | ||
|
||
@classmethod | ||
def register(cls, type: str): | ||
super(WriterAddToKG, cls).register(type) | ||
register_abstract_template(type, AbstractTemplate( | ||
baseType="workflows_node", | ||
writer={ | ||
"name": "Add to Knowledge Graph", | ||
"description": "Adds files to an existing knowledge graph.", | ||
"category": "Writer", | ||
"fields": { | ||
"graphId": { | ||
"name": "Graph id", | ||
"type": "Text", | ||
"desc": "The id for an existing knowledge graph. It has a UUID format." | ||
}, | ||
"files": { | ||
"name": "Files", | ||
"type": "Object", | ||
"default": "[]", | ||
"desc": "A list of files to be uploaded and added to the knowledge graph. You can use files uploaded via the File Input component or specify dictionaries with data, type and name." | ||
}, | ||
}, | ||
"outs": { | ||
"success": { | ||
"name": "Success", | ||
"description": "If the execution was successful.", | ||
"style": "success", | ||
}, | ||
"error": { | ||
"name": "Error", | ||
"description": "If the function raises an Exception.", | ||
"style": "error", | ||
}, | ||
}, | ||
} | ||
)) | ||
|
||
def _get_prepared_file(self, raw_file: Any): | ||
if not isinstance(raw_file, dict): | ||
raise WriterConfigurationError("Files must be dictionaries and contain `data`, `type` and `name` attributes.") | ||
|
||
if "data" not in raw_file or "type" not in raw_file or "name" not in raw_file: | ||
raise WriterConfigurationError("A file specified as a dictionary must contain `data`, `type` and `name` attributes.") | ||
|
||
return raw_file | ||
|
||
def run(self): | ||
try: | ||
import writer.ai | ||
|
||
graph_id = self._get_field("graphId", required=True) | ||
raw_files = self._get_field("files", as_json=True) | ||
prepared_files = [] | ||
|
||
if not isinstance(raw_files, list): | ||
raise WriterConfigurationError("Files must be a list.") | ||
|
||
for raw_file in raw_files: | ||
prepared_files.append(self._get_prepared_file(raw_file)) | ||
|
||
graph = writer.ai.retrieve_graph(graph_id) | ||
|
||
for prepared_file in prepared_files: | ||
file = writer.ai.upload_file(prepared_file.get("data"), | ||
prepared_file.get("type"), | ||
prepared_file.get("name")) | ||
graph.add_file(file) | ||
|
||
self.outcome = "success" | ||
except BaseException as e: | ||
self.outcome = "error" | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import pytest | ||
import writer.ai | ||
from writer.blocks.writeraddtokg import WriterAddToKG | ||
from writer.ss_types import WriterConfigurationError | ||
|
||
|
||
class MockFile(): | ||
pass | ||
|
||
class MockGraph(): | ||
|
||
def add_file(self, file): | ||
assert isinstance(file, MockFile) | ||
|
||
def mock_retrieve_graph(graph_id): | ||
assert graph_id == "abc123" | ||
return MockGraph() | ||
|
||
def mock_upload_file(data, type, name): | ||
assert data == b"123" | ||
assert type == "application/pdf" | ||
assert name == "interesting.pdf" | ||
return MockFile() | ||
|
||
|
||
def test_add_to_kg(session, runner): | ||
writer.ai.retrieve_graph = mock_retrieve_graph | ||
writer.ai.upload_file = mock_upload_file | ||
session.session_state["my_files"] = [ | ||
{ | ||
"data": b"123", | ||
"type": "application/pdf", | ||
"name": "interesting.pdf" | ||
}, | ||
{ | ||
"data": b"123", | ||
"type": "application/pdf", | ||
"name": "interesting.pdf" | ||
} | ||
] | ||
session.add_fake_component({ | ||
"graphId": "abc123", | ||
"files": "@{my_files}" | ||
}) | ||
block = WriterAddToKG("fake_id", runner, {}) | ||
block.run() | ||
assert block.outcome == "success" | ||
|
||
|
||
def test_add_to_kg_missing_type(session, runner): | ||
writer.ai.retrieve_graph = mock_retrieve_graph | ||
writer.ai.upload_file = mock_upload_file | ||
session.session_state["my_files"] = [ | ||
{ | ||
"data": b"123", | ||
"name": "interesting.pdf" | ||
} | ||
] | ||
session.add_fake_component({ | ||
"graphId": "abc123", | ||
"files": "@{my_files}" | ||
}) | ||
block = WriterAddToKG("fake_id", runner, {}) | ||
|
||
with pytest.raises(WriterConfigurationError): | ||
block.run() | ||
|
||
def test_add_to_kg_wrong_type(session, runner): | ||
writer.ai.retrieve_graph = mock_retrieve_graph | ||
writer.ai.upload_file = mock_upload_file | ||
session.session_state["my_files"] = "should be list" | ||
session.add_fake_component({ | ||
"graphId": "abc123", | ||
"files": "@{my_files}" | ||
}) | ||
block = WriterAddToKG("fake_id", runner, {}) | ||
|
||
with pytest.raises(WriterConfigurationError): | ||
block.run() |