-
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.
- Loading branch information
1 parent
e92e085
commit 317ec50
Showing
12 changed files
with
64 additions
and
46 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
29 changes: 18 additions & 11 deletions
29
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,24 +1,31 @@ | ||
from schema import Schema | ||
import json | ||
|
||
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([{"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_text(sample_json_text) | ||
result = json_engine.extract_artifacts(ListArtifact(web_data)) | ||
|
||
for artifact in result: | ||
print(artifact.value) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Extract information from the Text. | ||
Extract information from the Text based on the Extraction Template JSON Schema into an array of JSON objects. | ||
Text: """{{ text }}""" | ||
|
||
JSON: | ||
JSON Array: |
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