-
Notifications
You must be signed in to change notification settings - Fork 163
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
9d9b643
commit 674a61a
Showing
15 changed files
with
164 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from griptape.rules import Rule, Ruleset | ||
from griptape.structures import Agent | ||
|
||
pipeline = Agent( | ||
rulesets=[ | ||
Ruleset( | ||
name="Personality", | ||
rules=[Rule("Talk like a pirate.")], | ||
), | ||
] | ||
) | ||
|
||
pipeline.run("Hi there! How are you?") |
14 changes: 14 additions & 0 deletions
14
docs/griptape-framework/structures/src/json_schema_rule.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import schema | ||
|
||
from griptape.rules.json_schema_rule import JsonSchemaRule | ||
from griptape.structures import Agent | ||
|
||
agent = Agent( | ||
rules=[ | ||
JsonSchemaRule( | ||
schema.Schema({"answer": str, "relevant_emojis": schema.Schema(["str"])}).json_schema("Output Format") | ||
) | ||
] | ||
) | ||
|
||
agent.run("What is the sentiment of this message?: 'I am so happy!'") |
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,5 +1,7 @@ | ||
from griptape.rules.base_rule import BaseRule | ||
from griptape.rules.rule import Rule | ||
from griptape.rules.json_schema_rule import JsonSchemaRule | ||
from griptape.rules.ruleset import Ruleset | ||
|
||
|
||
__all__ = ["Rule", "Ruleset"] | ||
__all__ = ["BaseRule", "Rule", "JsonSchemaRule", "Ruleset"] |
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,17 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
from attrs import define, field | ||
|
||
|
||
@define(frozen=True) | ||
class BaseRule(ABC): | ||
value: Any = field() | ||
|
||
def __str__(self) -> str: | ||
return self.to_text() | ||
|
||
@abstractmethod | ||
def to_text(self) -> str: ... |
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,17 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
|
||
from attrs import define, field | ||
|
||
from griptape.rules import BaseRule | ||
from griptape.utils import J2 | ||
|
||
|
||
@define(frozen=True) | ||
class JsonSchemaRule(BaseRule): | ||
value: dict = field() | ||
template_generator: J2 = field(default=J2("rules/json_schema.j2")) | ||
|
||
def to_text(self) -> str: | ||
return self.template_generator.render(json_schema=json.dumps(self.value)) |
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,8 +1,13 @@ | ||
from __future__ import annotations | ||
|
||
from attrs import define | ||
from attrs import define, field | ||
|
||
from griptape.rules import BaseRule | ||
|
||
|
||
@define(frozen=True) | ||
class Rule: | ||
value: str | ||
class Rule(BaseRule): | ||
value: str = field() | ||
|
||
def to_text(self) -> str: | ||
return self.value |
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,14 +1,14 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import TYPE_CHECKING, Sequence | ||
|
||
from attrs import define, field | ||
|
||
if TYPE_CHECKING: | ||
from griptape.rules import Rule | ||
from griptape.rules import BaseRule | ||
|
||
|
||
@define | ||
class Ruleset: | ||
name: str = field() | ||
rules: list[Rule] = field() | ||
rules: Sequence[BaseRule] = field() |
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 @@ | ||
You must respond with a JSON object that successfully validates against the following schema: {{json_schema}} |
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,32 @@ | ||
import json | ||
|
||
import schema | ||
|
||
from griptape.rules import JsonSchemaRule | ||
|
||
|
||
class TestJsonSchemaRule: | ||
def test_init(self): | ||
json_schema = schema.Schema({"type": "string"}).json_schema("test") | ||
rule = JsonSchemaRule(json_schema) | ||
assert rule.value == { | ||
"type": "object", | ||
"properties": {"type": {"const": "string"}}, | ||
"required": ["type"], | ||
"additionalProperties": False, | ||
"$id": "test", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
} | ||
|
||
def test_to_text(self): | ||
json_schema = schema.Schema({"type": "string"}).json_schema("test") | ||
rule = JsonSchemaRule(json_schema) | ||
assert ( | ||
rule.to_text() | ||
== f"You must respond with a JSON object that successfully validates against the following schema: {json.dumps(json_schema)}" | ||
) | ||
|
||
def test___str__(self): | ||
json_schema = schema.Schema({"type": "string"}).json_schema("test") | ||
rule = JsonSchemaRule(json_schema) | ||
assert str(rule) == rule.to_text() |
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