Skip to content

Commit

Permalink
ais: thoughts improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Dec 18, 2023
1 parent 6734ca9 commit c3d8ea0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
20 changes: 14 additions & 6 deletions kyo-llm-macros/shared/src/main/scala/kyo/llm/json/JsonSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,50 @@ object Schema {
"description" -> Json.Str(v)
}.distinct.toList

def constDesc: List[(String, Json)] =
List("description" -> Json.Str(
"Note the json schema 'const' definition. It is the only possible value for the field."
))

def convert(schema: ZSchema[_]): List[(String, Json)] = {
def desc = this.desc(schema.annotations)
schema match {

case ZSchema.Primitive(StandardType.StringType, Chunk(Const(v))) =>
desc ++ List("const" -> Json.Str(v.asInstanceOf[String]))
constDesc ++ List("type" -> Json.Str("string"), "const" -> Json.Str(v.asInstanceOf[String]))

case ZSchema.Primitive(StandardType.StringType, _) =>
desc ++ List("type" -> Json.Str("string"))

case ZSchema.Primitive(StandardType.IntType, Chunk(Const(v))) =>
desc ++ List("const" -> Json.Num(v.asInstanceOf[Int]))
constDesc ++ List("type" -> Json.Str("integer"), "const" -> Json.Num(v.asInstanceOf[Int]))

case ZSchema.Primitive(StandardType.IntType, _) =>
desc ++ List("type" -> Json.Str("integer"), "format" -> Json.Str("int32"))

case ZSchema.Primitive(StandardType.LongType, Chunk(Const(v))) =>
desc ++ List("const" -> Json.Num(v.asInstanceOf[Long]))
constDesc ++ List("type" -> Json.Str("integer"), "const" -> Json.Num(v.asInstanceOf[Long]))

case ZSchema.Primitive(StandardType.LongType, _) =>
desc ++ List("type" -> Json.Str("integer"), "format" -> Json.Str("int64"))

case ZSchema.Primitive(StandardType.DoubleType, Chunk(Const(v))) =>
desc ++ List("const" -> Json.Num(v.asInstanceOf[Double]))
constDesc ++ List("type" -> Json.Str("number"), "const" -> Json.Num(v.asInstanceOf[Double]))

case ZSchema.Primitive(StandardType.DoubleType, _) =>
desc ++ List("type" -> Json.Str("number"))

case ZSchema.Primitive(StandardType.FloatType, Chunk(Const(v))) =>
desc ++ List("const" -> Json.Num(v.asInstanceOf[Float]))
constDesc ++ List("type" -> Json.Str("number"), "const" -> Json.Num(v.asInstanceOf[Float]))

case ZSchema.Primitive(StandardType.FloatType, _) =>
desc ++ List("type" -> Json.Str("number"), "format" -> Json.Str("float"))

case ZSchema.Primitive(StandardType.BoolType, Chunk(Const(v))) =>
desc ++ List("const" -> Json.Bool(v.asInstanceOf[Boolean]))
constDesc ++ List(
"type" -> Json.Str("boolean"),
"const" -> Json.Bool(v.asInstanceOf[Boolean])
)

case ZSchema.Primitive(StandardType.BoolType, _) =>
desc ++ List("type" -> Json.Str("boolean"))
Expand Down
81 changes: 44 additions & 37 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ object thoughts {
)

object Reasoning {
case class StartReasoning(
`I understand that the fields under 'reasoning' are meant as a way for me to reason about the json I need to generate`: Boolean,
`When generating the values for the 'reasoning' fields, I won't mention themselves in the reasoning process`: Boolean,
`Some fields in the json schema have a 'const' definition. I understand the provided const value is the only possible value for the field`: Boolean
)
case class Contextualize(
`When I disconsider the fields under 'reasoning' and analyze the other fields in the json schema, I briefly conclude the purpose of the other fields is`: String,
`My goal generating this data is`: String,
Expand All @@ -24,17 +29,20 @@ object thoughts {
`I noticed that I previously failed to produce valid data`: Boolean,
`The reasons I failed previously are`: List[String]
)
case class Step(
name: String,
description: String,
`potential mistakes`: String
)
case class Plan(
`Considering all the information provided so far, I'll produce a plan to generate high-quality data that satisfies the user's needs`: Boolean,
`Known formal and informal techniques that can help in the generation of the data`: String,
`Steps to generate the data`: List[String],
`Most likely reasons I might fail to generate high-quality data`: List[String]
)
case class StartReasoning(
`I understand that the fields under 'reasoning' is meant as a way for me to reason about the json I need to generate`: Boolean,
`When generating the values for the 'reasoning' fields, I won't mention themselves in the reasoning process`: Boolean
`Formal techniques from literature I can be used to aid the generation`: String,
`Additional informal techniques`: String,
`Let's think step by step`: List[Step],
`Now that I have the plan steps, I'll generate 'endReasoning'`: Boolean
)
case class EndReasoning(
`This reasoning process was helpful to generate the json because`: String,
`I understand the reasoning process has ended and will now focus on generating actual data requested by the user based on the reasoning`: Boolean
)
}
Expand All @@ -56,42 +64,41 @@ object thoughts {
)
}

type NonEmpty = "Non-empty"

case class Constrain[T, C <: String](
@desc("Constraints to consider when generating the value")
constraints: C,
`constraints to consider to proceed with the 'value' json generation`: C,
value: T
)

type NonEmpty = "Non-empty"
}

// object tt extends KyoLLMApp {
object tt extends KyoLLMApp {

// import thoughts._
// import kyo.llm.ais._
import thoughts._
import kyo.llm.ais._

// case class CQA(
// @desc("Excerpt from the input text")
// excerpt: Constrain[String, NonEmpty],
// @desc("An elaborate question regarding the excerpt")
// question: Constrain[String, NonEmpty],
// @desc("A comprehensive answer")
// answer: Constrain[String, NonEmpty]
// )
// case class Req(
// reasoning: Reasoning,
// @desc("Comprehensive set of questions covering all information in the input text")
// questions: Collect[Constrain[String, NonEmpty]],
// @desc("Process each question")
// processed: Collect[CQA]
// )
case class CQA(
@desc("Excerpt from the input text")
excerpt: Constrain[String, NonEmpty],
@desc("An elaborate question regarding the excerpt")
question: Constrain[String, NonEmpty],
@desc("A comprehensive answer")
answer: Constrain[String, NonEmpty]
)
case class Req(
reasoning: Reasoning,
@desc("Comprehensive set of questions covering all information in the input text")
questions: Collect[Constrain[String, NonEmpty]],
@desc("Process each question")
processedQuestions: Constrain[Collect[CQA], NonEmpty]
)

// run {
// AIs.gen[Req](text)
// }
run {
AIs.gen[Req](text)
}

// def text =
// p"""
// General relativity is a theory of gravitation developed by Einstein in the years 1907–1915. The development of general relativity began with the equivalence principle, under which the states of accelerated motion and being at rest in a gravitational field (for example, when standing on the surface of the Earth) are physically identical. The upshot of this is that free fall is inertial motion: an object in free fall is falling because that is how objects move when there is no force being exerted on them, instead of this being due to the force of gravity as is the case in classical mechanics. This is incompatible with classical mechanics and special relativity because in those theories inertially moving objects cannot accelerate with respect to each other, but objects in free fall do so. To resolve this difficulty Einstein first proposed that spacetime is curved. Einstein discussed his idea with mathematician Marcel Grossmann and they concluded that general relativity could be formulated in the context of Riemannian geometry which had been developed in the 1800s.[10] In 1915, he devised the Einstein field equations which relate the curvature of spacetime with the mass, energy, and any momentum within it.
// """
// }
def text =
p"""
General relativity is a theory of gravitation developed by Einstein in the years 1907–1915. The development of general relativity began with the equivalence principle, under which the states of accelerated motion and being at rest in a gravitational field (for example, when standing on the surface of the Earth) are physically identical. The upshot of this is that free fall is inertial motion: an object in free fall is falling because that is how objects move when there is no force being exerted on them, instead of this being due to the force of gravity as is the case in classical mechanics. This is incompatible with classical mechanics and special relativity because in those theories inertially moving objects cannot accelerate with respect to each other, but objects in free fall do so. To resolve this difficulty Einstein first proposed that spacetime is curved. Einstein discussed his idea with mathematician Marcel Grossmann and they concluded that general relativity could be formulated in the context of Riemannian geometry which had been developed in the 1800s.[10] In 1915, he devised the Einstein field equations which relate the curvature of spacetime with the mass, energy, and any momentum within it.
"""
}

0 comments on commit c3d8ea0

Please sign in to comment.