-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extraction Engine Improvements (#1097)
- Loading branch information
1 parent
9cd4199
commit 79f976b
Showing
13 changed files
with
114 additions
and
83 deletions.
There are no files selected for viewing
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
37 changes: 20 additions & 17 deletions
37
docs/griptape-framework/engines/src/extraction_engines_2.py
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
from schema import Schema | ||
import json | ||
|
||
from griptape.artifacts.list_artifact import ListArtifact | ||
from schema import Literal, Schema | ||
|
||
from griptape.artifacts import ErrorArtifact, ListArtifact | ||
from griptape.drivers import OpenAiChatPromptDriver | ||
from griptape.engines import JsonExtractionEngine | ||
from griptape.loaders import WebLoader | ||
|
||
# Define a schema for extraction | ||
user_schema = Schema({"users": [{"name": str, "age": int, "location": str}]}).json_schema("UserSchema") | ||
|
||
|
||
json_engine = JsonExtractionEngine( | ||
prompt_driver=OpenAiChatPromptDriver(model="gpt-3.5-turbo"), template_schema=user_schema | ||
prompt_driver=OpenAiChatPromptDriver(model="gpt-4o"), | ||
template_schema=Schema( | ||
{ | ||
Literal("model", description="Name of an LLM model."): str, | ||
Literal("notes", description="Any notes of substance about the model."): Schema([str]), | ||
} | ||
).json_schema("ProductSchema"), | ||
) | ||
|
||
# Define some unstructured data | ||
sample_json_text = """ | ||
Alice (Age 28) lives in New York. | ||
Bob (Age 35) lives in California. | ||
""" | ||
# Load data from the web | ||
web_data = WebLoader().load("https://en.wikipedia.org/wiki/Large_language_model") | ||
|
||
if isinstance(web_data, ErrorArtifact): | ||
raise Exception(web_data.value) | ||
|
||
# Extract data using the engine | ||
result = json_engine.extract(sample_json_text) | ||
result = json_engine.extract_artifacts(ListArtifact(web_data)) | ||
|
||
if isinstance(result, ListArtifact): | ||
for artifact in result.value: | ||
print(artifact.value) | ||
else: | ||
print(result.to_text()) | ||
for artifact in result: | ||
print(json.dumps(artifact.value, indent=2)) |
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
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
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