From 3587258b1b61ec882b275a8bba177a657273defc Mon Sep 17 00:00:00 2001 From: Flavio Brasil Date: Fri, 22 Dec 2023 01:36:48 -0800 Subject: [PATCH] logic thoughts wip --- .../kyo/llm/thoughts/logic/Equivalence.scala | 8 +- .../scala/kyo/llm/thoughts/logic/Expr.scala | 26 ----- .../kyo/llm/thoughts/logic/Optimization.scala | 6 +- .../kyo/llm/thoughts/logic/Reduction.scala | 94 +++++++++++++++++-- 4 files changed, 94 insertions(+), 40 deletions(-) delete mode 100644 kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Expr.scala diff --git a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Equivalence.scala b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Equivalence.scala index 1a5e83641..dc54efab8 100644 --- a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Equivalence.scala +++ b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Equivalence.scala @@ -12,15 +12,15 @@ import kyo.llm.ais._ """ ) case class EquivalenceChecking( - initialExpr1: Expr, - initialExpr2: Expr, + initialExpr1: String, + initialExpr2: String, equivalenceSteps: List[EquivalenceStep], finalEquivalence: Boolean ) case class EquivalenceStep( stepDescription: String, - expr1Transformation: Expr, - expr2Transformation: Expr, + expr1Transformation: String, + expr2Transformation: String, areEquivalent: Boolean ) diff --git a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Expr.scala b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Expr.scala deleted file mode 100644 index 5898b4d0d..000000000 --- a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Expr.scala +++ /dev/null @@ -1,26 +0,0 @@ -package kyo.llm.thoughts.logic - -sealed trait Expr - -sealed trait BooleanExpr extends Expr -object BooleanExpr { - case object True extends BooleanExpr - case object False extends BooleanExpr - case class Not(BooleanExpr: BooleanExpr) extends BooleanExpr - case class And(left: BooleanExpr, right: BooleanExpr) extends BooleanExpr - case class Or(left: BooleanExpr, right: BooleanExpr) extends BooleanExpr - case class Var(name: String) extends BooleanExpr -} - -sealed trait MathExpr extends Expr - -object MathExpr { - case class Constant(value: Double) extends MathExpr - case class Variable(name: String) extends MathExpr - case class Add(left: MathExpr, right: MathExpr) extends MathExpr - case class Subtract(left: MathExpr, right: MathExpr) extends MathExpr - case class Multiply(left: MathExpr, right: MathExpr) extends MathExpr - case class Divide(left: MathExpr, right: MathExpr) extends MathExpr - case class Power(base: MathExpr, exponent: MathExpr) extends MathExpr - case class Root(MathExpression: MathExpr, degree: MathExpr) extends MathExpr -} diff --git a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Optimization.scala b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Optimization.scala index d6fee072c..e57bd9a66 100644 --- a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Optimization.scala +++ b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Optimization.scala @@ -10,13 +10,13 @@ import kyo.llm.ais._ - Results in an optimized expression that is logically equivalent to the original. """ ) -case class ExpressionOptimization( +case class ExpressionOptimization[Expr]( originalExpression: Expr, - optimizationSteps: List[OptimizationStep], + optimizationSteps: List[OptimizationStep[Expr]], optimizedExpression: Expr ) -case class OptimizationStep( +case class OptimizationStep[Expr]( stepDescription: String, inputExpression: Expr, outputExpression: Expr diff --git a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Reduction.scala b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Reduction.scala index b574ab55c..248de4a75 100644 --- a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Reduction.scala +++ b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/logic/Reduction.scala @@ -1,23 +1,103 @@ package kyo.llm.thoughts.logic +import kyo._ +import kyo.ios._ import kyo.llm.ais._ +import kyo.llm.KyoLLMApp +import kyo.llm.configs.Config +import scala.util.Random +import kyo.llm.thoughts.meta.Collect +import kyo.consoles.Consoles @desc( p""" - The Boolean thought applies algebraic principles to simplify a boolean expression. + The Reduction thought allows you to apply reduction steps to simplify an expression. - Outlines each step of the reduction process with rule descriptions and expression transformations. - - Iteratively simplifies the expression using boolean algebra rules. - - Aimed at reducing the expression to True or False. + - Iteratively simplifies the expression using rules. + - Normalize the input expression using prefix operators. Example: (&& false false) + - Apply reduction steps to produce the final outcome. + - **Do not produce fields with empty values** """ ) case class Reduction( - initialExpression: Expr, + initialExpression: String, + `Normalize the expression`: Boolean, + normalizedExpression: String, reductionSteps: List[ReductionStep], - finalOutcome: Expr + finalOutcome: String ) +@desc( + p""" + Make sure to use small steps to reason through + the reduction process. One for each sub-expression. + """ +) case class ReductionStep( ruleDescription: String, - inputExpression: Expr, - outputExpression: Expr + inputExpression: String, + outputExpression: String ) + +object tt extends KyoLLMApp { + + sealed trait BooleanExpr { + def eval: Boolean + def show: String + } + + case object True extends BooleanExpr { + def eval = true + def show = "true" + } + case object False extends BooleanExpr { + def eval = false + def show = "false" + } + case class Not(expr: BooleanExpr) extends BooleanExpr { + def eval = !expr.eval + def show = s"!(${expr.show})" + } + case class And(left: BooleanExpr, right: BooleanExpr) extends BooleanExpr { + def eval = left.eval && right.eval + def show = s"(${left.show} && ${right.show})" + } + case class Or(left: BooleanExpr, right: BooleanExpr) extends BooleanExpr { + def eval = left.eval || right.eval + def show = s"(${left.show} || ${right.show})" + } + + def gen(size: Int): BooleanExpr = + if (size <= 1) { + Random.nextBoolean() match { + case true => True + case false => False + } + } else { + Random.nextInt(3) match { + case 0 => And(gen(size / 2), gen(size / 2)) + case 1 => Or(gen(size / 2), gen(size / 2)) + case 2 => Not(gen(size - 1)) + } + } + + override def config: Config = + super.config.apiKey("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") + + run { + def loop(): Unit < AIs = + IOs { + val e = gen(5) + println(e.show) + AIs.gen[Reduction](e.show).map { r => + Consoles.println(r).andThen { + if (r.finalOutcome.toBoolean != e.eval) { + println("FAIL: " + e.show) + } + loop() + } + } + } + AIs.parallel(List.fill(5)(loop())) + } +}