@@ -12,13 +12,18 @@ class PromptValidationSpec extends AnyWordSpec with Matchers {
1212 case class Person (name : String , age : Int )
1313 object Person {
1414 given validator : PromptValidator [Person ] =
15- PromptValidator .from((person : Person ) => IO .apply(! person.name.trim().isEmpty() && person.age >= 18 ))
16-
17- import PromptValidator .unsafe .acceptPrompt
18- import PromptValidator .unsafe .acceptString
15+ PromptValidator .pure(person => ! person.name.trim().isEmpty() && person.age >= 18 )
1916
2017 given promptEncoder : PromptEncoder [Person ] =
21- PromptEncoder .from((person : Person ) => prompt """ Person name: ${person.name.unsafeLiteral} and age: ${person.age.toString.unsafeLiteral}""" )
18+ given stringValidator : PromptValidator [String ] = PromptValidator .unsafe.acceptString
19+ given intValidator : PromptValidator [Int ] = PromptValidator .pure(_ => true )
20+ given stringEncoder : PromptEncoder [String ] = PromptEncoder .unsafe.string
21+ given intEncoder : PromptEncoder [Int ] = PromptEncoder .from(_.toString.unsafeLiteral)
22+ // TODO: rethink this
23+ // why do we need validators to create encoders?
24+ // we should capture the input arg types and expect the validators when we render the prompt later
25+ // feels a lot like ZIO like abstraction where we capture the input requirements and provide them later
26+ PromptEncoder .from(person => prompt """ Person name: ${person.name} and age: ${person.age}""" )
2227 }
2328
2429 lazy val grownUpPerson : Person = Person (" John" , 30 ) // should pass
@@ -28,7 +33,7 @@ class PromptValidationSpec extends AnyWordSpec with Matchers {
2833 " Prompt.validate method" should {
2934 " pass when it is a constant (literal) string" in {
3035 given stringValidator : PromptValidator [String ] =
31- PromptValidator .from(( str : String ) => IO .apply(str. length > 0 ) )
36+ PromptValidator .pure(_. length > 0 )
3237
3338 val p : Prompt = prompt """ You are a helpful assistant. """
3439
@@ -37,7 +42,7 @@ class PromptValidationSpec extends AnyWordSpec with Matchers {
3742
3843 " fail when the string validator fails with a constant string" in {
3944 given stringValidator : PromptValidator [String ] =
40- PromptValidator .from(( str : String ) => IO .apply( ! str .toLowerCase().contains(" bomb" ) ))
45+ PromptValidator .pure( ! _ .toLowerCase().contains(" bomb" ))
4146
4247 val p : Prompt = prompt """ You are a bomb assistant. """
4348
@@ -46,7 +51,7 @@ class PromptValidationSpec extends AnyWordSpec with Matchers {
4651
4752 " fail when the string validator fails when the parts are ok but not the sum of them" in {
4853 given stringValidator : PromptValidator [String ] =
49- PromptValidator .from(( str : String ) => IO .apply( ! str .toLowerCase().contains(" bomb" ) ))
54+ PromptValidator .pure( ! _ .toLowerCase().contains(" bomb" ))
5055
5156 val p : Prompt = prompt """ |
5257 |You are an expert engineer. I have all the materials for the task in question.
@@ -99,7 +104,7 @@ class PromptValidationSpec extends AnyWordSpec with Matchers {
99104 import PromptValidator .unsafe .acceptPrompt
100105
101106 given promptEncoder : PromptEncoder [WithoutValidator ] =
102- PromptEncoder .from(( withoutValidator : WithoutValidator ) => prompt """ This is a string value: ${withoutValidator.value.unsafeLiteral}""" )
107+ PromptEncoder .from(withoutValidator => prompt """ This is a string value: ${withoutValidator.value.unsafeLiteral}""" )
103108 }
104109
105110 val withoutValidatorInstance : WithoutValidator = WithoutValidator (" John" )
@@ -110,8 +115,7 @@ class PromptValidationSpec extends AnyWordSpec with Matchers {
110115 " fail compilation if the type has no prompt encoder" in {
111116 case class WithoutPromptEncoder (value : String )
112117 object WithoutPromptEncoder {
113- given validator : PromptValidator [WithoutPromptEncoder ] =
114- PromptValidator .from((withoutPromptEncoder : WithoutPromptEncoder ) => IO .apply(true )) // dummy validator
118+ given validator : PromptValidator [WithoutPromptEncoder ] = PromptValidator .accept[WithoutPromptEncoder ]
115119 }
116120
117121 val withoutPromptEncoderInstance : WithoutPromptEncoder = WithoutPromptEncoder (" John" )
0 commit comments