Skip to content

Commit

Permalink
thought eval wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Dec 30, 2023
1 parent 5e77e37 commit faabde5
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ case class Brainstorm(
`Reflect on the user's intent`: String,
`Apply lateral thinking for new perspectives`: String,
`Consider alternative innovative solutions`: String
) extends Thought.Opening
) extends Thought
92 changes: 92 additions & 0 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Check.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package kyo.llm.thoughts

import kyo.llm.ais._
import zio.schema.{Schema => ZSchema}
import kyo._
import kyo.stats.Stats
import kyo.stats.Attributes
import kyo.ios.IOs

object Check {

private val stats = Thought.stats.scope("checks")
private val success = stats.initCounter("success")
private val failure = stats.initCounter("failure")

case class CheckFailed(path: List[String], invariant: String, analysis: String)
extends RuntimeException

private def observe(path: List[String], result: Boolean) = {
val c = if (result) success else failure
c.attributes(Attributes.of("thought", path.last)).inc
}

private def warn(ai: AI, path: List[String], invariant: String): Unit < AIs =
ai.systemMessage(
p"""
Thought Invariant Failure
=========================
Description: $invariant
Path: ${path.map(v => s"`$v`").mkString(".")}
Plase analyze and fix any mistakes.
"""
)

case class Info(result: Boolean) extends Thought {
override def eval(path: List[String], ai: AI) =
observe(path, result)
}

object Info {
implicit val schema: ZSchema[Info] =
ZSchema.primitive[Boolean].transform(Info(_), _.result)
}

case class Warn[Invarant <: String](
`Invariant check description`: Invarant,
`Invariant holds`: Boolean
) extends Thought {
override def eval(path: List[String], ai: AI) =
observe(path, `Invariant holds`).andThen {
warn(ai, path, `Invariant check description`)
}
}

case class Fail[Invarant <: String](
`Invariant check description`: Invarant,
`Invariant check analysis`: String,
`Invariant holds`: Boolean
) extends Thought {
override def eval(path: List[String], ai: AI) =
observe(path, `Invariant holds`).andThen {
warn(ai, path, `Invariant check description`).andThen {
IOs.fail(CheckFailed(path, `Invariant check description`, `Invariant check analysis`))
}
}
}

case class SelfRepair[Invarant <: String](
`Invariant check description`: Invarant,
`Invariant check analysis`: String,
`Invariant holds`: Boolean
) extends Thought {
override def eval(path: List[String], ai: AI) =
observe(path, `Invariant holds`).andThen {
AIs.ephemeral {
warn(ai, path, `Invariant check description`).andThen {
ai.gen[Repair]("Provide a repair for the failed thought invariant.")
}
}.map { repair =>
ai.systemMessage(
p"""
Thought Invariant Repair
========================
Description: ${`Invariant check description`}
Path: ${path.map(v => s"`$v`").mkString(".")}
Inferred Repair: ${pprint(repair)}
"""
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ case class Contextualize(
`Determine the core message or question in the input`: String,
`Assess how the current input relates to previous discussions or knowledge`: String,
`Identify any ambiguous or unclear aspects that need further clarification`: String
) extends Thought.Opening
) extends Thought
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ case class Elaborate(
`Generate a json that is complete and as detailed as possible`: true,
`Strategy to elaborate outputs and generate long texts`: String,
`I'll elaborate the outputs and use bulleted lists if appropiate`: true
) extends Thought.Opening
) extends Thought
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package kyo.llm.thoughts
case class Expert(
`I am an expert in the task requested by the user`: true,
`My skills that can be useful to perform the task`: String
) extends Thought.Opening
) extends Thought
2 changes: 1 addition & 1 deletion kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Humor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ case class Humor(
`Comedic elements identified`: String,
`Strategy for humorous content creation`: String,
`Appropriateness and audience sensitivity`: String
) extends Thought.Opening
) extends Thought
4 changes: 2 additions & 2 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Reduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ case class Reduce[Expr, Result](
completeStepByStepReduce: List[ReduceStep[Expr]],
fullyReducedResult: Result,
`Reduce steps and result are not empty`: Boolean
) extends Thought.Opening
) extends Thought

case class ReduceStep[Expr](
ruleDescription: String,
inputExpression: Expr,
`Description of inputExpression`: String,
`Method to apply rule`: String,
outputExpression: Expr
) extends Thought.Opening
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ case class Refine[T](
`Aspects to double check for mistakes`: List[String],
`Elaborate on how to fix mistakes`: String,
finalCorrectSolution: T
) extends Thought.Opening
) extends Thought
4 changes: 2 additions & 2 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Remember.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package kyo.llm.thoughts

final case class Remember[T <: String](
`Remeber`: T
) extends Thought.Opening
`Remeber`: T
) extends Thought
4 changes: 2 additions & 2 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Repair.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kyo.llm.thoughts
import kyo.llm.ais._

case class Repair(
`Check for failures from function calls`: String,
`Check for failures from tool and system messages`: String,
`Identify causes of the failures`: String,
`Detail corrective measures for improvement`: String
) extends Thought.Opening
) extends Thought
2 changes: 1 addition & 1 deletion kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Role.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package kyo.llm.thoughts
case class Role[Desc <: String](
`The user has defined the following const string as my role`: Desc,
`Strategy to assume the role`: String
) extends Thought.Opening
) extends Thought
36 changes: 21 additions & 15 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Thought.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ import kyo._
import kyo.llm.ais._
import zio.schema.{Schema => ZSchema}
import scala.reflect.ClassTag
import kyo.concurrent.fibers.Fibers
import kyo.stats.Stats

sealed abstract class Thought {
abstract class Thought {
self: Product =>

class Check(
name: String,
description: String,
f: () => Boolean < AIs
)

object Check {
def apply(name: String, description: String = "")(f: => Boolean < AIs): Check =
new Check(name, description, () => f)
final def handle(path: List[String], ai: AI): Unit < AIs = {
val npath = productPrefix :: path
val thoughts =
eval(npath, ai).andThen {
(0 until productArity).flatMap { idx =>
productElement(idx) match {
case v: Thought =>
Some((productElementName(idx), v))
case _ =>
None
}
}
}
AIs.parallelTraverse(thoughts)((f, t) => t.eval(f :: npath, ai)).unit
}

def checks: List[Check] = Nil
def eval(path: List[String], ai: AI): Unit < AIs = ()
}

object Thought {

abstract class Opening extends Thought {
self: Product =>
}
val stats = Stats.initScope("thoughts")

abstract class Closing extends Thought {
self: Product =>
}
Expand All @@ -42,7 +48,7 @@ object Thought {
new Info {
type Thought = T
val name = t.runtimeClass.getSimpleName()
val opening = classOf[Opening].isAssignableFrom(t.runtimeClass)
val opening = !classOf[Closing].isAssignableFrom(t.runtimeClass)
val zSchema = j.zSchema
}
}

0 comments on commit faabde5

Please sign in to comment.